blob: 7b682572cf06f54de7840b3716404f29472250fe [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 Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static 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 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static 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 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static 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 +0200453static 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 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000730static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000732static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000733static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000735static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200736#ifdef FEAT_FLOAT
737static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#ifdef FEAT_FLOAT
744static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
745#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200747static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200748static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100749static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100753static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000760static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100764static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000766static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000767static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int get_env_len __ARGS((char_u **arg));
769static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
772#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
773#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
774 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000775static 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 +0000776static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000777static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100778static 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 +0000779static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static typval_T *alloc_tv __ARGS((void));
781static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static void init_tv __ARGS((typval_T *varp));
783static long get_tv_number __ARGS((typval_T *varp));
784static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000785static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static char_u *get_tv_string __ARGS((typval_T *varp));
787static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000788static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100789static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
790static 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 +0000791static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
792static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
793static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000794static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
795static 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 +0000796static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
797static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000798static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200799static int var_check_func_name __ARGS((char_u *name, int new_var));
800static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000801static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000802static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
804static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
805static int eval_fname_script __ARGS((char_u *p));
806static int eval_fname_sid __ARGS((char_u *p));
807static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static ufunc_T *find_func __ARGS((char_u *name));
809static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000810static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
812static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000813static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
814static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_total_cmp __ARGS((const void *s1, const void *s2));
820static int
821# ifdef __BORLANDC__
822 _RTLENTRYF
823# endif
824 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200826static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000827static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000828static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static 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 +0000831static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
832static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
835static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000836static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000838static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200840
841#ifdef EBCDIC
842static int compare_func_name __ARGS((const void *s1, const void *s2));
843static void sortFunctions __ARGS(());
844#endif
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
856 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200857 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000858 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
861 for (i = 0; i < VV_LEN; ++i)
862 {
863 p = &vimvars[i];
864 STRCPY(p->vv_di.di_key, p->vv_name);
865 if (p->vv_flags & VV_RO)
866 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
867 else if (p->vv_flags & VV_RO_SBX)
868 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
869 else
870 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000871
872 /* add to v: scope dict, unless the value is not always available */
873 if (p->vv_type != VAR_UNKNOWN)
874 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000876 /* add to compat scope dict */
877 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000879 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100880 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200881 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
884 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100885 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886 */
887 sortFunctions();
888#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000889}
890
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000891#if defined(EXITFREE) || defined(PROTO)
892 void
893eval_clear()
894{
895 int i;
896 struct vimvar *p;
897
898 for (i = 0; i < VV_LEN; ++i)
899 {
900 p = &vimvars[i];
901 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000903 vim_free(p->vv_str);
904 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000905 }
906 else if (p->vv_di.di_tv.v_type == VAR_LIST)
907 {
908 list_unref(p->vv_list);
909 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 }
912 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000913 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 hash_clear(&compat_hashtab);
915
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100917# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200918 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100919# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920
921 /* global variables */
922 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000923
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000924 /* autoloaded script names */
925 ga_clear_strings(&ga_loaded);
926
Bram Moolenaarcca74132013-09-25 21:00:28 +0200927 /* Script-local variables. First clear all the variables and in a second
928 * loop free the scriptvar_T, because a variable in one script might hold
929 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200932 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 ga_clear(&ga_scripts);
935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000936 /* unreferenced lists and dicts */
937 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000938
939 /* functions */
940 free_all_functions();
941 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942}
943#endif
944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Return the name of the executed function.
947 */
948 char_u *
949func_name(cookie)
950 void *cookie;
951{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the next breakpoint line for a funccall cookie.
957 */
958 linenr_T *
959func_breakpoint(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the debug tick for a funccall cookie.
967 */
968 int *
969func_dbg_tick(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the nesting level for a funccall cookie.
977 */
978 int
979func_level(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000986funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000988/* pointer to list of previously used funccal, still around because some
989 * item in it is still being used. */
990funccall_T *previous_funccal = NULL;
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Return TRUE when a function was ended by a ":return" command.
994 */
995 int
996current_func_returned()
997{
998 return current_funccal->returned;
999}
1000
1001
1002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * Set an internal variable to a string value. Creates the variable if it does
1004 * not already exist.
1005 */
1006 void
1007set_internal_string_var(name, value)
1008 char_u *name;
1009 char_u *value;
1010{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 val = vim_strsave(value);
1015 if (val != NULL)
1016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001018 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 }
1024}
1025
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028static char_u *redir_endp = NULL;
1029static char_u *redir_varname = NULL;
1030
1031/*
1032 * Start recording command output to a variable
1033 * Returns OK if successfully completed the setup. FAIL otherwise.
1034 */
1035 int
1036var_redir_start(name, append)
1037 char_u *name;
1038 int append; /* append to an existing variable */
1039{
1040 int save_emsg;
1041 int err;
1042 typval_T tv;
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001045 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 {
1047 EMSG(_(e_invarg));
1048 return FAIL;
1049 }
1050
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001051 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 redir_varname = vim_strsave(name);
1053 if (redir_varname == NULL)
1054 return FAIL;
1055
1056 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1057 if (redir_lval == NULL)
1058 {
1059 var_redir_stop();
1060 return FAIL;
1061 }
1062
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001063 /* The output is stored in growarray "redir_ga" until redirection ends. */
1064 ga_init2(&redir_ga, (int)sizeof(char), 500);
1065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001067 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001068 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1070 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001071 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp != NULL && *redir_endp != NUL)
1073 /* Trailing characters are present after the variable name */
1074 EMSG(_(e_trailing));
1075 else
1076 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 var_redir_stop();
1079 return FAIL;
1080 }
1081
1082 /* check if we can write to the variable: set it to or append an empty
1083 * string */
1084 save_emsg = did_emsg;
1085 did_emsg = FALSE;
1086 tv.v_type = VAR_STRING;
1087 tv.vval.v_string = (char_u *)"";
1088 if (append)
1089 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1090 else
1091 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001092 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001094 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (err)
1096 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001097 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 var_redir_stop();
1099 return FAIL;
1100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106 * Append "value[value_len]" to the variable set by var_redir_start().
1107 * The actual appending is postponed until redirection ends, because the value
1108 * appended may in fact be the string we write to, changing it may cause freed
1109 * memory to be used:
1110 * :redir => foo
1111 * :let foo
1112 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 */
1114 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
1121 if (redir_lval == NULL)
1122 return;
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 {
1131 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 }
1134 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136}
1137
1138/*
1139 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
1143var_redir_stop()
1144{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145 typval_T tv;
1146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_lval != NULL)
1148 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* If there was no error: assign the text to the variable. */
1150 if (redir_endp != NULL)
1151 {
1152 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 /* Call get_lval() again, if it's inside a Dict or List it may
1156 * have changed. */
1157 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001158 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1160 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1161 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 /* free the collected output */
1165 vim_free(redir_ga.ga_data);
1166 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 vim_free(redir_lval);
1169 redir_lval = NULL;
1170 }
1171 vim_free(redir_varname);
1172 redir_varname = NULL;
1173}
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# if defined(FEAT_MBYTE) || defined(PROTO)
1176 int
1177eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1178 char_u *enc_from;
1179 char_u *enc_to;
1180 char_u *fname_from;
1181 char_u *fname_to;
1182{
1183 int err = FALSE;
1184
1185 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1186 set_vim_var_string(VV_CC_TO, enc_to, -1);
1187 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1188 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1189 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1190 err = TRUE;
1191 set_vim_var_string(VV_CC_FROM, NULL, -1);
1192 set_vim_var_string(VV_CC_TO, NULL, -1);
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1195
1196 if (err)
1197 return FAIL;
1198 return OK;
1199}
1200# endif
1201
1202# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1203 int
1204eval_printexpr(fname, args)
1205 char_u *fname;
1206 char_u *args;
1207{
1208 int err = FALSE;
1209
1210 set_vim_var_string(VV_FNAME_IN, fname, -1);
1211 set_vim_var_string(VV_CMDARG, args, -1);
1212 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1215 set_vim_var_string(VV_CMDARG, NULL, -1);
1216
1217 if (err)
1218 {
1219 mch_remove(fname);
1220 return FAIL;
1221 }
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_DIFF) || defined(PROTO)
1227 void
1228eval_diff(origfile, newfile, outfile)
1229 char_u *origfile;
1230 char_u *newfile;
1231 char_u *outfile;
1232{
1233 int err = FALSE;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243
1244 void
1245eval_patch(origfile, difffile, outfile)
1246 char_u *origfile;
1247 char_u *difffile;
1248 char_u *outfile;
1249{
1250 int err;
1251
1252 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1253 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1254 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1255 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259}
1260# endif
1261
1262/*
1263 * Top level evaluation function, returning a boolean.
1264 * Sets "error" to TRUE if there was an error.
1265 * Return TRUE or FALSE.
1266 */
1267 int
1268eval_to_bool(arg, error, nextcmd, skip)
1269 char_u *arg;
1270 int *error;
1271 char_u **nextcmd;
1272 int skip; /* only parse, don't execute */
1273{
Bram Moolenaar33570922005-01-25 22:26:29 +00001274 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int retval = FALSE;
1276
1277 if (skip)
1278 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else
1282 {
1283 *error = FALSE;
1284 if (!skip)
1285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 }
1290 if (skip)
1291 --emsg_skip;
1292
1293 return retval;
1294}
1295
1296/*
1297 * Top level evaluation function, returning a string. If "skip" is TRUE,
1298 * only parsing to "nextcmd" is done, without reporting errors. Return
1299 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1300 */
1301 char_u *
1302eval_to_string_skip(arg, nextcmd, skip)
1303 char_u *arg;
1304 char_u **nextcmd;
1305 int skip; /* only parse, don't execute */
1306{
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *retval;
1309
1310 if (skip)
1311 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 retval = NULL;
1314 else
1315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 retval = vim_strsave(get_tv_string(&tv));
1317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 if (skip)
1320 --emsg_skip;
1321
1322 return retval;
1323}
1324
1325/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326 * Skip over an expression at "*pp".
1327 * Return FAIL for an error, OK otherwise.
1328 */
1329 int
1330skip_expr(pp)
1331 char_u **pp;
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334
1335 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337}
1338
1339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 * When "convert" is TRUE convert a List into a sequence of lines and convert
1342 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Return pointer to allocated memory, or NULL for failure.
1344 */
1345 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *arg;
1348 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001354#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 retval = NULL;
1360 else
1361 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 {
1364 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001365 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001366 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001367 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001368 if (tv.vval.v_list->lv_len > 0)
1369 ga_append(&ga, NL);
1370 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 ga_append(&ga, NUL);
1372 retval = (char_u *)ga.ga_data;
1373 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374#ifdef FEAT_FLOAT
1375 else if (convert && tv.v_type == VAR_FLOAT)
1376 {
1377 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1378 retval = vim_strsave(numbuf);
1379 }
1380#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 else
1382 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 * Call eval_to_string() without using current local variables and using
1391 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 */
1393 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *arg;
1396 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 char_u *retval;
1400 void *save_funccalp;
1401
1402 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403 if (use_sandbox)
1404 ++sandbox;
1405 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 if (use_sandbox)
1408 --sandbox;
1409 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 restore_funccal(save_funccalp);
1411 return retval;
1412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Top level evaluation function, returning a number.
1416 * Evaluates "expr" silently.
1417 * Returns -1 for an error.
1418 */
1419 int
1420eval_to_number(expr)
1421 char_u *expr;
1422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001425 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427 ++emsg_off;
1428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 retval = -1;
1431 else
1432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001433 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436 --emsg_off;
1437
1438 return retval;
1439}
1440
Bram Moolenaara40058a2005-07-11 22:42:07 +00001441/*
1442 * Prepare v: variable "idx" to be used.
1443 * Save the current typeval in "save_tv".
1444 * When not used yet add the variable to the v: hashtable.
1445 */
1446 static void
1447prepare_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 *save_tv = vimvars[idx].vv_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1454}
1455
1456/*
1457 * Restore v: variable "idx" to typeval "save_tv".
1458 * When no longer defined, remove the variable from the v: hashtable.
1459 */
1460 static void
1461restore_vimvar(idx, save_tv)
1462 int idx;
1463 typval_T *save_tv;
1464{
1465 hashitem_T *hi;
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467 vimvars[idx].vv_tv = *save_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 {
1470 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1471 if (HASHITEM_EMPTY(hi))
1472 EMSG2(_(e_intern2), "restore_vimvar()");
1473 else
1474 hash_remove(&vimvarht, hi);
1475 }
1476}
1477
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001478#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001479/*
1480 * Evaluate an expression to a list with suggestions.
1481 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001482 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001483 */
1484 list_T *
1485eval_spell_expr(badword, expr)
1486 char_u *badword;
1487 char_u *expr;
1488{
1489 typval_T save_val;
1490 typval_T rettv;
1491 list_T *list = NULL;
1492 char_u *p = skipwhite(expr);
1493
1494 /* Set "v:val" to the bad word. */
1495 prepare_vimvar(VV_VAL, &save_val);
1496 vimvars[VV_VAL].vv_type = VAR_STRING;
1497 vimvars[VV_VAL].vv_str = badword;
1498 if (p_verbose == 0)
1499 ++emsg_off;
1500
1501 if (eval1(&p, &rettv, TRUE) == OK)
1502 {
1503 if (rettv.v_type != VAR_LIST)
1504 clear_tv(&rettv);
1505 else
1506 list = rettv.vval.v_list;
1507 }
1508
1509 if (p_verbose == 0)
1510 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001511 restore_vimvar(VV_VAL, &save_val);
1512
1513 return list;
1514}
1515
1516/*
1517 * "list" is supposed to contain two items: a word and a number. Return the
1518 * word in "pp" and the number as the return value.
1519 * Return -1 if anything isn't right.
1520 * Used to get the good word and score from the eval_spell_expr() result.
1521 */
1522 int
1523get_spellword(list, pp)
1524 list_T *list;
1525 char_u **pp;
1526{
1527 listitem_T *li;
1528
1529 li = list->lv_first;
1530 if (li == NULL)
1531 return -1;
1532 *pp = get_tv_string(&li->li_tv);
1533
1534 li = li->li_next;
1535 if (li == NULL)
1536 return -1;
1537 return get_tv_number(&li->li_tv);
1538}
1539#endif
1540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 * Top level evaluation function.
1543 * Returns an allocated typval_T with the result.
1544 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 */
1546 typval_T *
1547eval_expr(arg, nextcmd)
1548 char_u *arg;
1549 char_u **nextcmd;
1550{
1551 typval_T *tv;
1552
1553 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 {
1556 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 }
1559
1560 return tv;
1561}
1562
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 * Uses argv[argc] for the function arguments. Only Number and String
1567 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001570 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001571call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 char_u *func;
1573 int argc;
1574 char_u **argv;
1575 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001576 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar33570922005-01-25 22:26:29 +00001579 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 long n;
1581 int len;
1582 int i;
1583 int doesrange;
1584 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001587 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 for (i = 0; i < argc; i++)
1592 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 /* Pass a NULL or empty argument as an empty string */
1594 if (argv[i] == NULL || *argv[i] == NUL)
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_STRING;
1597 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001598 continue;
1599 }
1600
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001601 if (str_arg_only)
1602 len = 0;
1603 else
1604 /* Recognize a number argument, the others must be strings. */
1605 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (len != 0 && len == (int)STRLEN(argv[i]))
1607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001608 argvars[i].v_type = VAR_NUMBER;
1609 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001613 argvars[i].v_type = VAR_STRING;
1614 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 }
1617
1618 if (safe)
1619 {
1620 save_funccalp = save_funccal();
1621 ++sandbox;
1622 }
1623
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1625 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (safe)
1629 {
1630 --sandbox;
1631 restore_funccal(save_funccalp);
1632 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 vim_free(argvars);
1634
1635 if (ret == FAIL)
1636 clear_tv(rettv);
1637
1638 return ret;
1639}
1640
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001641/*
1642 * Call vimL function "func" and return the result as a number.
1643 * Returns -1 when calling the function fails.
1644 * Uses argv[argc] for the function arguments.
1645 */
1646 long
1647call_func_retnr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
1654 long retval;
1655
1656 /* All arguments are passed as strings, no conversion to number. */
1657 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1658 return -1;
1659
1660 retval = get_tv_number_chk(&rettv, NULL);
1661 clear_tv(&rettv);
1662 return retval;
1663}
1664
1665#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1666 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1667
Bram Moolenaar4f688582007-07-24 12:34:30 +00001668# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670 * Call vimL function "func" and return the result as a string.
1671 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Uses argv[argc] for the function arguments.
1673 */
1674 void *
1675call_func_retstr(func, argc, argv, safe)
1676 char_u *func;
1677 int argc;
1678 char_u **argv;
1679 int safe; /* use the sandbox */
1680{
1681 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001684 /* All arguments are passed as strings, no conversion to number. */
1685 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 return NULL;
1687
1688 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 return retval;
1691}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001695 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001697 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 */
1699 void *
1700call_func_retlist(func, argc, argv, safe)
1701 char_u *func;
1702 int argc;
1703 char_u **argv;
1704 int safe; /* use the sandbox */
1705{
1706 typval_T rettv;
1707
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 return NULL;
1711
1712 if (rettv.v_type != VAR_LIST)
1713 {
1714 clear_tv(&rettv);
1715 return NULL;
1716 }
1717
1718 return rettv.vval.v_list;
1719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721
1722/*
1723 * Save the current function call pointer, and set it to NULL.
1724 * Used when executing autocommands and for ":source".
1725 */
1726 void *
1727save_funccal()
1728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 current_funccal = NULL;
1732 return (void *)fc;
1733}
1734
1735 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736restore_funccal(vfc)
1737 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = (funccall_T *)vfc;
1740
1741 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742}
1743
Bram Moolenaar05159a02005-02-26 23:04:13 +00001744#if defined(FEAT_PROFILE) || defined(PROTO)
1745/*
1746 * Prepare profiling for entering a child or something else that is not
1747 * counted for the script/function itself.
1748 * Should always be called in pair with prof_child_exit().
1749 */
1750 void
1751prof_child_enter(tm)
1752 proftime_T *tm; /* place to store waittime */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 profile_start(&fc->prof_child);
1758 script_prof_save(tm);
1759}
1760
1761/*
1762 * Take care of time spent in a child.
1763 * Should always be called after prof_child_enter().
1764 */
1765 void
1766prof_child_exit(tm)
1767 proftime_T *tm; /* where waittime was stored */
1768{
1769 funccall_T *fc = current_funccal;
1770
1771 if (fc != NULL && fc->func->uf_profiling)
1772 {
1773 profile_end(&fc->prof_child);
1774 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1775 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1776 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1777 }
1778 script_prof_restore(tm);
1779}
1780#endif
1781
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#ifdef FEAT_FOLDING
1784/*
1785 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1786 * it in "*cp". Doesn't give error messages.
1787 */
1788 int
1789eval_foldexpr(arg, cp)
1790 char_u *arg;
1791 int *cp;
1792{
Bram Moolenaar33570922005-01-25 22:26:29 +00001793 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 int retval;
1795 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001796 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1797 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001800 if (use_sandbox)
1801 ++sandbox;
1802 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 if (tv.v_type == VAR_NUMBER)
1810 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001811 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 retval = 0;
1813 else
1814 {
1815 /* If the result is a string, check if there is a non-digit before
1816 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (!VIM_ISDIGIT(*s) && *s != '-')
1819 *cp = *s++;
1820 retval = atol((char *)s);
1821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 --sandbox;
1827 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 return retval;
1830}
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 * ":let" list all variable values
1835 * ":let var1 var2" list variable values
1836 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 * ":let var += expr" assignment command.
1838 * ":let var -= expr" assignment command.
1839 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 void
1843ex_let(eap)
1844 exarg_T *eap;
1845{
1846 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 int var_count = 0;
1851 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001852 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 argend = skip_var_list(arg, &var_count, &semicolon);
1857 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001859 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1860 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001861 expr = skipwhite(argend);
1862 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1863 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001865 /*
1866 * ":let" without "=": list variables
1867 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 if (*arg == '[')
1869 EMSG(_(e_invarg));
1870 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_glob_vars(&first);
1877 list_buf_vars(&first);
1878 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001879#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_script_vars(&first);
1883 list_func_vars(&first);
1884 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 eap->nextcmd = check_nextcmd(arg);
1887 }
1888 else
1889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 op[0] = '=';
1891 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001892 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001894 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1895 op[0] = *expr; /* +=, -= or .= */
1896 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001898 else
1899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002250 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002530 * flags:
2531 * GLV_QUIET: do not give error messages
2532 * GLV_NO_AUTOLOAD: do not use script autoloading
2533 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002535 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 */
2538 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 typval_T *rettv;
2542 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 int unlet;
2544 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002545 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002546 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 char_u *expr_start, *expr_end;
2550 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 dictitem_T *v;
2552 typval_T var1;
2553 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563
2564 if (skip)
2565 {
2566 /* When skipping just find the end of the name. */
2567 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002568 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 }
2570
2571 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (expr_start != NULL)
2574 {
2575 /* Don't expand the name when we already know there is an error. */
2576 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2577 && *p != '[' && *p != '.')
2578 {
2579 EMSG(_(e_trailing));
2580 return NULL;
2581 }
2582
2583 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2584 if (lp->ll_exp_name == NULL)
2585 {
2586 /* Report an invalid expression in braces, unless the
2587 * expression evaluation has been cancelled due to an
2588 * aborting error, an interrupt, or an exception. */
2589 if (!aborting() && !quiet)
2590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002591 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 EMSG2(_(e_invarg2), name);
2593 return NULL;
2594 }
2595 }
2596 lp->ll_name = lp->ll_exp_name;
2597 }
2598 else
2599 lp->ll_name = name;
2600
2601 /* Without [idx] or .key we are done. */
2602 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2603 return p;
2604
2605 cc = *p;
2606 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002607 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (v == NULL && !quiet)
2609 EMSG2(_(e_undefvar), lp->ll_name);
2610 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 if (v == NULL)
2612 return NULL;
2613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 /*
2615 * Loop until no more [idx] or .key is following.
2616 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2621 && !(lp->ll_tv->v_type == VAR_DICT
2622 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E689: Can only index a List or Dictionary"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!quiet)
2631 EMSG(_("E708: [:] must come last"));
2632 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 len = -1;
2636 if (*p == '.')
2637 {
2638 key = p + 1;
2639 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2640 ;
2641 if (len == 0)
2642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_(e_emptykey));
2645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647 p = key + len;
2648 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (*p == ':')
2654 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 else
2656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 empty1 = FALSE;
2658 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002660 if (get_tv_string_chk(&var1) == NULL)
2661 {
2662 /* not a number or string */
2663 clear_tv(&var1);
2664 return NULL;
2665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 }
2667
2668 /* Optionally get the second index [ :expr]. */
2669 if (*p == ':')
2670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (rettv != NULL && (rettv->v_type != VAR_LIST
2680 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 p = skipwhite(p + 1);
2689 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 else
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (!empty1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (get_tv_string_chk(&var2) == NULL)
2701 {
2702 /* not a number or string */
2703 if (!empty1)
2704 clear_tv(&var1);
2705 clear_tv(&var2);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724
2725 /* Skip to past ']'. */
2726 ++p;
2727 }
2728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 {
2731 if (len == -1)
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (*key == NUL)
2736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (!quiet)
2738 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 lp->ll_list = NULL;
2744 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 /* When assigning to a scope dictionary check that a function and
2748 * variable name is valid (only variable name unless it is l: or
2749 * g: dictionary). Disallow overwriting a builtin function. */
2750 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002752 int prevval;
2753 int wrong;
2754
2755 if (len != -1)
2756 {
2757 prevval = key[len];
2758 key[len] = NUL;
2759 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002760 else
2761 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2763 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 || !valid_varname(key);
2766 if (len != -1)
2767 key[len] = prevval;
2768 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 return NULL;
2770 }
2771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 /* Can't add "v:" variable. */
2775 if (lp->ll_dict == &vimvardict)
2776 {
2777 EMSG2(_(e_illvar), name);
2778 return NULL;
2779 }
2780
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (len == -1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 p = NULL;
2798 break;
2799 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* existing variable, need to check if it can be changed */
2801 else if (var_check_ro(lp->ll_di->di_flags, name))
2802 return NULL;
2803
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 else
2809 {
2810 /*
2811 * Get the number and item for the only or first index of the List.
2812 */
2813 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
2816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 clear_tv(&var1);
2819 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_list = lp->ll_tv->vval.v_list;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 if (lp->ll_n1 < 0)
2826 {
2827 lp->ll_n1 = 0;
2828 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2829 }
2830 }
2831 if (lp->ll_li == NULL)
2832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839
2840 /*
2841 * May need to find the item or absolute index for the second
2842 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 * When no index given: "lp->ll_empty2" is TRUE.
2844 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002854 {
2855 if (!quiet)
2856 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2863 if (lp->ll_n1 < 0)
2864 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2865 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 {
2867 if (!quiet)
2868 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 }
2872
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 }
2876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return p;
2878}
2879
2880/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 */
2883 static void
2884clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 vim_free(lp->ll_exp_name);
2888 vim_free(lp->ll_newkey);
2889}
2890
2891/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 */
2896 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903{
2904 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 listitem_T *ri;
2906 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907
2908 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 cc = *endp;
2913 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 if (op != NULL && *op != '=')
2915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002919 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002920 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 {
2922 if (tv_op(&tv, rettv, op) == OK)
2923 set_var(lp->ll_name, &tv, FALSE);
2924 clear_tv(&tv);
2925 }
2926 }
2927 else
2928 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 else if (tv_check_lock(lp->ll_newkey == NULL
2933 ? lp->ll_tv->v_lock
2934 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2935 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 else if (lp->ll_range)
2937 {
2938 /*
2939 * Assign the List values to the list items.
2940 */
2941 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 if (op != NULL && *op != '=')
2944 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2945 else
2946 {
2947 clear_tv(&lp->ll_li->li_tv);
2948 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 ri = ri->li_next;
2951 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2952 break;
2953 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002956 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 ri = NULL;
2959 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 lp->ll_li = lp->ll_li->li_next;
2963 ++lp->ll_n1;
2964 }
2965 if (ri != NULL)
2966 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 else if (lp->ll_empty2
2968 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 : lp->ll_n1 != lp->ll_n2)
2970 EMSG(_("E711: List value has not enough items"));
2971 }
2972 else
2973 {
2974 /*
2975 * Assign to a List or Dictionary item.
2976 */
2977 if (lp->ll_newkey != NULL)
2978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 if (op != NULL && *op != '=')
2980 {
2981 EMSG2(_(e_letwrong), op);
2982 return;
2983 }
2984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002986 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 if (di == NULL)
2988 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2990 {
2991 vim_free(di);
2992 return;
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else if (op != NULL && *op != '=')
2997 {
2998 tv_op(lp->ll_tv, rettv, op);
2999 return;
3000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003003
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 /*
3005 * Assign the value to the variable or list item.
3006 */
3007 if (copy)
3008 copy_tv(rettv, lp->ll_tv);
3009 else
3010 {
3011 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003012 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 }
3015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003016}
3017
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3020 * Returns OK or FAIL.
3021 */
3022 static int
3023tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 typval_T *tv1;
3025 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 char_u *op;
3027{
3028 long n;
3029 char_u numbuf[NUMBUFLEN];
3030 char_u *s;
3031
3032 /* Can't do anything with a Funcref or a Dict on the right. */
3033 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3034 {
3035 switch (tv1->v_type)
3036 {
3037 case VAR_DICT:
3038 case VAR_FUNC:
3039 break;
3040
3041 case VAR_LIST:
3042 if (*op != '+' || tv2->v_type != VAR_LIST)
3043 break;
3044 /* List += List */
3045 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3046 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3047 return OK;
3048
3049 case VAR_NUMBER:
3050 case VAR_STRING:
3051 if (tv2->v_type == VAR_LIST)
3052 break;
3053 if (*op == '+' || *op == '-')
3054 {
3055 /* nr += nr or nr -= nr*/
3056 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057#ifdef FEAT_FLOAT
3058 if (tv2->v_type == VAR_FLOAT)
3059 {
3060 float_T f = n;
3061
3062 if (*op == '+')
3063 f += tv2->vval.v_float;
3064 else
3065 f -= tv2->vval.v_float;
3066 clear_tv(tv1);
3067 tv1->v_type = VAR_FLOAT;
3068 tv1->vval.v_float = f;
3069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071#endif
3072 {
3073 if (*op == '+')
3074 n += get_tv_number(tv2);
3075 else
3076 n -= get_tv_number(tv2);
3077 clear_tv(tv1);
3078 tv1->v_type = VAR_NUMBER;
3079 tv1->vval.v_number = n;
3080 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 }
3082 else
3083 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084 if (tv2->v_type == VAR_FLOAT)
3085 break;
3086
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 /* str .= str */
3088 s = get_tv_string(tv1);
3089 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3090 clear_tv(tv1);
3091 tv1->v_type = VAR_STRING;
3092 tv1->vval.v_string = s;
3093 }
3094 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095
3096#ifdef FEAT_FLOAT
3097 case VAR_FLOAT:
3098 {
3099 float_T f;
3100
3101 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3102 && tv2->v_type != VAR_NUMBER
3103 && tv2->v_type != VAR_STRING))
3104 break;
3105 if (tv2->v_type == VAR_FLOAT)
3106 f = tv2->vval.v_float;
3107 else
3108 f = get_tv_number(tv2);
3109 if (*op == '+')
3110 tv1->vval.v_float += f;
3111 else
3112 tv1->vval.v_float -= f;
3113 }
3114 return OK;
3115#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 }
3117 }
3118
3119 EMSG2(_(e_letwrong), op);
3120 return FAIL;
3121}
3122
3123/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 * Add a watcher to a list.
3125 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003126 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 list_T *l;
3129 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130{
3131 lw->lw_next = l->lv_watch;
3132 l->lv_watch = lw;
3133}
3134
3135/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003136 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 * No warning when it isn't found...
3138 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003139 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 list_T *l;
3142 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143{
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145
3146 lwp = &l->lv_watch;
3147 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3148 {
3149 if (lw == lwrem)
3150 {
3151 *lwp = lw->lw_next;
3152 break;
3153 }
3154 lwp = &lw->lw_next;
3155 }
3156}
3157
3158/*
3159 * Just before removing an item from a list: advance watchers to the next
3160 * item.
3161 */
3162 static void
3163list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 list_T *l;
3165 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166{
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3170 if (lw->lw_item == item)
3171 lw->lw_item = item->li_next;
3172}
3173
3174/*
3175 * Evaluate the expression used in a ":for var in expr" command.
3176 * "arg" points to "var".
3177 * Set "*errp" to TRUE for an error, FALSE otherwise;
3178 * Return a pointer that holds the info. Null when there is an error.
3179 */
3180 void *
3181eval_for_line(arg, errp, nextcmdp, skip)
3182 char_u *arg;
3183 int *errp;
3184 char_u **nextcmdp;
3185 int skip;
3186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 typval_T tv;
3190 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 *errp = TRUE; /* default: there is an error */
3193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 if (fi == NULL)
3196 return NULL;
3197
3198 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3199 if (expr == NULL)
3200 return fi;
3201
3202 expr = skipwhite(expr);
3203 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003205 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 return fi;
3207 }
3208
3209 if (skip)
3210 ++emsg_skip;
3211 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3212 {
3213 *errp = FALSE;
3214 if (!skip)
3215 {
3216 l = tv.vval.v_list;
3217 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003220 clear_tv(&tv);
3221 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 else
3223 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003224 /* No need to increment the refcount, it's already set for the
3225 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 fi->fi_list = l;
3227 list_add_watch(l, &fi->fi_lw);
3228 fi->fi_lw.lw_item = l->lv_first;
3229 }
3230 }
3231 }
3232 if (skip)
3233 --emsg_skip;
3234
3235 return fi;
3236}
3237
3238/*
3239 * Use the first item in a ":for" list. Advance to the next.
3240 * Assign the values to the variable (list). "arg" points to the first one.
3241 * Return TRUE when a valid item was found, FALSE when at end of list or
3242 * something wrong.
3243 */
3244 int
3245next_for_item(fi_void, arg)
3246 void *fi_void;
3247 char_u *arg;
3248{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 item = fi->fi_lw.lw_item;
3254 if (item == NULL)
3255 result = FALSE;
3256 else
3257 {
3258 fi->fi_lw.lw_item = item->li_next;
3259 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3260 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3261 }
3262 return result;
3263}
3264
3265/*
3266 * Free the structure used to store info used by ":for".
3267 */
3268 void
3269free_for_info(fi_void)
3270 void *fi_void;
3271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003274 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 list_unref(fi->fi_list);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 vim_free(fi);
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3283
3284 void
3285set_context_for_expression(xp, arg, cmdidx)
3286 expand_T *xp;
3287 char_u *arg;
3288 cmdidx_T cmdidx;
3289{
3290 int got_eq = FALSE;
3291 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 if (cmdidx == CMD_let)
3295 {
3296 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003297 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 {
3299 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 if (vim_iswhite(*p))
3305 break;
3306 }
3307 return;
3308 }
3309 }
3310 else
3311 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3312 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 while ((xp->xp_pattern = vim_strpbrk(arg,
3314 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3315 {
3316 c = *xp->xp_pattern;
3317 if (c == '&')
3318 {
3319 c = xp->xp_pattern[1];
3320 if (c == '&')
3321 {
3322 ++xp->xp_pattern;
3323 xp->xp_context = cmdidx != CMD_let || got_eq
3324 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3325 }
3326 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003329 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3330 xp->xp_pattern += 2;
3331
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else if (c == '$')
3335 {
3336 /* environment variable */
3337 xp->xp_context = EXPAND_ENV_VARS;
3338 }
3339 else if (c == '=')
3340 {
3341 got_eq = TRUE;
3342 xp->xp_context = EXPAND_EXPRESSION;
3343 }
3344 else if (c == '<'
3345 && xp->xp_context == EXPAND_FUNCTIONS
3346 && vim_strchr(xp->xp_pattern, '(') == NULL)
3347 {
3348 /* Function name can start with "<SNR>" */
3349 break;
3350 }
3351 else if (cmdidx != CMD_let || got_eq)
3352 {
3353 if (c == '"') /* string */
3354 {
3355 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3356 if (c == '\\' && xp->xp_pattern[1] != NUL)
3357 ++xp->xp_pattern;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '\'') /* literal string */
3361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3364 /* skip */ ;
3365 xp->xp_context = EXPAND_NOTHING;
3366 }
3367 else if (c == '|')
3368 {
3369 if (xp->xp_pattern[1] == '|')
3370 {
3371 ++xp->xp_pattern;
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
3375 xp->xp_context = EXPAND_COMMANDS;
3376 }
3377 else
3378 xp->xp_context = EXPAND_EXPRESSION;
3379 }
3380 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 /* Doesn't look like something valid, expand as an expression
3382 * anyway. */
3383 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 arg = xp->xp_pattern;
3385 if (*arg != NUL)
3386 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3387 /* skip */ ;
3388 }
3389 xp->xp_pattern = arg;
3390}
3391
3392#endif /* FEAT_CMDL_COMPL */
3393
3394/*
3395 * ":1,25call func(arg1, arg2)" function call.
3396 */
3397 void
3398ex_call(eap)
3399 exarg_T *eap;
3400{
3401 char_u *arg = eap->arg;
3402 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003404 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 linenr_T lnum;
3408 int doesrange;
3409 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003412 if (eap->skip)
3413 {
3414 /* trans_function_name() doesn't work well when skipping, use eval0()
3415 * instead to skip to any following command, e.g. for:
3416 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003417 ++emsg_skip;
3418 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3419 clear_tv(&rettv);
3420 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003421 return;
3422 }
3423
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003425 if (fudi.fd_newkey != NULL)
3426 {
3427 /* Still need to give an error message for missing key. */
3428 EMSG2(_(e_dictkey), fudi.fd_newkey);
3429 vim_free(fudi.fd_newkey);
3430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 if (tofree == NULL)
3432 return;
3433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 /* Increase refcount on dictionary, it could get deleted when evaluating
3435 * the arguments. */
3436 if (fudi.fd_dict != NULL)
3437 ++fudi.fd_dict->dv_refcount;
3438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003440 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003441 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar532c7802005-01-27 14:44:31 +00003443 /* Skip white space to allow ":call func ()". Not good, but required for
3444 * backward compatibility. */
3445 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (*startarg != '(')
3449 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003450 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 goto end;
3452 }
3453
3454 /*
3455 * When skipping, evaluate the function once, to find the end of the
3456 * arguments.
3457 * When the function takes a range, this is discovered after the first
3458 * call, and the loop is broken.
3459 */
3460 if (eap->skip)
3461 {
3462 ++emsg_skip;
3463 lnum = eap->line2; /* do it once, also with an invalid range */
3464 }
3465 else
3466 lnum = eap->line1;
3467 for ( ; lnum <= eap->line2; ++lnum)
3468 {
3469 if (!eap->skip && eap->addr_count > 0)
3470 {
3471 curwin->w_cursor.lnum = lnum;
3472 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003473#ifdef FEAT_VIRTUALEDIT
3474 curwin->w_cursor.coladd = 0;
3475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 eap->line1, eap->line2, &doesrange,
3480 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 failed = TRUE;
3483 break;
3484 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003485
3486 /* Handle a function returning a Funcref, Dictionary or List. */
3487 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3488 {
3489 failed = TRUE;
3490 break;
3491 }
3492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (doesrange || eap->skip)
3495 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003498 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003500 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (aborting())
3502 break;
3503 }
3504 if (eap->skip)
3505 --emsg_skip;
3506
3507 if (!failed)
3508 {
3509 /* Check for trailing illegal characters and a following command. */
3510 if (!ends_excmd(*arg))
3511 {
3512 emsg_severe = TRUE;
3513 EMSG(_(e_trailing));
3514 }
3515 else
3516 eap->nextcmd = check_nextcmd(arg);
3517 }
3518
3519end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003521 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524/*
3525 * ":unlet[!] var1 ... " command.
3526 */
3527 void
3528ex_unlet(eap)
3529 exarg_T *eap;
3530{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 ex_unletlock(eap, eap->arg, 0);
3532}
3533
3534/*
3535 * ":lockvar" and ":unlockvar" commands
3536 */
3537 void
3538ex_lockvar(eap)
3539 exarg_T *eap;
3540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 int deep = 2;
3543
3544 if (eap->forceit)
3545 deep = -1;
3546 else if (vim_isdigit(*arg))
3547 {
3548 deep = getdigits(&arg);
3549 arg = skipwhite(arg);
3550 }
3551
3552 ex_unletlock(eap, arg, deep);
3553}
3554
3555/*
3556 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3557 */
3558 static void
3559ex_unletlock(eap, argstart, deep)
3560 exarg_T *eap;
3561 char_u *argstart;
3562 int deep;
3563{
3564 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 do
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003572 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003573 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 if (lv.ll_name == NULL)
3575 error = TRUE; /* error but continue parsing */
3576 if (name_end == NULL || (!vim_iswhite(*name_end)
3577 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 if (name_end != NULL)
3580 {
3581 emsg_severe = TRUE;
3582 EMSG(_(e_trailing));
3583 }
3584 if (!(eap->skip || error))
3585 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587 }
3588
3589 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 {
3591 if (eap->cmdidx == CMD_unlet)
3592 {
3593 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3594 error = TRUE;
3595 }
3596 else
3597 {
3598 if (do_lock_var(&lv, name_end, deep,
3599 eap->cmdidx == CMD_lockvar) == FAIL)
3600 error = TRUE;
3601 }
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 if (!eap->skip)
3605 clear_lval(&lv);
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 arg = skipwhite(name_end);
3608 } while (!ends_excmd(*arg));
3609
3610 eap->nextcmd = check_nextcmd(arg);
3611}
3612
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 int forceit;
3618{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 int ret = OK;
3620 int cc;
3621
3622 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 cc = *name_end;
3625 *name_end = NUL;
3626
3627 /* Normal name or expanded name. */
3628 if (check_changedtick(lp->ll_name))
3629 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3635 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else if (lp->ll_range)
3637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639
3640 /* Delete a range of List items. */
3641 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3642 {
3643 li = lp->ll_li->li_next;
3644 listitem_remove(lp->ll_list, lp->ll_li);
3645 lp->ll_li = li;
3646 ++lp->ll_n1;
3647 }
3648 }
3649 else
3650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 /* unlet a List item. */
3653 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003656 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 }
3658
3659 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660}
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 hashtab_T *ht;
3672 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003674 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 ht = find_var_ht(name, &varname);
3677 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hi = hash_find(ht, varname);
3680 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003682 di = HI2DI(hi);
3683 if (var_check_fixed(di->di_flags, name)
3684 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
3686 delete_var(ht, hi);
3687 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690 if (forceit)
3691 return OK;
3692 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694}
3695
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696/*
3697 * Lock or unlock variable indicated by "lp".
3698 * "deep" is the levels to go (-1 for unlimited);
3699 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3700 */
3701 static int
3702do_lock_var(lp, name_end, deep, lock)
3703 lval_T *lp;
3704 char_u *name_end;
3705 int deep;
3706 int lock;
3707{
3708 int ret = OK;
3709 int cc;
3710 dictitem_T *di;
3711
3712 if (deep == 0) /* nothing to do */
3713 return OK;
3714
3715 if (lp->ll_tv == NULL)
3716 {
3717 cc = *name_end;
3718 *name_end = NUL;
3719
3720 /* Normal name or expanded name. */
3721 if (check_changedtick(lp->ll_name))
3722 ret = FAIL;
3723 else
3724 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003725 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 if (di == NULL)
3727 ret = FAIL;
3728 else
3729 {
3730 if (lock)
3731 di->di_flags |= DI_FLAGS_LOCK;
3732 else
3733 di->di_flags &= ~DI_FLAGS_LOCK;
3734 item_lock(&di->di_tv, deep, lock);
3735 }
3736 }
3737 *name_end = cc;
3738 }
3739 else if (lp->ll_range)
3740 {
3741 listitem_T *li = lp->ll_li;
3742
3743 /* (un)lock a range of List items. */
3744 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3745 {
3746 item_lock(&li->li_tv, deep, lock);
3747 li = li->li_next;
3748 ++lp->ll_n1;
3749 }
3750 }
3751 else if (lp->ll_list != NULL)
3752 /* (un)lock a List item. */
3753 item_lock(&lp->ll_li->li_tv, deep, lock);
3754 else
3755 /* un(lock) a Dictionary item. */
3756 item_lock(&lp->ll_di->di_tv, deep, lock);
3757
3758 return ret;
3759}
3760
3761/*
3762 * Lock or unlock an item. "deep" is nr of levels to go.
3763 */
3764 static void
3765item_lock(tv, deep, lock)
3766 typval_T *tv;
3767 int deep;
3768 int lock;
3769{
3770 static int recurse = 0;
3771 list_T *l;
3772 listitem_T *li;
3773 dict_T *d;
3774 hashitem_T *hi;
3775 int todo;
3776
3777 if (recurse >= DICT_MAXNEST)
3778 {
3779 EMSG(_("E743: variable nested too deep for (un)lock"));
3780 return;
3781 }
3782 if (deep == 0)
3783 return;
3784 ++recurse;
3785
3786 /* lock/unlock the item itself */
3787 if (lock)
3788 tv->v_lock |= VAR_LOCKED;
3789 else
3790 tv->v_lock &= ~VAR_LOCKED;
3791
3792 switch (tv->v_type)
3793 {
3794 case VAR_LIST:
3795 if ((l = tv->vval.v_list) != NULL)
3796 {
3797 if (lock)
3798 l->lv_lock |= VAR_LOCKED;
3799 else
3800 l->lv_lock &= ~VAR_LOCKED;
3801 if (deep < 0 || deep > 1)
3802 /* recursive: lock/unlock the items the List contains */
3803 for (li = l->lv_first; li != NULL; li = li->li_next)
3804 item_lock(&li->li_tv, deep - 1, lock);
3805 }
3806 break;
3807 case VAR_DICT:
3808 if ((d = tv->vval.v_dict) != NULL)
3809 {
3810 if (lock)
3811 d->dv_lock |= VAR_LOCKED;
3812 else
3813 d->dv_lock &= ~VAR_LOCKED;
3814 if (deep < 0 || deep > 1)
3815 {
3816 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003817 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3819 {
3820 if (!HASHITEM_EMPTY(hi))
3821 {
3822 --todo;
3823 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3824 }
3825 }
3826 }
3827 }
3828 }
3829 --recurse;
3830}
3831
Bram Moolenaara40058a2005-07-11 22:42:07 +00003832/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003833 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3834 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835 */
3836 static int
3837tv_islocked(tv)
3838 typval_T *tv;
3839{
3840 return (tv->v_lock & VAR_LOCKED)
3841 || (tv->v_type == VAR_LIST
3842 && tv->vval.v_list != NULL
3843 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3844 || (tv->v_type == VAR_DICT
3845 && tv->vval.v_dict != NULL
3846 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3847}
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3850/*
3851 * Delete all "menutrans_" variables.
3852 */
3853 void
3854del_menutrans_vars()
3855{
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003860 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 {
3863 if (!HASHITEM_EMPTY(hi))
3864 {
3865 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3867 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 }
3869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871}
3872#endif
3873
3874#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3875
3876/*
3877 * Local string buffer for the next two functions to store a variable name
3878 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3879 * get_user_var_name().
3880 */
3881
3882static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3883
3884static char_u *varnamebuf = NULL;
3885static int varnamebuflen = 0;
3886
3887/*
3888 * Function to concatenate a prefix and a variable name.
3889 */
3890 static char_u *
3891cat_prefix_varname(prefix, name)
3892 int prefix;
3893 char_u *name;
3894{
3895 int len;
3896
3897 len = (int)STRLEN(name) + 3;
3898 if (len > varnamebuflen)
3899 {
3900 vim_free(varnamebuf);
3901 len += 10; /* some additional space */
3902 varnamebuf = alloc(len);
3903 if (varnamebuf == NULL)
3904 {
3905 varnamebuflen = 0;
3906 return NULL;
3907 }
3908 varnamebuflen = len;
3909 }
3910 *varnamebuf = prefix;
3911 varnamebuf[1] = ':';
3912 STRCPY(varnamebuf + 2, name);
3913 return varnamebuf;
3914}
3915
3916/*
3917 * Function given to ExpandGeneric() to obtain the list of user defined
3918 * (global/buffer/window/built-in) variable names.
3919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u *
3921get_user_var_name(xp, idx)
3922 expand_T *xp;
3923 int idx;
3924{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static long_u gdone;
3926 static long_u bdone;
3927 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928#ifdef FEAT_WINDOWS
3929 static long_u tdone;
3930#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 static int vidx;
3932 static hashitem_T *hi;
3933 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003936 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003938#ifdef FEAT_WINDOWS
3939 tdone = 0;
3940#endif
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* Global variables */
3944 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3953 return cat_prefix_varname('g', hi->hi_key);
3954 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003971 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return (char_u *)"b:changedtick";
3973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974
3975 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003976 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003979 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003981 else
3982 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 while (HASHITEM_EMPTY(hi))
3984 ++hi;
3985 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988#ifdef FEAT_WINDOWS
3989 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003990 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991 if (tdone < ht->ht_used)
3992 {
3993 if (tdone++ == 0)
3994 hi = ht->ht_array;
3995 else
3996 ++hi;
3997 while (HASHITEM_EMPTY(hi))
3998 ++hi;
3999 return cat_prefix_varname('t', hi->hi_key);
4000 }
4001#endif
4002
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 /* v: variables */
4004 if (vidx < VV_LEN)
4005 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 vim_free(varnamebuf);
4008 varnamebuf = NULL;
4009 varnamebuflen = 0;
4010 return NULL;
4011}
4012
4013#endif /* FEAT_CMDL_COMPL */
4014
4015/*
4016 * types for expressions.
4017 */
4018typedef enum
4019{
4020 TYPE_UNKNOWN = 0
4021 , TYPE_EQUAL /* == */
4022 , TYPE_NEQUAL /* != */
4023 , TYPE_GREATER /* > */
4024 , TYPE_GEQUAL /* >= */
4025 , TYPE_SMALLER /* < */
4026 , TYPE_SEQUAL /* <= */
4027 , TYPE_MATCH /* =~ */
4028 , TYPE_NOMATCH /* !~ */
4029} exptype_T;
4030
4031/*
4032 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4035 */
4036
4037/*
4038 * Handle zero level expression.
4039 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004041 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * Return OK or FAIL.
4043 */
4044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **nextcmd;
4049 int evaluate;
4050{
4051 int ret;
4052 char_u *p;
4053
4054 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (ret == FAIL || !ends_excmd(*p))
4057 {
4058 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
4061 * Report the invalid expression unless the expression evaluation has
4062 * been cancelled due to an aborting error, an interrupt, or an
4063 * exception.
4064 */
4065 if (!aborting())
4066 EMSG2(_(e_invexpr2), arg);
4067 ret = FAIL;
4068 }
4069 if (nextcmd != NULL)
4070 *nextcmd = check_nextcmd(p);
4071
4072 return ret;
4073}
4074
4075/*
4076 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004077 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 *
4079 * "arg" must point to the first non-white of the expression.
4080 * "arg" is advanced to the next non-white after the recognized expression.
4081 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
4083 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * Return OK or FAIL.
4085 */
4086 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 int evaluate;
4091{
4092 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 /*
4096 * Get the first variable.
4097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 if ((*arg)[0] == '?')
4102 {
4103 result = FALSE;
4104 if (evaluate)
4105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int error = FALSE;
4107
4108 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004111 if (error)
4112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /*
4116 * Get the second variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121
4122 /*
4123 * Check for the ":".
4124 */
4125 if ((*arg)[0] != ':')
4126 {
4127 EMSG(_("E109: Missing ':' after '?'"));
4128 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131 }
4132
4133 /*
4134 * Get the third variable.
4135 */
4136 *arg = skipwhite(*arg + 1);
4137 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4138 {
4139 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142 }
4143 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Handle first level expression:
4152 * expr2 || expr2 || expr2 logical OR
4153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
4157 * Return OK or FAIL.
4158 */
4159 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int evaluate;
4164{
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 long result;
4167 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 /*
4177 * Repeat until there is no following "||".
4178 */
4179 first = TRUE;
4180 result = FALSE;
4181 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4182 {
4183 if (evaluate && first)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 first = FALSE;
4191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 2);
4197 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4198 return FAIL;
4199
4200 /*
4201 * Compute the result.
4202 */
4203 if (evaluate && !result)
4204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (error)
4209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 if (evaluate)
4212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 rettv->v_type = VAR_NUMBER;
4214 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle second level expression:
4223 * expr3 && expr3 && expr3 logical AND
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 long result;
4238 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * Get the first variable.
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
4246
4247 /*
4248 * Repeat until there is no following "&&".
4249 */
4250 first = TRUE;
4251 result = TRUE;
4252 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4253 {
4254 if (evaluate && first)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 first = FALSE;
4262 }
4263
4264 /*
4265 * Get the second variable.
4266 */
4267 *arg = skipwhite(*arg + 2);
4268 if (eval4(arg, &var2, evaluate && result) == FAIL)
4269 return FAIL;
4270
4271 /*
4272 * Compute the result.
4273 */
4274 if (evaluate && result)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (evaluate)
4283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle third level expression:
4294 * var1 == var2
4295 * var1 =~ var2
4296 * var1 != var2
4297 * var1 !~ var2
4298 * var1 > var2
4299 * var1 >= var2
4300 * var1 < var2
4301 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 * var1 is var2
4303 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 *
4305 * "arg" must point to the first non-white of the expression.
4306 * "arg" is advanced to the next non-white after the recognized expression.
4307 *
4308 * Return OK or FAIL.
4309 */
4310 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int evaluate;
4315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 char_u *p;
4318 int i;
4319 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004320 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int len = 2;
4322 long n1, n2;
4323 char_u *s1, *s2;
4324 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4325 regmatch_T regmatch;
4326 int ic;
4327 char_u *save_cpo;
4328
4329 /*
4330 * Get the first variable.
4331 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return FAIL;
4334
4335 p = *arg;
4336 switch (p[0])
4337 {
4338 case '=': if (p[1] == '=')
4339 type = TYPE_EQUAL;
4340 else if (p[1] == '~')
4341 type = TYPE_MATCH;
4342 break;
4343 case '!': if (p[1] == '=')
4344 type = TYPE_NEQUAL;
4345 else if (p[1] == '~')
4346 type = TYPE_NOMATCH;
4347 break;
4348 case '>': if (p[1] != '=')
4349 {
4350 type = TYPE_GREATER;
4351 len = 1;
4352 }
4353 else
4354 type = TYPE_GEQUAL;
4355 break;
4356 case '<': if (p[1] != '=')
4357 {
4358 type = TYPE_SMALLER;
4359 len = 1;
4360 }
4361 else
4362 type = TYPE_SEQUAL;
4363 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364 case 'i': if (p[1] == 's')
4365 {
4366 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4367 len = 5;
4368 if (!vim_isIDc(p[len]))
4369 {
4370 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4371 type_is = TRUE;
4372 }
4373 }
4374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376
4377 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 */
4380 if (type != TYPE_UNKNOWN)
4381 {
4382 /* extra question mark appended: ignore case */
4383 if (p[len] == '?')
4384 {
4385 ic = TRUE;
4386 ++len;
4387 }
4388 /* extra '#' appended: match case */
4389 else if (p[len] == '#')
4390 {
4391 ic = FALSE;
4392 ++len;
4393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 else
4396 ic = p_ic;
4397
4398 /*
4399 * Get the second variable.
4400 */
4401 *arg = skipwhite(p + len);
4402 if (eval5(arg, &var2, evaluate) == FAIL)
4403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
4406 }
4407
4408 if (evaluate)
4409 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004410 if (type_is && rettv->v_type != var2.v_type)
4411 {
4412 /* For "is" a different type always means FALSE, for "notis"
4413 * it means TRUE. */
4414 n1 = (type == TYPE_NEQUAL);
4415 }
4416 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4417 {
4418 if (type_is)
4419 {
4420 n1 = (rettv->v_type == var2.v_type
4421 && rettv->vval.v_list == var2.vval.v_list);
4422 if (type == TYPE_NEQUAL)
4423 n1 = !n1;
4424 }
4425 else if (rettv->v_type != var2.v_type
4426 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4427 {
4428 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004429 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004431 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 clear_tv(rettv);
4433 clear_tv(&var2);
4434 return FAIL;
4435 }
4436 else
4437 {
4438 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004439 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4440 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 if (type == TYPE_NEQUAL)
4442 n1 = !n1;
4443 }
4444 }
4445
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004446 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_dict == var2.vval.v_dict);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
4459 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4460 else
4461 EMSG(_("E736: Invalid operation for Dictionary"));
4462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4470 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4477 {
4478 if (rettv->v_type != var2.v_type
4479 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4480 {
4481 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004482 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004484 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 clear_tv(rettv);
4486 clear_tv(&var2);
4487 return FAIL;
4488 }
4489 else
4490 {
4491 /* Compare two Funcrefs for being equal or unequal. */
4492 if (rettv->vval.v_string == NULL
4493 || var2.vval.v_string == NULL)
4494 n1 = FALSE;
4495 else
4496 n1 = STRCMP(rettv->vval.v_string,
4497 var2.vval.v_string) == 0;
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 }
4502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004503#ifdef FEAT_FLOAT
4504 /*
4505 * If one of the two variables is a float, compare as a float.
4506 * When using "=~" or "!~", always compare as string.
4507 */
4508 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4509 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 {
4511 float_T f1, f2;
4512
4513 if (rettv->v_type == VAR_FLOAT)
4514 f1 = rettv->vval.v_float;
4515 else
4516 f1 = get_tv_number(rettv);
4517 if (var2.v_type == VAR_FLOAT)
4518 f2 = var2.vval.v_float;
4519 else
4520 f2 = get_tv_number(&var2);
4521 n1 = FALSE;
4522 switch (type)
4523 {
4524 case TYPE_EQUAL: n1 = (f1 == f2); break;
4525 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4526 case TYPE_GREATER: n1 = (f1 > f2); break;
4527 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4528 case TYPE_SMALLER: n1 = (f1 < f2); break;
4529 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4530 case TYPE_UNKNOWN:
4531 case TYPE_MATCH:
4532 case TYPE_NOMATCH: break; /* avoid gcc warning */
4533 }
4534 }
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /*
4538 * If one of the two variables is a number, compare as a number.
4539 * When using "=~" or "!~", always compare as string.
4540 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 n1 = get_tv_number(rettv);
4545 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 switch (type)
4547 {
4548 case TYPE_EQUAL: n1 = (n1 == n2); break;
4549 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4550 case TYPE_GREATER: n1 = (n1 > n2); break;
4551 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4552 case TYPE_SMALLER: n1 = (n1 < n2); break;
4553 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4554 case TYPE_UNKNOWN:
4555 case TYPE_MATCH:
4556 case TYPE_NOMATCH: break; /* avoid gcc warning */
4557 }
4558 }
4559 else
4560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 s1 = get_tv_string_buf(rettv, buf1);
4562 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4564 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4565 else
4566 i = 0;
4567 n1 = FALSE;
4568 switch (type)
4569 {
4570 case TYPE_EQUAL: n1 = (i == 0); break;
4571 case TYPE_NEQUAL: n1 = (i != 0); break;
4572 case TYPE_GREATER: n1 = (i > 0); break;
4573 case TYPE_GEQUAL: n1 = (i >= 0); break;
4574 case TYPE_SMALLER: n1 = (i < 0); break;
4575 case TYPE_SEQUAL: n1 = (i <= 0); break;
4576
4577 case TYPE_MATCH:
4578 case TYPE_NOMATCH:
4579 /* avoid 'l' flag in 'cpoptions' */
4580 save_cpo = p_cpo;
4581 p_cpo = (char_u *)"";
4582 regmatch.regprog = vim_regcomp(s2,
4583 RE_MAGIC + RE_STRING);
4584 regmatch.rm_ic = ic;
4585 if (regmatch.regprog != NULL)
4586 {
4587 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004588 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (type == TYPE_NOMATCH)
4590 n1 = !n1;
4591 }
4592 p_cpo = save_cpo;
4593 break;
4594
4595 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4596 }
4597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 clear_tv(rettv);
4599 clear_tv(&var2);
4600 rettv->v_type = VAR_NUMBER;
4601 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604
4605 return OK;
4606}
4607
4608/*
4609 * Handle fourth level expression:
4610 * + number addition
4611 * - number subtraction
4612 * . string concatenation
4613 *
4614 * "arg" must point to the first non-white of the expression.
4615 * "arg" is advanced to the next non-white after the recognized expression.
4616 *
4617 * Return OK or FAIL.
4618 */
4619 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 int evaluate;
4624{
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T var2;
4626 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int op;
4628 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629#ifdef FEAT_FLOAT
4630 float_T f1 = 0, f2 = 0;
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *s1, *s2;
4633 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4634 char_u *p;
4635
4636 /*
4637 * Get the first variable.
4638 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004639 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 return FAIL;
4641
4642 /*
4643 * Repeat computing, until no '+', '-' or '.' is following.
4644 */
4645 for (;;)
4646 {
4647 op = **arg;
4648 if (op != '+' && op != '-' && op != '.')
4649 break;
4650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004651 if ((op != '+' || rettv->v_type != VAR_LIST)
4652#ifdef FEAT_FLOAT
4653 && (op == '.' || rettv->v_type != VAR_FLOAT)
4654#endif
4655 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 {
4657 /* For "list + ...", an illegal use of the first operand as
4658 * a number cannot be determined before evaluating the 2nd
4659 * operand: if this is also a list, all is ok.
4660 * For "something . ...", "something - ..." or "non-list + ...",
4661 * we know that the first operand needs to be a string or number
4662 * without evaluating the 2nd operand. So check before to avoid
4663 * side effects after an error. */
4664 if (evaluate && get_tv_string_chk(rettv) == NULL)
4665 {
4666 clear_tv(rettv);
4667 return FAIL;
4668 }
4669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 /*
4672 * Get the second variable.
4673 */
4674 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004675 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679 }
4680
4681 if (evaluate)
4682 {
4683 /*
4684 * Compute the result.
4685 */
4686 if (op == '.')
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4689 s2 = get_tv_string_buf_chk(&var2, buf2);
4690 if (s2 == NULL) /* type error ? */
4691 {
4692 clear_tv(rettv);
4693 clear_tv(&var2);
4694 return FAIL;
4695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004696 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 clear_tv(rettv);
4698 rettv->v_type = VAR_STRING;
4699 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 else if (op == '+' && rettv->v_type == VAR_LIST
4702 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004703 {
4704 /* concatenate Lists */
4705 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4706 &var3) == FAIL)
4707 {
4708 clear_tv(rettv);
4709 clear_tv(&var2);
4710 return FAIL;
4711 }
4712 clear_tv(rettv);
4713 *rettv = var3;
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 else
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int error = FALSE;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719#ifdef FEAT_FLOAT
4720 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 f1 = rettv->vval.v_float;
4723 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 else
4726#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 n1 = get_tv_number_chk(rettv, &error);
4729 if (error)
4730 {
4731 /* This can only happen for "list + non-list". For
4732 * "non-list + ..." or "something - ...", we returned
4733 * before evaluating the 2nd operand. */
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (var2.v_type == VAR_FLOAT)
4739 f1 = n1;
4740#endif
4741 }
4742#ifdef FEAT_FLOAT
4743 if (var2.v_type == VAR_FLOAT)
4744 {
4745 f2 = var2.vval.v_float;
4746 n2 = 0;
4747 }
4748 else
4749#endif
4750 {
4751 n2 = get_tv_number_chk(&var2, &error);
4752 if (error)
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
4758#ifdef FEAT_FLOAT
4759 if (rettv->v_type == VAR_FLOAT)
4760 f2 = n2;
4761#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764
4765#ifdef FEAT_FLOAT
4766 /* If there is a float on either side the result is a float. */
4767 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4768 {
4769 if (op == '+')
4770 f1 = f1 + f2;
4771 else
4772 f1 = f1 - f2;
4773 rettv->v_type = VAR_FLOAT;
4774 rettv->vval.v_float = f1;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#endif
4778 {
4779 if (op == '+')
4780 n1 = n1 + n2;
4781 else
4782 n1 = n1 - n2;
4783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = n1;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 }
4790 return OK;
4791}
4792
4793/*
4794 * Handle fifth level expression:
4795 * * number multiplication
4796 * / number division
4797 * % number modulo
4798 *
4799 * "arg" must point to the first non-white of the expression.
4800 * "arg" is advanced to the next non-white after the recognized expression.
4801 *
4802 * Return OK or FAIL.
4803 */
4804 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004809 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 int op;
4813 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#ifdef FEAT_FLOAT
4815 int use_float = FALSE;
4816 float_T f1 = 0, f2;
4817#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 /*
4821 * Get the first variable.
4822 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004823 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return FAIL;
4825
4826 /*
4827 * Repeat computing, until no '*', '/' or '%' is following.
4828 */
4829 for (;;)
4830 {
4831 op = **arg;
4832 if (op != '*' && op != '/' && op != '%')
4833 break;
4834
4835 if (evaluate)
4836 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
4839 {
4840 f1 = rettv->vval.v_float;
4841 use_float = TRUE;
4842 n1 = 0;
4843 }
4844 else
4845#endif
4846 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 if (error)
4849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 else
4852 n1 = 0;
4853
4854 /*
4855 * Get the second variable.
4856 */
4857 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004858 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return FAIL;
4860
4861 if (evaluate)
4862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#ifdef FEAT_FLOAT
4864 if (var2.v_type == VAR_FLOAT)
4865 {
4866 if (!use_float)
4867 {
4868 f1 = n1;
4869 use_float = TRUE;
4870 }
4871 f2 = var2.vval.v_float;
4872 n2 = 0;
4873 }
4874 else
4875#endif
4876 {
4877 n2 = get_tv_number_chk(&var2, &error);
4878 clear_tv(&var2);
4879 if (error)
4880 return FAIL;
4881#ifdef FEAT_FLOAT
4882 if (use_float)
4883 f2 = n2;
4884#endif
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 if (op == '*')
4895 f1 = f1 * f2;
4896 else if (op == '/')
4897 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# ifdef VMS
4899 /* VMS crashes on divide by zero, work around it */
4900 if (f2 == 0.0)
4901 {
4902 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004903 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004904 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004905 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004906 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004907 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908 }
4909 else
4910 f1 = f1 / f2;
4911# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 /* We rely on the floating point library to handle divide
4913 * by zero to result in "inf" and not a crash. */
4914 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004915# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004919 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 return FAIL;
4921 }
4922 rettv->v_type = VAR_FLOAT;
4923 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 if (op == '*')
4929 n1 = n1 * n2;
4930 else if (op == '/')
4931 {
4932 if (n2 == 0) /* give an error message? */
4933 {
4934 if (n1 == 0)
4935 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4936 else if (n1 < 0)
4937 n1 = -0x7fffffffL;
4938 else
4939 n1 = 0x7fffffffL;
4940 }
4941 else
4942 n1 = n1 / n2;
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
4946 if (n2 == 0) /* give an error message? */
4947 n1 = 0;
4948 else
4949 n1 = n1 % n2;
4950 }
4951 rettv->v_type = VAR_NUMBER;
4952 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 }
4956
4957 return OK;
4958}
4959
4960/*
4961 * Handle sixth level expression:
4962 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004963 * "string" string constant
4964 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * &option-name option value
4966 * @r register contents
4967 * identifier variable value
4968 * function() function call
4969 * $VAR environment variable
4970 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004971 * [expr, expr] List
4972 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * Also handle:
4975 * ! in front logical NOT
4976 * - in front unary minus
4977 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * trailing [] subscript in String or List
4979 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 *
4981 * "arg" must point to the first non-white of the expression.
4982 * "arg" is advanced to the next non-white after the recognized expression.
4983 *
4984 * Return OK or FAIL.
4985 */
4986 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004987eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004991 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 long n;
4994 int len;
4995 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u *start_leader, *end_leader;
4997 int ret = OK;
4998 char_u *alias;
4999
5000 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * Skip '!' and '-' characters. They are handled later.
5008 */
5009 start_leader = *arg;
5010 while (**arg == '!' || **arg == '-' || **arg == '+')
5011 *arg = skipwhite(*arg + 1);
5012 end_leader = *arg;
5013
5014 switch (**arg)
5015 {
5016 /*
5017 * Number constant.
5018 */
5019 case '0':
5020 case '1':
5021 case '2':
5022 case '3':
5023 case '4':
5024 case '5':
5025 case '6':
5026 case '7':
5027 case '8':
5028 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 {
5030#ifdef FEAT_FLOAT
5031 char_u *p = skipdigits(*arg + 1);
5032 int get_float = FALSE;
5033
5034 /* We accept a float when the format matches
5035 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005036 * strict to avoid backwards compatibility problems.
5037 * Don't look for a float after the "." operator, so that
5038 * ":let vers = 1.2.3" doesn't fail. */
5039 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005041 get_float = TRUE;
5042 p = skipdigits(p + 2);
5043 if (*p == 'e' || *p == 'E')
5044 {
5045 ++p;
5046 if (*p == '-' || *p == '+')
5047 ++p;
5048 if (!vim_isdigit(*p))
5049 get_float = FALSE;
5050 else
5051 p = skipdigits(p + 1);
5052 }
5053 if (ASCII_ISALPHA(*p) || *p == '.')
5054 get_float = FALSE;
5055 }
5056 if (get_float)
5057 {
5058 float_T f;
5059
5060 *arg += string2float(*arg, &f);
5061 if (evaluate)
5062 {
5063 rettv->v_type = VAR_FLOAT;
5064 rettv->vval.v_float = f;
5065 }
5066 }
5067 else
5068#endif
5069 {
5070 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5071 *arg += len;
5072 if (evaluate)
5073 {
5074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = n;
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * String constant: "string".
5083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 break;
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 break;
5092
5093 /*
5094 * List: [expr, expr]
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 * Dictionary: {key: val, key: val}
5101 */
5102 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5103 break;
5104
5105 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Environment variable: $VAR.
5113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /*
5118 * Register contents: @r.
5119 */
5120 case '@': ++*arg;
5121 if (evaluate)
5122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005124 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 if (**arg != NUL)
5127 ++*arg;
5128 break;
5129
5130 /*
5131 * nested expression: (expression).
5132 */
5133 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (**arg == ')')
5136 ++*arg;
5137 else if (ret == OK)
5138 {
5139 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ret = FAIL;
5142 }
5143 break;
5144
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148
5149 if (ret == NOTDONE)
5150 {
5151 /*
5152 * Must be a variable or function name.
5153 * Can also be a curly-braces kind of name: {expr}.
5154 */
5155 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 if (alias != NULL)
5158 s = alias;
5159
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 ret = FAIL;
5162 else
5163 {
5164 if (**arg == '(') /* recursive! */
5165 {
5166 /* If "s" is the name of a variable of type VAR_FUNC
5167 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005168 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169
5170 /* Invoke the function. */
5171 ret = get_func_tv(s, len, rettv, arg,
5172 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005174
5175 /* If evaluate is FALSE rettv->v_type was not set in
5176 * get_func_tv, but it's needed in handle_subscript() to parse
5177 * what follows. So set it here. */
5178 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5179 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005180 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005181 rettv->v_type = VAR_FUNC;
5182 }
5183
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 /* Stop the expression evaluation when immediately
5185 * aborting on error, or when an interrupt occurred or
5186 * an exception was thrown but not caught. */
5187 if (aborting())
5188 {
5189 if (ret == OK)
5190 clear_tv(rettv);
5191 ret = FAIL;
5192 }
5193 }
5194 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005195 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005196 else
5197 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005199 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 }
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 *arg = skipwhite(*arg);
5203
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5205 * expr(expr). */
5206 if (ret == OK)
5207 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 /*
5210 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5211 */
5212 if (ret == OK && evaluate && end_leader > start_leader)
5213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 int val = 0;
5216#ifdef FEAT_FLOAT
5217 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 if (rettv->v_type == VAR_FLOAT)
5220 f = rettv->vval.v_float;
5221 else
5222#endif
5223 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 clear_tv(rettv);
5227 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 else
5230 {
5231 while (end_leader > start_leader)
5232 {
5233 --end_leader;
5234 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 {
5236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 f = !f;
5239 else
5240#endif
5241 val = !val;
5242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005244 {
5245#ifdef FEAT_FLOAT
5246 if (rettv->v_type == VAR_FLOAT)
5247 f = -f;
5248 else
5249#endif
5250 val = -val;
5251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253#ifdef FEAT_FLOAT
5254 if (rettv->v_type == VAR_FLOAT)
5255 {
5256 clear_tv(rettv);
5257 rettv->vval.v_float = f;
5258 }
5259 else
5260#endif
5261 {
5262 clear_tv(rettv);
5263 rettv->v_type = VAR_NUMBER;
5264 rettv->vval.v_number = val;
5265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268
5269 return ret;
5270}
5271
5272/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005273 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5274 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5276 */
5277 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283{
5284 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 long len = -1;
5288 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005294 if (verbose)
5295 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 return FAIL;
5297 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005298#ifdef FEAT_FLOAT
5299 else if (rettv->v_type == VAR_FLOAT)
5300 {
5301 if (verbose)
5302 EMSG(_(e_float_as_string));
5303 return FAIL;
5304 }
5305#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 /*
5310 * dict.name
5311 */
5312 key = *arg + 1;
5313 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5314 ;
5315 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 }
5319 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 /*
5322 * something[idx]
5323 *
5324 * Get the (first) variable from inside the [].
5325 */
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ':')
5328 empty1 = TRUE;
5329 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5330 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5332 {
5333 /* not a number or string */
5334 clear_tv(&var1);
5335 return FAIL;
5336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337
5338 /*
5339 * Get the second variable from inside the [:].
5340 */
5341 if (**arg == ':')
5342 {
5343 range = TRUE;
5344 *arg = skipwhite(*arg + 1);
5345 if (**arg == ']')
5346 empty2 = TRUE;
5347 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 if (!empty1)
5350 clear_tv(&var1);
5351 return FAIL;
5352 }
5353 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5354 {
5355 /* not a number or string */
5356 if (!empty1)
5357 clear_tv(&var1);
5358 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 return FAIL;
5360 }
5361 }
5362
5363 /* Check for the ']'. */
5364 if (**arg != ']')
5365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (verbose)
5367 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 clear_tv(&var1);
5369 if (range)
5370 clear_tv(&var2);
5371 return FAIL;
5372 }
5373 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375
5376 if (evaluate)
5377 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 n1 = 0;
5379 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 n1 = get_tv_number(&var1);
5382 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 if (range)
5385 {
5386 if (empty2)
5387 n2 = -1;
5388 else
5389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 n2 = get_tv_number(&var2);
5391 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 }
5394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
5397 case VAR_NUMBER:
5398 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 len = (long)STRLEN(s);
5401 if (range)
5402 {
5403 /* The resulting variable is a substring. If the indexes
5404 * are out of range the result is empty. */
5405 if (n1 < 0)
5406 {
5407 n1 = len + n1;
5408 if (n1 < 0)
5409 n1 = 0;
5410 }
5411 if (n2 < 0)
5412 n2 = len + n2;
5413 else if (n2 >= len)
5414 n2 = len;
5415 if (n1 >= len || n2 < 0 || n1 > n2)
5416 s = NULL;
5417 else
5418 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5419 }
5420 else
5421 {
5422 /* The resulting variable is a string of a single
5423 * character. If the index is too big or negative the
5424 * result is empty. */
5425 if (n1 >= len || n1 < 0)
5426 s = NULL;
5427 else
5428 s = vim_strnsave(s + n1, 1);
5429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 clear_tv(rettv);
5431 rettv->v_type = VAR_STRING;
5432 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 break;
5434
5435 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 if (n1 < 0)
5438 n1 = len + n1;
5439 if (!empty1 && (n1 < 0 || n1 >= len))
5440 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005441 /* For a range we allow invalid values and return an empty
5442 * list. A list index out of range is an error. */
5443 if (!range)
5444 {
5445 if (verbose)
5446 EMSGN(_(e_listidx), n1);
5447 return FAIL;
5448 }
5449 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 if (range)
5452 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 list_T *l;
5454 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455
5456 if (n2 < 0)
5457 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005458 else if (n2 >= len)
5459 n2 = len - 1;
5460 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005461 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 l = list_alloc();
5463 if (l == NULL)
5464 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 n1 <= n2; ++n1)
5467 {
5468 if (list_append_tv(l, &item->li_tv) == FAIL)
5469 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005470 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return FAIL;
5472 }
5473 item = item->li_next;
5474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 clear_tv(rettv);
5476 rettv->v_type = VAR_LIST;
5477 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005478 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 else
5481 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005482 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 clear_tv(rettv);
5484 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 }
5486 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487
5488 case VAR_DICT:
5489 if (range)
5490 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005491 if (verbose)
5492 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 if (len == -1)
5494 clear_tv(&var1);
5495 return FAIL;
5496 }
5497 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499
5500 if (len == -1)
5501 {
5502 key = get_tv_string(&var1);
5503 if (*key == NUL)
5504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005505 if (verbose)
5506 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 clear_tv(&var1);
5508 return FAIL;
5509 }
5510 }
5511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005512 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005515 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 if (len == -1)
5517 clear_tv(&var1);
5518 if (item == NULL)
5519 return FAIL;
5520
5521 copy_tv(&item->di_tv, &var1);
5522 clear_tv(rettv);
5523 *rettv = var1;
5524 }
5525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 }
5527 }
5528
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 return OK;
5530}
5531
5532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 * Get an option value.
5534 * "arg" points to the '&' or '+' before the option name.
5535 * "arg" is advanced to character after the option name.
5536 * Return OK or FAIL.
5537 */
5538 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 int evaluate;
5543{
5544 char_u *option_end;
5545 long numval;
5546 char_u *stringval;
5547 int opt_type;
5548 int c;
5549 int working = (**arg == '+'); /* has("+option") */
5550 int ret = OK;
5551 int opt_flags;
5552
5553 /*
5554 * Isolate the option name and find its value.
5555 */
5556 option_end = find_option_end(arg, &opt_flags);
5557 if (option_end == NULL)
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E112: Option name missing: %s"), *arg);
5561 return FAIL;
5562 }
5563
5564 if (!evaluate)
5565 {
5566 *arg = option_end;
5567 return OK;
5568 }
5569
5570 c = *option_end;
5571 *option_end = NUL;
5572 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 if (opt_type == -3) /* invalid name */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 EMSG2(_("E113: Unknown option: %s"), *arg);
5579 ret = FAIL;
5580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 if (opt_type == -2) /* hidden string option */
5584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 rettv->v_type = VAR_STRING;
5586 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 else if (opt_type == -1) /* hidden number option */
5589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 rettv->v_type = VAR_NUMBER;
5591 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 else if (opt_type == 1) /* number option */
5594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 rettv->v_type = VAR_NUMBER;
5596 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598 else /* string option */
5599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 rettv->v_type = VAR_STRING;
5601 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 }
5604 else if (working && (opt_type == -2 || opt_type == -1))
5605 ret = FAIL;
5606
5607 *option_end = c; /* put back for error messages */
5608 *arg = option_end;
5609
5610 return ret;
5611}
5612
5613/*
5614 * Allocate a variable for a string constant.
5615 * Return OK or FAIL.
5616 */
5617 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 int evaluate;
5622{
5623 char_u *p;
5624 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int extra = 0;
5626
5627 /*
5628 * Find the end of the string, skipping backslashed characters.
5629 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005630 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 if (*p == '\\' && p[1] != NUL)
5633 {
5634 ++p;
5635 /* A "\<x>" form occupies at least 4 characters, and produces up
5636 * to 6 characters: reserve space for 2 extra */
5637 if (*p == '<')
5638 extra += 2;
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641
5642 if (*p != '"')
5643 {
5644 EMSG2(_("E114: Missing quote: %s"), *arg);
5645 return FAIL;
5646 }
5647
5648 /* If only parsing, set *arg and return here */
5649 if (!evaluate)
5650 {
5651 *arg = p + 1;
5652 return OK;
5653 }
5654
5655 /*
5656 * Copy the string into allocated memory, handling backslashed
5657 * characters.
5658 */
5659 name = alloc((unsigned)(p - *arg + extra));
5660 if (name == NULL)
5661 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 rettv->v_type = VAR_STRING;
5663 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 if (*p == '\\')
5668 {
5669 switch (*++p)
5670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 case 'b': *name++ = BS; ++p; break;
5672 case 'e': *name++ = ESC; ++p; break;
5673 case 'f': *name++ = FF; ++p; break;
5674 case 'n': *name++ = NL; ++p; break;
5675 case 'r': *name++ = CAR; ++p; break;
5676 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 case 'X': /* hex: "\x1", "\x12" */
5679 case 'x':
5680 case 'u': /* Unicode: "\u0023" */
5681 case 'U':
5682 if (vim_isxdigit(p[1]))
5683 {
5684 int n, nr;
5685 int c = toupper(*p);
5686
5687 if (c == 'X')
5688 n = 2;
5689 else
5690 n = 4;
5691 nr = 0;
5692 while (--n >= 0 && vim_isxdigit(p[1]))
5693 {
5694 ++p;
5695 nr = (nr << 4) + hex2nr(*p);
5696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698#ifdef FEAT_MBYTE
5699 /* For "\u" store the number according to
5700 * 'encoding'. */
5701 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else
5704#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* octal: "\1", "\12", "\123" */
5710 case '0':
5711 case '1':
5712 case '2':
5713 case '3':
5714 case '4':
5715 case '5':
5716 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 case '7': *name = *p++ - '0';
5718 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 *name = (*name << 3) + *p++ - '0';
5721 if (*p >= '0' && *p <= '7')
5722 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 break;
5726
5727 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (extra != 0)
5730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 break;
5733 }
5734 /* FALLTHROUGH */
5735
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 break;
5738 }
5739 }
5740 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 *arg = p + 1;
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 return OK;
5748}
5749
5750/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 * Return OK or FAIL.
5753 */
5754 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 int evaluate;
5759{
5760 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 int reduce = 0;
5763
5764 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 */
5767 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 break;
5773 ++reduce;
5774 ++p;
5775 }
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 return FAIL;
5782 }
5783
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 if (!evaluate)
5786 {
5787 *arg = p + 1;
5788 return OK;
5789 }
5790
5791 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 */
5794 str = alloc((unsigned)((p - *arg) - reduce));
5795 if (str == NULL)
5796 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 rettv->v_type = VAR_STRING;
5798 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 break;
5806 ++p;
5807 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 *arg = p + 1;
5812
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 return OK;
5814}
5815
5816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 * Allocate a variable for a List and fill it from "*arg".
5818 * Return OK or FAIL.
5819 */
5820 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005821get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005823 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 int evaluate;
5825{
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 list_T *l = NULL;
5827 typval_T tv;
5828 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829
5830 if (evaluate)
5831 {
5832 l = list_alloc();
5833 if (l == NULL)
5834 return FAIL;
5835 }
5836
5837 *arg = skipwhite(*arg + 1);
5838 while (**arg != ']' && **arg != NUL)
5839 {
5840 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5841 goto failret;
5842 if (evaluate)
5843 {
5844 item = listitem_alloc();
5845 if (item != NULL)
5846 {
5847 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005848 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 list_append(l, item);
5850 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 else
5852 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 }
5854
5855 if (**arg == ']')
5856 break;
5857 if (**arg != ',')
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 goto failret;
5861 }
5862 *arg = skipwhite(*arg + 1);
5863 }
5864
5865 if (**arg != ']')
5866 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868failret:
5869 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005870 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 return FAIL;
5872 }
5873
5874 *arg = skipwhite(*arg + 1);
5875 if (evaluate)
5876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005877 rettv->v_type = VAR_LIST;
5878 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 ++l->lv_refcount;
5880 }
5881
5882 return OK;
5883}
5884
5885/*
5886 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005887 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005889 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890list_alloc()
5891{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 list_T *l;
5893
5894 l = (list_T *)alloc_clear(sizeof(list_T));
5895 if (l != NULL)
5896 {
5897 /* Prepend the list to the list of lists for garbage collection. */
5898 if (first_list != NULL)
5899 first_list->lv_used_prev = l;
5900 l->lv_used_prev = NULL;
5901 l->lv_used_next = first_list;
5902 first_list = l;
5903 }
5904 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905}
5906
5907/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005908 * Allocate an empty list for a return value.
5909 * Returns OK or FAIL.
5910 */
5911 static int
5912rettv_list_alloc(rettv)
5913 typval_T *rettv;
5914{
5915 list_T *l = list_alloc();
5916
5917 if (l == NULL)
5918 return FAIL;
5919
5920 rettv->vval.v_list = l;
5921 rettv->v_type = VAR_LIST;
5922 ++l->lv_refcount;
5923 return OK;
5924}
5925
5926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 * Unreference a list: decrement the reference count and free it when it
5928 * becomes zero.
5929 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005930 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 if (l != NULL && --l->lv_refcount <= 0)
5935 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936}
5937
5938/*
5939 * Free a list, including all items it points to.
5940 * Ignores the reference count.
5941 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005942 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943list_free(l, recurse)
5944 list_T *l;
5945 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 /* Remove the list from the list of lists for garbage collection. */
5950 if (l->lv_used_prev == NULL)
5951 first_list = l->lv_used_next;
5952 else
5953 l->lv_used_prev->lv_used_next = l->lv_used_next;
5954 if (l->lv_used_next != NULL)
5955 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5956
Bram Moolenaard9fba312005-06-26 22:34:35 +00005957 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005959 /* Remove the item before deleting it. */
5960 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005961 if (recurse || (item->li_tv.v_type != VAR_LIST
5962 && item->li_tv.v_type != VAR_DICT))
5963 clear_tv(&item->li_tv);
5964 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966 vim_free(l);
5967}
5968
5969/*
5970 * Allocate a list item.
5971 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005972 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973listitem_alloc()
5974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976}
5977
5978/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005979 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005985 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 vim_free(item);
5987}
5988
5989/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990 * Remove a list item from a List and free it. Also clears the value.
5991 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005992 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005993listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 list_T *l;
5995 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005996{
5997 list_remove(l, item, item);
5998 listitem_free(item);
5999}
6000
6001/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 * Get the number of items in a list.
6003 */
6004 static long
6005list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 if (l == NULL)
6009 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011}
6012
6013/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 * Return TRUE when two lists have exactly the same values.
6015 */
6016 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006018 list_T *l1;
6019 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006022{
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006025 if (l1 == NULL || l2 == NULL)
6026 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006027 if (l1 == l2)
6028 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 if (list_len(l1) != list_len(l2))
6030 return FALSE;
6031
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006032 for (item1 = l1->lv_first, item2 = l2->lv_first;
6033 item1 != NULL && item2 != NULL;
6034 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 return FALSE;
6037 return item1 == NULL && item2 == NULL;
6038}
6039
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006040#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6041 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006042/*
6043 * Return the dictitem that an entry in a hashtable points to.
6044 */
6045 dictitem_T *
6046dict_lookup(hi)
6047 hashitem_T *hi;
6048{
6049 return HI2DI(hi);
6050}
6051#endif
6052
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 * Return TRUE when two dictionaries have exactly the same key/values.
6055 */
6056 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 dict_T *d1;
6059 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 hashitem_T *hi;
6064 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006067 if (d1 == NULL || d2 == NULL)
6068 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006069 if (d1 == d2)
6070 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071 if (dict_len(d1) != dict_len(d2))
6072 return FALSE;
6073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006074 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006076 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 if (!HASHITEM_EMPTY(hi))
6078 {
6079 item2 = dict_find(d2, hi->hi_key, -1);
6080 if (item2 == NULL)
6081 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 return FALSE;
6084 --todo;
6085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086 }
6087 return TRUE;
6088}
6089
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090static int tv_equal_recurse_limit;
6091
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006094 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006095 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 */
6097 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 typval_T *tv1;
6100 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 int ic; /* ignore case */
6102 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
6104 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006105 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 * recursiveness to a limit. We guess they are equal then.
6114 * A fixed limit has the problem of still taking an awful long time.
6115 * Reduce the limit every time running into it. That should work fine for
6116 * deeply linked structures that are not recursively linked and catch
6117 * recursiveness quickly. */
6118 if (!recursive)
6119 tv_equal_recurse_limit = 1000;
6120 if (recursive_cnt >= tv_equal_recurse_limit)
6121 {
6122 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006123 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006125
6126 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135 ++recursive_cnt;
6136 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6137 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006138 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006139
6140 case VAR_FUNC:
6141 return (tv1->vval.v_string != NULL
6142 && tv2->vval.v_string != NULL
6143 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6144
6145 case VAR_NUMBER:
6146 return tv1->vval.v_number == tv2->vval.v_number;
6147
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006148#ifdef FEAT_FLOAT
6149 case VAR_FLOAT:
6150 return tv1->vval.v_float == tv2->vval.v_float;
6151#endif
6152
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006153 case VAR_STRING:
6154 s1 = get_tv_string_buf(tv1, buf1);
6155 s2 = get_tv_string_buf(tv2, buf2);
6156 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006157 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006158
6159 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006160 return TRUE;
6161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Locate item with index "n" in list "l" and return it.
6165 * A negative index is counted from the end; -1 is the last item.
6166 * Returns NULL when "n" is out of range.
6167 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006168 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 long n;
6172{
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 long idx;
6175
6176 if (l == NULL)
6177 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006178
6179 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006181 n = l->lv_len + n;
6182
6183 /* Check for index out of range. */
6184 if (n < 0 || n >= l->lv_len)
6185 return NULL;
6186
6187 /* When there is a cached index may start search from there. */
6188 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006190 if (n < l->lv_idx / 2)
6191 {
6192 /* closest to the start of the list */
6193 item = l->lv_first;
6194 idx = 0;
6195 }
6196 else if (n > (l->lv_idx + l->lv_len) / 2)
6197 {
6198 /* closest to the end of the list */
6199 item = l->lv_last;
6200 idx = l->lv_len - 1;
6201 }
6202 else
6203 {
6204 /* closest to the cached index */
6205 item = l->lv_idx_item;
6206 idx = l->lv_idx;
6207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 }
6209 else
6210 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006211 if (n < l->lv_len / 2)
6212 {
6213 /* closest to the start of the list */
6214 item = l->lv_first;
6215 idx = 0;
6216 }
6217 else
6218 {
6219 /* closest to the end of the list */
6220 item = l->lv_last;
6221 idx = l->lv_len - 1;
6222 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006223 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006224
6225 while (n > idx)
6226 {
6227 /* search forward */
6228 item = item->li_next;
6229 ++idx;
6230 }
6231 while (n < idx)
6232 {
6233 /* search backward */
6234 item = item->li_prev;
6235 --idx;
6236 }
6237
6238 /* cache the used index */
6239 l->lv_idx = idx;
6240 l->lv_idx_item = item;
6241
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 return item;
6243}
6244
6245/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006246 * Get list item "l[idx]" as a number.
6247 */
6248 static long
6249list_find_nr(l, idx, errorp)
6250 list_T *l;
6251 long idx;
6252 int *errorp; /* set to TRUE when something wrong */
6253{
6254 listitem_T *li;
6255
6256 li = list_find(l, idx);
6257 if (li == NULL)
6258 {
6259 if (errorp != NULL)
6260 *errorp = TRUE;
6261 return -1L;
6262 }
6263 return get_tv_number_chk(&li->li_tv, errorp);
6264}
6265
6266/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006267 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6268 */
6269 char_u *
6270list_find_str(l, idx)
6271 list_T *l;
6272 long idx;
6273{
6274 listitem_T *li;
6275
6276 li = list_find(l, idx - 1);
6277 if (li == NULL)
6278 {
6279 EMSGN(_(e_listidx), idx);
6280 return NULL;
6281 }
6282 return get_tv_string(&li->li_tv);
6283}
6284
6285/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006286 * Locate "item" list "l" and return its index.
6287 * Returns -1 when "item" is not in the list.
6288 */
6289 static long
6290list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006293{
6294 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006296
6297 if (l == NULL)
6298 return -1;
6299 idx = 0;
6300 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6301 ++idx;
6302 if (li == NULL)
6303 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006304 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006305}
6306
6307/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 * Append item "item" to the end of list "l".
6309 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006310 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 list_T *l;
6313 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314{
6315 if (l->lv_last == NULL)
6316 {
6317 /* empty list */
6318 l->lv_first = item;
6319 l->lv_last = item;
6320 item->li_prev = NULL;
6321 }
6322 else
6323 {
6324 l->lv_last->li_next = item;
6325 item->li_prev = l->lv_last;
6326 l->lv_last = item;
6327 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006328 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 item->li_next = NULL;
6330}
6331
6332/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006336 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 list_T *l;
6339 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006341 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342
Bram Moolenaar05159a02005-02-26 23:04:13 +00006343 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 copy_tv(tv, &li->li_tv);
6346 list_append(l, li);
6347 return OK;
6348}
6349
6350/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006351 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 * Return FAIL when out of memory.
6353 */
6354 int
6355list_append_dict(list, dict)
6356 list_T *list;
6357 dict_T *dict;
6358{
6359 listitem_T *li = listitem_alloc();
6360
6361 if (li == NULL)
6362 return FAIL;
6363 li->li_tv.v_type = VAR_DICT;
6364 li->li_tv.v_lock = 0;
6365 li->li_tv.vval.v_dict = dict;
6366 list_append(list, li);
6367 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 return OK;
6369}
6370
6371/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006374 * Returns FAIL when out of memory.
6375 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006376 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 list_T *l;
6379 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381{
6382 listitem_T *li = listitem_alloc();
6383
6384 if (li == NULL)
6385 return FAIL;
6386 list_append(l, li);
6387 li->li_tv.v_type = VAR_STRING;
6388 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006389 if (str == NULL)
6390 li->li_tv.vval.v_string = NULL;
6391 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006392 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006393 return FAIL;
6394 return OK;
6395}
6396
6397/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006398 * Append "n" to list "l".
6399 * Returns FAIL when out of memory.
6400 */
6401 static int
6402list_append_number(l, n)
6403 list_T *l;
6404 varnumber_T n;
6405{
6406 listitem_T *li;
6407
6408 li = listitem_alloc();
6409 if (li == NULL)
6410 return FAIL;
6411 li->li_tv.v_type = VAR_NUMBER;
6412 li->li_tv.v_lock = 0;
6413 li->li_tv.vval.v_number = n;
6414 list_append(l, li);
6415 return OK;
6416}
6417
6418/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 * If "item" is NULL append at the end.
6421 * Return FAIL when out of memory.
6422 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006423 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 list_T *l;
6426 typval_T *tv;
6427 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428{
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430
6431 if (ni == NULL)
6432 return FAIL;
6433 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006434 list_insert(l, ni, item);
6435 return OK;
6436}
6437
6438 void
6439list_insert(l, ni, item)
6440 list_T *l;
6441 listitem_T *ni;
6442 listitem_T *item;
6443{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444 if (item == NULL)
6445 /* Append new item at end of list. */
6446 list_append(l, ni);
6447 else
6448 {
6449 /* Insert new item before existing item. */
6450 ni->li_prev = item->li_prev;
6451 ni->li_next = item;
6452 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455 ++l->lv_idx;
6456 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006460 l->lv_idx_item = NULL;
6461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465}
6466
6467/*
6468 * Extend "l1" with "l2".
6469 * If "bef" is NULL append at the end, otherwise insert before this item.
6470 * Returns FAIL when out of memory.
6471 */
6472 static int
6473list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 list_T *l1;
6475 list_T *l2;
6476 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006479 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006481 /* We also quit the loop when we have inserted the original item count of
6482 * the list, avoid a hang when we extend a list with itself. */
6483 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6485 return FAIL;
6486 return OK;
6487}
6488
6489/*
6490 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6491 * Return FAIL when out of memory.
6492 */
6493 static int
6494list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *l1;
6496 list_T *l2;
6497 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498{
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006501 if (l1 == NULL || l2 == NULL)
6502 return FAIL;
6503
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 if (l == NULL)
6507 return FAIL;
6508 tv->v_type = VAR_LIST;
6509 tv->vval.v_list = l;
6510
6511 /* append all items from the second list */
6512 return list_extend(l, l2, NULL);
6513}
6514
6515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 * Returns NULL when out of memory.
6520 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526{
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *copy;
6528 listitem_T *item;
6529 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530
6531 if (orig == NULL)
6532 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533
6534 copy = list_alloc();
6535 if (copy != NULL)
6536 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 if (copyID != 0)
6538 {
6539 /* Do this before adding the items, because one of the items may
6540 * refer back to this list. */
6541 orig->lv_copyID = copyID;
6542 orig->lv_copylist = copy;
6543 }
6544 for (item = orig->lv_first; item != NULL && !got_int;
6545 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 {
6547 ni = listitem_alloc();
6548 if (ni == NULL)
6549 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006551 {
6552 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6553 {
6554 vim_free(ni);
6555 break;
6556 }
6557 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006559 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 list_append(copy, ni);
6561 }
6562 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 if (item != NULL)
6564 {
6565 list_unref(copy);
6566 copy = NULL;
6567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 }
6569
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 return copy;
6571}
6572
6573/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006575 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006577 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *l;
6580 listitem_T *item;
6581 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 /* notify watchers */
6586 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006588 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 list_fix_watch(l, ip);
6590 if (ip == item2)
6591 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593
6594 if (item2->li_next == NULL)
6595 l->lv_last = item->li_prev;
6596 else
6597 item2->li_next->li_prev = item->li_prev;
6598 if (item->li_prev == NULL)
6599 l->lv_first = item2->li_next;
6600 else
6601 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006602 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603}
6604
6605/*
6606 * Return an allocated string with the string representation of a list.
6607 * May return NULL.
6608 */
6609 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006610list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006612 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613{
6614 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
6616 if (tv->vval.v_list == NULL)
6617 return NULL;
6618 ga_init2(&ga, (int)sizeof(char), 80);
6619 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006620 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 {
6622 vim_free(ga.ga_data);
6623 return NULL;
6624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 ga_append(&ga, ']');
6626 ga_append(&ga, NUL);
6627 return (char_u *)ga.ga_data;
6628}
6629
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006630typedef struct join_S {
6631 char_u *s;
6632 char_u *tofree;
6633} join_T;
6634
6635 static int
6636list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6637 garray_T *gap; /* to store the result in */
6638 list_T *l;
6639 char_u *sep;
6640 int echo_style;
6641 int copyID;
6642 garray_T *join_gap; /* to keep each list item string */
6643{
6644 int i;
6645 join_T *p;
6646 int len;
6647 int sumlen = 0;
6648 int first = TRUE;
6649 char_u *tofree;
6650 char_u numbuf[NUMBUFLEN];
6651 listitem_T *item;
6652 char_u *s;
6653
6654 /* Stringify each item in the list. */
6655 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6656 {
6657 if (echo_style)
6658 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6659 else
6660 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6661 if (s == NULL)
6662 return FAIL;
6663
6664 len = (int)STRLEN(s);
6665 sumlen += len;
6666
6667 ga_grow(join_gap, 1);
6668 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6669 if (tofree != NULL || s != numbuf)
6670 {
6671 p->s = s;
6672 p->tofree = tofree;
6673 }
6674 else
6675 {
6676 p->s = vim_strnsave(s, len);
6677 p->tofree = p->s;
6678 }
6679
6680 line_breakcheck();
6681 }
6682
6683 /* Allocate result buffer with its total size, avoid re-allocation and
6684 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6685 if (join_gap->ga_len >= 2)
6686 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6687 if (ga_grow(gap, sumlen + 2) == FAIL)
6688 return FAIL;
6689
6690 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6691 {
6692 if (first)
6693 first = FALSE;
6694 else
6695 ga_concat(gap, sep);
6696 p = ((join_T *)join_gap->ga_data) + i;
6697
6698 if (p->s != NULL)
6699 ga_concat(gap, p->s);
6700 line_breakcheck();
6701 }
6702
6703 return OK;
6704}
6705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006708 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006712list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006716 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 garray_T join_ga;
6720 int retval;
6721 join_T *p;
6722 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6725 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6726
6727 /* Dispose each item in join_ga. */
6728 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 p = (join_T *)join_ga.ga_data;
6731 for (i = 0; i < join_ga.ga_len; ++i)
6732 {
6733 vim_free(p->tofree);
6734 ++p;
6735 }
6736 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006737 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738
6739 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740}
6741
6742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 * Garbage collection for lists and dictionaries.
6744 *
6745 * We use reference counts to be able to free most items right away when they
6746 * are no longer used. But for composite items it's possible that it becomes
6747 * unused while the reference count is > 0: When there is a recursive
6748 * reference. Example:
6749 * :let l = [1, 2, 3]
6750 * :let d = {9: l}
6751 * :let l[1] = d
6752 *
6753 * Since this is quite unusual we handle this with garbage collection: every
6754 * once in a while find out which lists and dicts are not referenced from any
6755 * variable.
6756 *
6757 * Here is a good reference text about garbage collection (refers to Python
6758 * but it applies to all reference-counting mechanisms):
6759 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761
6762/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 * Do garbage collection for lists and dicts.
6764 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 int
6767garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006769 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 buf_T *buf;
6771 win_T *wp;
6772 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006773 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774 int did_free;
6775 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006776#ifdef FEAT_WINDOWS
6777 tabpage_T *tp;
6778#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006780 /* Only do this once. */
6781 want_garbage_collect = FALSE;
6782 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006783 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006784
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006785 /* We advance by two because we add one for items referenced through
6786 * previous_funccal. */
6787 current_copyID += COPYID_INC;
6788 copyID = current_copyID;
6789
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 /*
6791 * 1. Go through all accessible variables and mark all lists and dicts
6792 * with copyID.
6793 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006794
6795 /* Don't free variables in the previous_funccal list unless they are only
6796 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006797 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6799 {
6800 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6801 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6802 }
6803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 /* script-local variables */
6805 for (i = 1; i <= ga_scripts.ga_len; ++i)
6806 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6807
6808 /* buffer-local variables */
6809 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006810 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811
6812 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006813 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006814 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006815#ifdef FEAT_AUTOCMD
6816 if (aucmd_win != NULL)
6817 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6818#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820#ifdef FEAT_WINDOWS
6821 /* tabpage-local variables */
6822 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006823 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824#endif
6825
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 /* global variables */
6827 set_ref_in_ht(&globvarht, copyID);
6828
6829 /* function-local variables */
6830 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6831 {
6832 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6833 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6834 }
6835
Bram Moolenaard812df62008-11-09 12:46:09 +00006836 /* v: vars */
6837 set_ref_in_ht(&vimvarht, copyID);
6838
Bram Moolenaar1dced572012-04-05 16:54:08 +02006839#ifdef FEAT_LUA
6840 set_ref_in_lua(copyID);
6841#endif
6842
Bram Moolenaardb913952012-06-29 12:54:53 +02006843#ifdef FEAT_PYTHON
6844 set_ref_in_python(copyID);
6845#endif
6846
6847#ifdef FEAT_PYTHON3
6848 set_ref_in_python3(copyID);
6849#endif
6850
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006851 /*
6852 * 2. Free lists and dictionaries that are not referenced.
6853 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 did_free = free_unref_items(copyID);
6855
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006856 /*
6857 * 3. Check if any funccal can be freed now.
6858 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 for (pfc = &previous_funccal; *pfc != NULL; )
6860 {
6861 if (can_free_funccal(*pfc, copyID))
6862 {
6863 fc = *pfc;
6864 *pfc = fc->caller;
6865 free_funccal(fc, TRUE);
6866 did_free = TRUE;
6867 did_free_funccal = TRUE;
6868 }
6869 else
6870 pfc = &(*pfc)->caller;
6871 }
6872 if (did_free_funccal)
6873 /* When a funccal was freed some more items might be garbage
6874 * collected, so run again. */
6875 (void)garbage_collect();
6876
6877 return did_free;
6878}
6879
6880/*
6881 * Free lists and dictionaries that are no longer referenced.
6882 */
6883 static int
6884free_unref_items(copyID)
6885 int copyID;
6886{
6887 dict_T *dd;
6888 list_T *ll;
6889 int did_free = FALSE;
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006892 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 */
6894 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006897 /* Free the Dictionary and ordinary items it contains, but don't
6898 * recurse into Lists and Dictionaries, they will be in the list
6899 * of dicts or list of lists. */
6900 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 did_free = TRUE;
6902
6903 /* restart, next dict may also have been freed */
6904 dd = first_dict;
6905 }
6906 else
6907 dd = dd->dv_used_next;
6908
6909 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 * Go through the list of lists and free items without the copyID.
6911 * But don't free a list that has a watcher (used in a for loop), these
6912 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 */
6914 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6916 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006918 /* Free the List and ordinary items it contains, but don't recurse
6919 * into Lists and Dictionaries, they will be in the list of dicts
6920 * or list of lists. */
6921 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 did_free = TRUE;
6923
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006924 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 ll = first_list;
6926 }
6927 else
6928 ll = ll->lv_used_next;
6929
6930 return did_free;
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_ht(ht, copyID)
6938 hashtab_T *ht;
6939 int copyID;
6940{
6941 int todo;
6942 hashitem_T *hi;
6943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006944 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 for (hi = ht->ht_array; todo > 0; ++hi)
6946 if (!HASHITEM_EMPTY(hi))
6947 {
6948 --todo;
6949 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6950 }
6951}
6952
6953/*
6954 * Mark all lists and dicts referenced through list "l" with "copyID".
6955 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006956 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957set_ref_in_list(l, copyID)
6958 list_T *l;
6959 int copyID;
6960{
6961 listitem_T *li;
6962
6963 for (li = l->lv_first; li != NULL; li = li->li_next)
6964 set_ref_in_item(&li->li_tv, copyID);
6965}
6966
6967/*
6968 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6969 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006970 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971set_ref_in_item(tv, copyID)
6972 typval_T *tv;
6973 int copyID;
6974{
6975 dict_T *dd;
6976 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977
6978 switch (tv->v_type)
6979 {
6980 case VAR_DICT:
6981 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006982 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /* Didn't see this dict yet. */
6985 dd->dv_copyID = copyID;
6986 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989
6990 case VAR_LIST:
6991 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006992 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 /* Didn't see this list yet. */
6995 ll->lv_copyID = copyID;
6996 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001}
7002
7003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 * Allocate an empty header for a dictionary.
7005 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007006 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007007dict_alloc()
7008{
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007010
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007013 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007014 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 if (first_dict != NULL)
7016 first_dict->dv_used_prev = d;
7017 d->dv_used_next = first_dict;
7018 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007019 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007022 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007023 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007024 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007025 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028}
7029
7030/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007031 * Allocate an empty dict for a return value.
7032 * Returns OK or FAIL.
7033 */
7034 static int
7035rettv_dict_alloc(rettv)
7036 typval_T *rettv;
7037{
7038 dict_T *d = dict_alloc();
7039
7040 if (d == NULL)
7041 return FAIL;
7042
7043 rettv->vval.v_dict = d;
7044 rettv->v_type = VAR_DICT;
7045 ++d->dv_refcount;
7046 return OK;
7047}
7048
7049
7050/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051 * Unreference a Dictionary: decrement the reference count and free it when it
7052 * becomes zero.
7053 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007054 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007058 if (d != NULL && --d->dv_refcount <= 0)
7059 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060}
7061
7062/*
7063 * Free a Dictionary, including all items it contains.
7064 * Ignores the reference count.
7065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007066 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067dict_free(d, recurse)
7068 dict_T *d;
7069 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007073 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 /* Remove the dict from the list of dicts for garbage collection. */
7076 if (d->dv_used_prev == NULL)
7077 first_dict = d->dv_used_next;
7078 else
7079 d->dv_used_prev->dv_used_next = d->dv_used_next;
7080 if (d->dv_used_next != NULL)
7081 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7082
7083 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007085 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 if (!HASHITEM_EMPTY(hi))
7089 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007090 /* Remove the item before deleting it, just in case there is
7091 * something recursive causing trouble. */
7092 di = HI2DI(hi);
7093 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007094 if (recurse || (di->di_tv.v_type != VAR_LIST
7095 && di->di_tv.v_type != VAR_DICT))
7096 clear_tv(&di->di_tv);
7097 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007098 --todo;
7099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 vim_free(d);
7103}
7104
7105/*
7106 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107 * The "key" is copied to the new item.
7108 * Note that the value of the item "di_tv" still needs to be initialized!
7109 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007111 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112dictitem_alloc(key)
7113 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114{
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007117 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007119 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007121 di->di_flags = 0;
7122 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124}
7125
7126/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007127 * Make a copy of a Dictionary item.
7128 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132{
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007135 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7136 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137 if (di != NULL)
7138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007139 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007140 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 copy_tv(&org->di_tv, &di->di_tv);
7142 }
7143 return di;
7144}
7145
7146/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147 * Remove item "item" from Dictionary "dict" and free it.
7148 */
7149 static void
7150dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 dict_T *dict;
7152 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153{
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (HASHITEM_EMPTY(hi))
7158 EMSG2(_(e_intern2), "dictitem_remove()");
7159 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 dictitem_free(item);
7162}
7163
7164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 * Free a dict item. Also clears the value.
7166 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007167 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 clear_tv(&item->di_tv);
7172 vim_free(item);
7173}
7174
7175/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7177 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 * Returns NULL when out of memory.
7180 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *copy;
7188 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191
7192 if (orig == NULL)
7193 return NULL;
7194
7195 copy = dict_alloc();
7196 if (copy != NULL)
7197 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 if (copyID != 0)
7199 {
7200 orig->dv_copyID = copyID;
7201 orig->dv_copydict = copy;
7202 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007203 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 --todo;
7209
7210 di = dictitem_alloc(hi->hi_key);
7211 if (di == NULL)
7212 break;
7213 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 {
7215 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7216 copyID) == FAIL)
7217 {
7218 vim_free(di);
7219 break;
7220 }
7221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 else
7223 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7224 if (dict_add(copy, di) == FAIL)
7225 {
7226 dictitem_free(di);
7227 break;
7228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007233 if (todo > 0)
7234 {
7235 dict_unref(copy);
7236 copy = NULL;
7237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 }
7239
7240 return copy;
7241}
7242
7243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007245 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007247 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 dict_T *d;
7250 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253}
7254
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007256 * Add a number or string entry to dictionary "d".
7257 * When "str" is NULL use number "nr", otherwise use "str".
7258 * Returns FAIL when out of memory and when key already exists.
7259 */
7260 int
7261dict_add_nr_str(d, key, nr, str)
7262 dict_T *d;
7263 char *key;
7264 long nr;
7265 char_u *str;
7266{
7267 dictitem_T *item;
7268
7269 item = dictitem_alloc((char_u *)key);
7270 if (item == NULL)
7271 return FAIL;
7272 item->di_tv.v_lock = 0;
7273 if (str == NULL)
7274 {
7275 item->di_tv.v_type = VAR_NUMBER;
7276 item->di_tv.vval.v_number = nr;
7277 }
7278 else
7279 {
7280 item->di_tv.v_type = VAR_STRING;
7281 item->di_tv.vval.v_string = vim_strsave(str);
7282 }
7283 if (dict_add(d, item) == FAIL)
7284 {
7285 dictitem_free(item);
7286 return FAIL;
7287 }
7288 return OK;
7289}
7290
7291/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007292 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007293 * Returns FAIL when out of memory and when key already exists.
7294 */
7295 int
7296dict_add_list(d, key, list)
7297 dict_T *d;
7298 char *key;
7299 list_T *list;
7300{
7301 dictitem_T *item;
7302
7303 item = dictitem_alloc((char_u *)key);
7304 if (item == NULL)
7305 return FAIL;
7306 item->di_tv.v_lock = 0;
7307 item->di_tv.v_type = VAR_LIST;
7308 item->di_tv.vval.v_list = list;
7309 if (dict_add(d, item) == FAIL)
7310 {
7311 dictitem_free(item);
7312 return FAIL;
7313 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007314 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007315 return OK;
7316}
7317
7318/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 * Get the number of items in a Dictionary.
7320 */
7321 static long
7322dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 if (d == NULL)
7326 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007327 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Find item "key[len]" in Dictionary "d".
7332 * If "len" is negative use strlen(key).
7333 * Returns NULL when not found.
7334 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007335 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 char_u *key;
7339 int len;
7340{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341#define AKEYLEN 200
7342 char_u buf[AKEYLEN];
7343 char_u *akey;
7344 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (len < 0)
7348 akey = key;
7349 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 tofree = akey = vim_strnsave(key, len);
7352 if (akey == NULL)
7353 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007354 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 else
7356 {
7357 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007358 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 akey = buf;
7360 }
7361
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 vim_free(tofree);
7364 if (HASHITEM_EMPTY(hi))
7365 return NULL;
7366 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367}
7368
7369/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007370 * Get a string item from a dictionary.
7371 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007372 * Returns NULL if the entry doesn't exist or out of memory.
7373 */
7374 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376 dict_T *d;
7377 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007378 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007379{
7380 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007382
7383 di = dict_find(d, key, -1);
7384 if (di == NULL)
7385 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007386 s = get_tv_string(&di->di_tv);
7387 if (save && s != NULL)
7388 s = vim_strsave(s);
7389 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007390}
7391
7392/*
7393 * Get a number item from a dictionary.
7394 * Returns 0 if the entry doesn't exist or out of memory.
7395 */
7396 long
7397get_dict_number(d, key)
7398 dict_T *d;
7399 char_u *key;
7400{
7401 dictitem_T *di;
7402
7403 di = dict_find(d, key, -1);
7404 if (di == NULL)
7405 return 0;
7406 return get_tv_number(&di->di_tv);
7407}
7408
7409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 * Return an allocated string with the string representation of a Dictionary.
7411 * May return NULL.
7412 */
7413 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007414dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007416 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417{
7418 garray_T ga;
7419 int first = TRUE;
7420 char_u *tofree;
7421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 return NULL;
7429 ga_init2(&ga, (int)sizeof(char), 80);
7430 ga_append(&ga, '{');
7431
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007432 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 --todo;
7438
7439 if (first)
7440 first = FALSE;
7441 else
7442 ga_concat(&ga, (char_u *)", ");
7443
7444 tofree = string_quote(hi->hi_key, FALSE);
7445 if (tofree != NULL)
7446 {
7447 ga_concat(&ga, tofree);
7448 vim_free(tofree);
7449 }
7450 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 if (s != NULL)
7453 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 if (s == NULL)
7456 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 if (todo > 0)
7460 {
7461 vim_free(ga.ga_data);
7462 return NULL;
7463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464
7465 ga_append(&ga, '}');
7466 ga_append(&ga, NUL);
7467 return (char_u *)ga.ga_data;
7468}
7469
7470/*
7471 * Allocate a variable for a Dictionary and fill it from "*arg".
7472 * Return OK or FAIL. Returns NOTDONE for {expr}.
7473 */
7474 static int
7475get_dict_tv(arg, rettv, evaluate)
7476 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 int evaluate;
7479{
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 dict_T *d = NULL;
7481 typval_T tvkey;
7482 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007483 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 /*
7489 * First check if it's not a curly-braces thing: {expr}.
7490 * Must do this without evaluating, otherwise a function may be called
7491 * twice. Unfortunately this means we need to call eval1() twice for the
7492 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 if (*start != '}')
7496 {
7497 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7498 return FAIL;
7499 if (*start == '}')
7500 return NOTDONE;
7501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502
7503 if (evaluate)
7504 {
7505 d = dict_alloc();
7506 if (d == NULL)
7507 return FAIL;
7508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 tvkey.v_type = VAR_UNKNOWN;
7510 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
7512 *arg = skipwhite(*arg + 1);
7513 while (**arg != '}' && **arg != NUL)
7514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 goto failret;
7517 if (**arg != ':')
7518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007519 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 goto failret;
7522 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007523 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007525 key = get_tv_string_buf_chk(&tvkey, buf);
7526 if (key == NULL || *key == NUL)
7527 {
7528 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7529 if (key != NULL)
7530 EMSG(_(e_emptykey));
7531 clear_tv(&tvkey);
7532 goto failret;
7533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535
7536 *arg = skipwhite(*arg + 1);
7537 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7538 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007539 if (evaluate)
7540 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 goto failret;
7542 }
7543 if (evaluate)
7544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 if (item != NULL)
7547 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007548 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 clear_tv(&tv);
7551 goto failret;
7552 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 item = dictitem_alloc(key);
7554 clear_tv(&tvkey);
7555 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007558 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 if (dict_add(d, item) == FAIL)
7560 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 }
7562 }
7563
7564 if (**arg == '}')
7565 break;
7566 if (**arg != ',')
7567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007568 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 goto failret;
7570 }
7571 *arg = skipwhite(*arg + 1);
7572 }
7573
7574 if (**arg != '}')
7575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007576 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577failret:
7578 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007579 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 return FAIL;
7581 }
7582
7583 *arg = skipwhite(*arg + 1);
7584 if (evaluate)
7585 {
7586 rettv->v_type = VAR_DICT;
7587 rettv->vval.v_dict = d;
7588 ++d->dv_refcount;
7589 }
7590
7591 return OK;
7592}
7593
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 * Return a string with the string representation of a variable.
7596 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007597 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007598 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007600 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 */
7602 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007606 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 static int recurse = 0;
7610 char_u *r = NULL;
7611
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007614 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 *tofree = NULL;
7616 return NULL;
7617 }
7618 ++recurse;
7619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 switch (tv->v_type)
7621 {
7622 case VAR_FUNC:
7623 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 r = tv->vval.v_string;
7625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 if (tv->vval.v_list == NULL)
7629 {
7630 *tofree = NULL;
7631 r = NULL;
7632 }
7633 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7634 {
7635 *tofree = NULL;
7636 r = (char_u *)"[...]";
7637 }
7638 else
7639 {
7640 tv->vval.v_list->lv_copyID = copyID;
7641 *tofree = list2string(tv, copyID);
7642 r = *tofree;
7643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007645
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007647 if (tv->vval.v_dict == NULL)
7648 {
7649 *tofree = NULL;
7650 r = NULL;
7651 }
7652 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7653 {
7654 *tofree = NULL;
7655 r = (char_u *)"{...}";
7656 }
7657 else
7658 {
7659 tv->vval.v_dict->dv_copyID = copyID;
7660 *tofree = dict2string(tv, copyID);
7661 r = *tofree;
7662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 case VAR_STRING:
7666 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 *tofree = NULL;
7668 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671#ifdef FEAT_FLOAT
7672 case VAR_FLOAT:
7673 *tofree = NULL;
7674 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7675 r = numbuf;
7676 break;
7677#endif
7678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007683
7684 --recurse;
7685 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686}
7687
7688/*
7689 * Return a string with the string representation of a variable.
7690 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7691 * "numbuf" is used for a number.
7692 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007693 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 */
7695 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 char_u **tofree;
7699 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701{
7702 switch (tv->v_type)
7703 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 case VAR_FUNC:
7705 *tofree = string_quote(tv->vval.v_string, TRUE);
7706 return *tofree;
7707 case VAR_STRING:
7708 *tofree = string_quote(tv->vval.v_string, FALSE);
7709 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710#ifdef FEAT_FLOAT
7711 case VAR_FLOAT:
7712 *tofree = NULL;
7713 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7714 return numbuf;
7715#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007723 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724}
7725
7726/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 * Return string "str" in ' quotes, doubling ' characters.
7728 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 */
7731 static char_u *
7732string_quote(str, function)
7733 char_u *str;
7734 int function;
7735{
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007737 char_u *p, *r, *s;
7738
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 len = (function ? 13 : 3);
7740 if (str != NULL)
7741 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007742 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 for (p = str; *p != NUL; mb_ptr_adv(p))
7744 if (*p == '\'')
7745 ++len;
7746 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 s = r = alloc(len);
7748 if (r != NULL)
7749 {
7750 if (function)
7751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 r += 10;
7754 }
7755 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 if (str != NULL)
7758 for (p = str; *p != NUL; )
7759 {
7760 if (*p == '\'')
7761 *r++ = '\'';
7762 MB_COPY_CHAR(p, r);
7763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007765 if (function)
7766 *r++ = ')';
7767 *r++ = NUL;
7768 }
7769 return s;
7770}
7771
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007772#ifdef FEAT_FLOAT
7773/*
7774 * Convert the string "text" to a floating point number.
7775 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7776 * this always uses a decimal point.
7777 * Returns the length of the text that was consumed.
7778 */
7779 static int
7780string2float(text, value)
7781 char_u *text;
7782 float_T *value; /* result stored here */
7783{
7784 char *s = (char *)text;
7785 float_T f;
7786
7787 f = strtod(s, &s);
7788 *value = f;
7789 return (int)((char_u *)s - text);
7790}
7791#endif
7792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 * Get the value of an environment variable.
7795 * "arg" is pointing to the '$'. It is advanced to after the name.
7796 * If the environment variable was not set, silently assume it is empty.
7797 * Always return OK.
7798 */
7799 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 int evaluate;
7804{
7805 char_u *string = NULL;
7806 int len;
7807 int cc;
7808 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007809 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811 ++*arg;
7812 name = *arg;
7813 len = get_env_len(arg);
7814 if (evaluate)
7815 {
7816 if (len != 0)
7817 {
7818 cc = name[len];
7819 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007820 /* first try vim_getenv(), fast for normal environment vars */
7821 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 {
7824 if (!mustfree)
7825 string = vim_strsave(string);
7826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else
7828 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007829 if (mustfree)
7830 vim_free(string);
7831
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 /* next try expanding things like $VIM and ${HOME} */
7833 string = expand_env_save(name - 1);
7834 if (string != NULL && *string == '$')
7835 {
7836 vim_free(string);
7837 string = NULL;
7838 }
7839 }
7840 name[len] = cc;
7841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842 rettv->v_type = VAR_STRING;
7843 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845
7846 return OK;
7847}
7848
7849/*
7850 * Array with names and number of arguments of all internal functions
7851 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7852 */
7853static struct fst
7854{
7855 char *f_name; /* function name */
7856 char f_min_argc; /* minimal number of arguments */
7857 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007859 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860} functions[] =
7861{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862#ifdef FEAT_FLOAT
7863 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007864 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007866 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007867 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"append", 2, 2, f_append},
7869 {"argc", 0, 0, f_argc},
7870 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007871 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007875 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007878 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"bufexists", 1, 1, f_bufexists},
7880 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7881 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7882 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7883 {"buflisted", 1, 1, f_buflisted},
7884 {"bufloaded", 1, 1, f_bufloaded},
7885 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007886 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"bufwinnr", 1, 1, f_bufwinnr},
7888 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007889 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007890 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#ifdef FEAT_FLOAT
7893 {"ceil", 1, 1, f_ceil},
7894#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007895 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007896 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007898 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007900#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007901 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007902 {"complete_add", 1, 1, f_complete_add},
7903 {"complete_check", 0, 0, f_complete_check},
7904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907#ifdef FEAT_FLOAT
7908 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007909 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007913 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007914 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"delete", 1, 1, f_delete},
7916 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007917 {"diff_filler", 1, 1, f_diff_filler},
7918 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007919 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007921 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"eventhandler", 0, 0, f_eventhandler},
7923 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007924 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007926#ifdef FEAT_FLOAT
7927 {"exp", 1, 1, f_exp},
7928#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007929 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007930 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007931 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7933 {"filereadable", 1, 1, f_filereadable},
7934 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007936 {"finddir", 1, 3, f_finddir},
7937 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007938#ifdef FEAT_FLOAT
7939 {"float2nr", 1, 1, f_float2nr},
7940 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007941 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007943 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"fnamemodify", 2, 2, f_fnamemodify},
7945 {"foldclosed", 1, 1, f_foldclosed},
7946 {"foldclosedend", 1, 1, f_foldclosedend},
7947 {"foldlevel", 1, 1, f_foldlevel},
7948 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007949 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007952 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007953 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007954 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007955 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"getchar", 0, 1, f_getchar},
7957 {"getcharmod", 0, 0, f_getcharmod},
7958 {"getcmdline", 0, 0, f_getcmdline},
7959 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007960 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007962 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007963 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"getfsize", 1, 1, f_getfsize},
7965 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007966 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007967 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007968 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007969 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007970 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007971 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007972 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007973 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007975 {"gettabvar", 2, 3, f_gettabvar},
7976 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"getwinposx", 0, 0, f_getwinposx},
7978 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007979 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007980 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007981 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007984 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007985 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7987 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7988 {"histadd", 2, 2, f_histadd},
7989 {"histdel", 1, 2, f_histdel},
7990 {"histget", 1, 2, f_histget},
7991 {"histnr", 1, 1, f_histnr},
7992 {"hlID", 1, 1, f_hlID},
7993 {"hlexists", 1, 1, f_hlexists},
7994 {"hostname", 0, 0, f_hostname},
7995 {"iconv", 3, 3, f_iconv},
7996 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007998 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008000 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"inputrestore", 0, 0, f_inputrestore},
8002 {"inputsave", 0, 0, f_inputsave},
8003 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008005 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008007 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008010 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008012 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"libcall", 3, 3, f_libcall},
8014 {"libcallnr", 3, 3, f_libcallnr},
8015 {"line", 1, 1, f_line},
8016 {"line2byte", 1, 1, f_line2byte},
8017 {"lispindent", 1, 1, f_lispindent},
8018 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008019#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008020 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021 {"log10", 1, 1, f_log10},
8022#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008023#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008024 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008025#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008027 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008028 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008029 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008030 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008031 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008032 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008034 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008035 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008036 {"max", 1, 1, f_max},
8037 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008038#ifdef vim_mkdir
8039 {"mkdir", 1, 3, f_mkdir},
8040#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008041 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008042#ifdef FEAT_MZSCHEME
8043 {"mzeval", 1, 1, f_mzeval},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008046 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008047 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008048 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008049#ifdef FEAT_FLOAT
8050 {"pow", 2, 2, f_pow},
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008053 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008055#ifdef FEAT_PYTHON3
8056 {"py3eval", 1, 1, f_py3eval},
8057#endif
8058#ifdef FEAT_PYTHON
8059 {"pyeval", 1, 1, f_pyeval},
8060#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008062 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008063 {"reltime", 0, 2, f_reltime},
8064 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"remote_expr", 2, 3, f_remote_expr},
8066 {"remote_foreground", 1, 1, f_remote_foreground},
8067 {"remote_peek", 1, 2, f_remote_peek},
8068 {"remote_read", 1, 1, f_remote_read},
8069 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008070 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008072 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008074 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"round", 1, 1, f_round},
8077#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008078 {"screenattr", 2, 2, f_screenattr},
8079 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008080 {"screencol", 0, 0, f_screencol},
8081 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008082 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008083 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008084 {"searchpair", 3, 7, f_searchpair},
8085 {"searchpairpos", 3, 7, f_searchpairpos},
8086 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"server2client", 2, 2, f_server2client},
8088 {"serverlist", 0, 0, f_serverlist},
8089 {"setbufvar", 3, 3, f_setbufvar},
8090 {"setcmdpos", 1, 1, f_setcmdpos},
8091 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008092 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008093 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008094 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008095 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008097 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008098 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008100#ifdef FEAT_CRYPT
8101 {"sha256", 1, 1, f_sha256},
8102#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008103 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008104 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#ifdef FEAT_FLOAT
8107 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008108 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008110 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008111 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008112 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008113 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008114 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"sqrt", 1, 1, f_sqrt},
8117 {"str2float", 1, 1, f_str2float},
8118#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008119 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008120 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008121 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122#ifdef HAVE_STRFTIME
8123 {"strftime", 1, 2, f_strftime},
8124#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"strlen", 1, 1, f_strlen},
8128 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008129 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008131 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008132 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"substitute", 4, 4, f_substitute},
8134 {"synID", 3, 3, f_synID},
8135 {"synIDattr", 2, 3, f_synIDattr},
8136 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008137 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008138 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008139 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008140 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008141 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008142 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008143 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008144 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008145#ifdef FEAT_FLOAT
8146 {"tan", 1, 1, f_tan},
8147 {"tanh", 1, 1, f_tanh},
8148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008150 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"tolower", 1, 1, f_tolower},
8152 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008153 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154#ifdef FEAT_FLOAT
8155 {"trunc", 1, 1, f_trunc},
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008158 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008159 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008160 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008161 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"virtcol", 1, 1, f_virtcol},
8163 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008164 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winbufnr", 1, 1, f_winbufnr},
8166 {"wincol", 0, 0, f_wincol},
8167 {"winheight", 1, 1, f_winheight},
8168 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008169 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008171 {"winrestview", 1, 1, f_winrestview},
8172 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008174 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008175 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176};
8177
8178#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8179
8180/*
8181 * Function given to ExpandGeneric() to obtain the list of internal
8182 * or user defined function names.
8183 */
8184 char_u *
8185get_function_name(xp, idx)
8186 expand_T *xp;
8187 int idx;
8188{
8189 static int intidx = -1;
8190 char_u *name;
8191
8192 if (idx == 0)
8193 intidx = -1;
8194 if (intidx < 0)
8195 {
8196 name = get_user_func_name(xp, idx);
8197 if (name != NULL)
8198 return name;
8199 }
8200 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8201 {
8202 STRCPY(IObuff, functions[intidx].f_name);
8203 STRCAT(IObuff, "(");
8204 if (functions[intidx].f_max_argc == 0)
8205 STRCAT(IObuff, ")");
8206 return IObuff;
8207 }
8208
8209 return NULL;
8210}
8211
8212/*
8213 * Function given to ExpandGeneric() to obtain the list of internal or
8214 * user defined variable or function names.
8215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 char_u *
8217get_expr_name(xp, idx)
8218 expand_T *xp;
8219 int idx;
8220{
8221 static int intidx = -1;
8222 char_u *name;
8223
8224 if (idx == 0)
8225 intidx = -1;
8226 if (intidx < 0)
8227 {
8228 name = get_function_name(xp, idx);
8229 if (name != NULL)
8230 return name;
8231 }
8232 return get_user_var_name(xp, ++intidx);
8233}
8234
8235#endif /* FEAT_CMDL_COMPL */
8236
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008237#if defined(EBCDIC) || defined(PROTO)
8238/*
8239 * Compare struct fst by function name.
8240 */
8241 static int
8242compare_func_name(s1, s2)
8243 const void *s1;
8244 const void *s2;
8245{
8246 struct fst *p1 = (struct fst *)s1;
8247 struct fst *p2 = (struct fst *)s2;
8248
8249 return STRCMP(p1->f_name, p2->f_name);
8250}
8251
8252/*
8253 * Sort the function table by function name.
8254 * The sorting of the table above is ASCII dependant.
8255 * On machines using EBCDIC we have to sort it.
8256 */
8257 static void
8258sortFunctions()
8259{
8260 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8261
8262 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8263}
8264#endif
8265
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267/*
8268 * Find internal function in table above.
8269 * Return index, or -1 if not found
8270 */
8271 static int
8272find_internal_func(name)
8273 char_u *name; /* name of the function */
8274{
8275 int first = 0;
8276 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8277 int cmp;
8278 int x;
8279
8280 /*
8281 * Find the function name in the table. Binary search.
8282 */
8283 while (first <= last)
8284 {
8285 x = first + ((unsigned)(last - first) >> 1);
8286 cmp = STRCMP(name, functions[x].f_name);
8287 if (cmp < 0)
8288 last = x - 1;
8289 else if (cmp > 0)
8290 first = x + 1;
8291 else
8292 return x;
8293 }
8294 return -1;
8295}
8296
8297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8299 * name it contains, otherwise return "name".
8300 */
8301 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008302deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 char_u *name;
8304 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008305 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306{
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 int cc;
8309
8310 cc = name[*lenp];
8311 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008312 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008317 {
8318 *lenp = 0;
8319 return (char_u *)""; /* just in case */
8320 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 }
8324
8325 return name;
8326}
8327
8328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 * Allocate a variable for the result of a function.
8330 * Return OK or FAIL.
8331 */
8332 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8334 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 char_u *name; /* name of the function */
8336 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 char_u **arg; /* argument, pointing to the '(' */
8339 linenr_T firstline; /* first line of range */
8340 linenr_T lastline; /* last line of range */
8341 int *doesrange; /* return: function handled range */
8342 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 char_u *argp;
8346 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008347 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 int argcount = 0; /* number of arguments found */
8349
8350 /*
8351 * Get the arguments.
8352 */
8353 argp = *arg;
8354 while (argcount < MAX_FUNC_ARGS)
8355 {
8356 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8357 if (*argp == ')' || *argp == ',' || *argp == NUL)
8358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8360 {
8361 ret = FAIL;
8362 break;
8363 }
8364 ++argcount;
8365 if (*argp != ',')
8366 break;
8367 }
8368 if (*argp == ')')
8369 ++argp;
8370 else
8371 ret = FAIL;
8372
8373 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 {
8378 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008379 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008381 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 *arg = skipwhite(argp);
8388 return ret;
8389}
8390
8391
8392/*
8393 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008394 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008395 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 */
8397 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008398call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008399 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008404 typval_T *argvars; /* vars for arguments, must have "argcount"
8405 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 linenr_T firstline; /* first line of range */
8407 linenr_T lastline; /* last line of range */
8408 int *doesrange; /* return: function handled range */
8409 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#define ERROR_UNKNOWN 0
8414#define ERROR_TOOMANY 1
8415#define ERROR_TOOFEW 2
8416#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008417#define ERROR_DICT 4
8418#define ERROR_NONE 5
8419#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 int error = ERROR_NONE;
8421 int i;
8422 int llen;
8423 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424#define FLEN_FIXED 40
8425 char_u fname_buf[FLEN_FIXED + 1];
8426 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008427 char_u *name;
8428
8429 /* Make a copy of the name, if it comes from a funcref variable it could
8430 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008431 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008432 if (name == NULL)
8433 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435 /*
8436 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8437 * Change <SNR>123_name() to K_SNR 123_name().
8438 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 llen = eval_fname_script(name);
8441 if (llen > 0)
8442 {
8443 fname_buf[0] = K_SPECIAL;
8444 fname_buf[1] = KS_EXTRA;
8445 fname_buf[2] = (int)KE_SNR;
8446 i = 3;
8447 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8448 {
8449 if (current_SID <= 0)
8450 error = ERROR_SCRIPT;
8451 else
8452 {
8453 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8454 i = (int)STRLEN(fname_buf);
8455 }
8456 }
8457 if (i + STRLEN(name + llen) < FLEN_FIXED)
8458 {
8459 STRCPY(fname_buf + i, name + llen);
8460 fname = fname_buf;
8461 }
8462 else
8463 {
8464 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8465 if (fname == NULL)
8466 error = ERROR_OTHER;
8467 else
8468 {
8469 mch_memmove(fname, fname_buf, (size_t)i);
8470 STRCPY(fname + i, name + llen);
8471 }
8472 }
8473 }
8474 else
8475 fname = name;
8476
8477 *doesrange = FALSE;
8478
8479
8480 /* execute the function if no errors detected and executing */
8481 if (evaluate && error == ERROR_NONE)
8482 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008483 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 error = ERROR_UNKNOWN;
8486
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
8489 /*
8490 * User defined function.
8491 */
8492 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008495 /* Trigger FuncUndefined event, may load the function. */
8496 if (fp == NULL
8497 && apply_autocmds(EVENT_FUNCUNDEFINED,
8498 fname, fname, TRUE, NULL)
8499 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008501 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 fp = find_func(fname);
8503 }
8504#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008505 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008506 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507 {
8508 /* loaded a package, search for the function again */
8509 fp = find_func(fname);
8510 }
8511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (fp != NULL)
8513 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008514 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008518 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008520 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 else
8523 {
8524 /*
8525 * Call the user function.
8526 * Save and restore search patterns, script variables and
8527 * redo buffer.
8528 */
8529 save_search_patterns();
8530 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008531 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8535 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8536 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008537 /* Function was unreferenced while being used, free it
8538 * now. */
8539 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 restoreRedobuff();
8541 restore_search_patterns();
8542 error = ERROR_NONE;
8543 }
8544 }
8545 }
8546 else
8547 {
8548 /*
8549 * Find the function name in the table, call its implementation.
8550 */
8551 i = find_internal_func(fname);
8552 if (i >= 0)
8553 {
8554 if (argcount < functions[i].f_min_argc)
8555 error = ERROR_TOOFEW;
8556 else if (argcount > functions[i].f_max_argc)
8557 error = ERROR_TOOMANY;
8558 else
8559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 error = ERROR_NONE;
8563 }
8564 }
8565 }
8566 /*
8567 * The function call (or "FuncUndefined" autocommand sequence) might
8568 * have been aborted by an error, an interrupt, or an explicitly thrown
8569 * exception that has not been caught so far. This situation can be
8570 * tested for by calling aborting(). For an error in an internal
8571 * function or for the "E132" error in call_user_func(), however, the
8572 * throw point at which the "force_abort" flag (temporarily reset by
8573 * emsg()) is normally updated has not been reached yet. We need to
8574 * update that flag first to make aborting() reliable.
8575 */
8576 update_force_abort();
8577 }
8578 if (error == ERROR_NONE)
8579 ret = OK;
8580
8581 /*
8582 * Report an error unless the argument evaluation or function call has been
8583 * cancelled due to an aborting error, an interrupt, or an exception.
8584 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008585 if (!aborting())
8586 {
8587 switch (error)
8588 {
8589 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008590 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008591 break;
8592 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008594 break;
8595 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008597 name);
8598 break;
8599 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008600 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008601 name);
8602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 name);
8606 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008607 }
8608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (fname != name && fname != fname_buf)
8611 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008612 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 return ret;
8615}
8616
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008617/*
8618 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008619 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008620 */
8621 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008622emsg_funcname(ermsg, name)
8623 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008624 char_u *name;
8625{
8626 char_u *p;
8627
8628 if (*name == K_SPECIAL)
8629 p = concat_str((char_u *)"<SNR>", name + 3);
8630 else
8631 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008632 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008633 if (p != name)
8634 vim_free(p);
8635}
8636
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008637/*
8638 * Return TRUE for a non-zero Number and a non-empty String.
8639 */
8640 static int
8641non_zero_arg(argvars)
8642 typval_T *argvars;
8643{
8644 return ((argvars[0].v_type == VAR_NUMBER
8645 && argvars[0].vval.v_number != 0)
8646 || (argvars[0].v_type == VAR_STRING
8647 && argvars[0].vval.v_string != NULL
8648 && *argvars[0].vval.v_string != NUL));
8649}
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651/*********************************************
8652 * Implementation of the built-in functions
8653 */
8654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008655#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008656static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8657
8658/*
8659 * Get the float value of "argvars[0]" into "f".
8660 * Returns FAIL when the argument is not a Number or Float.
8661 */
8662 static int
8663get_float_arg(argvars, f)
8664 typval_T *argvars;
8665 float_T *f;
8666{
8667 if (argvars[0].v_type == VAR_FLOAT)
8668 {
8669 *f = argvars[0].vval.v_float;
8670 return OK;
8671 }
8672 if (argvars[0].v_type == VAR_NUMBER)
8673 {
8674 *f = (float_T)argvars[0].vval.v_number;
8675 return OK;
8676 }
8677 EMSG(_("E808: Number or Float required"));
8678 return FAIL;
8679}
8680
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008681/*
8682 * "abs(expr)" function
8683 */
8684 static void
8685f_abs(argvars, rettv)
8686 typval_T *argvars;
8687 typval_T *rettv;
8688{
8689 if (argvars[0].v_type == VAR_FLOAT)
8690 {
8691 rettv->v_type = VAR_FLOAT;
8692 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8693 }
8694 else
8695 {
8696 varnumber_T n;
8697 int error = FALSE;
8698
8699 n = get_tv_number_chk(&argvars[0], &error);
8700 if (error)
8701 rettv->vval.v_number = -1;
8702 else if (n > 0)
8703 rettv->vval.v_number = n;
8704 else
8705 rettv->vval.v_number = -n;
8706 }
8707}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008708
8709/*
8710 * "acos()" function
8711 */
8712 static void
8713f_acos(argvars, rettv)
8714 typval_T *argvars;
8715 typval_T *rettv;
8716{
8717 float_T f;
8718
8719 rettv->v_type = VAR_FLOAT;
8720 if (get_float_arg(argvars, &f) == OK)
8721 rettv->vval.v_float = acos(f);
8722 else
8723 rettv->vval.v_float = 0.0;
8724}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008725#endif
8726
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 */
8730 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 typval_T *argvars;
8733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008740 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008741 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008742 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 }
8745 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 EMSG(_(e_listreq));
8747}
8748
8749/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008750 * "and(expr, expr)" function
8751 */
8752 static void
8753f_and(argvars, rettv)
8754 typval_T *argvars;
8755 typval_T *rettv;
8756{
8757 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8758 & get_tv_number_chk(&argvars[1], NULL);
8759}
8760
8761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 * "append(lnum, string/list)" function
8763 */
8764 static void
8765f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 typval_T *argvars;
8767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008768{
8769 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 list_T *l = NULL;
8772 listitem_T *li = NULL;
8773 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008774 long added = 0;
8775
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008776 /* When coming here from Insert mode, sync undo, so that this can be
8777 * undone separately from what was previously inserted. */
8778 if (u_sync_once == 2)
8779 {
8780 u_sync_once = 1; /* notify that u_sync() was called */
8781 u_sync(TRUE);
8782 }
8783
Bram Moolenaar0d660222005-01-07 21:51:51 +00008784 lnum = get_tv_lnum(argvars);
8785 if (lnum >= 0
8786 && lnum <= curbuf->b_ml.ml_line_count
8787 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008789 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008790 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008791 l = argvars[1].vval.v_list;
8792 if (l == NULL)
8793 return;
8794 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796 for (;;)
8797 {
8798 if (l == NULL)
8799 tv = &argvars[1]; /* append a string */
8800 else if (li == NULL)
8801 break; /* end of list */
8802 else
8803 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008804 line = get_tv_string_chk(tv);
8805 if (line == NULL) /* type error */
8806 {
8807 rettv->vval.v_number = 1; /* Failed */
8808 break;
8809 }
8810 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008811 ++added;
8812 if (l == NULL)
8813 break;
8814 li = li->li_next;
8815 }
8816
8817 appended_lines_mark(lnum, added);
8818 if (curwin->w_cursor.lnum > lnum)
8819 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 else
8822 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
8825/*
8826 * "argc()" function
8827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834}
8835
8836/*
8837 * "argidx()" function
8838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008841 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
8846
8847/*
8848 * "argv(nr)" function
8849 */
8850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T *argvars;
8853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 int idx;
8856
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008857 if (argvars[0].v_type != VAR_UNKNOWN)
8858 {
8859 idx = get_tv_number_chk(&argvars[0], NULL);
8860 if (idx >= 0 && idx < ARGCOUNT)
8861 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8862 else
8863 rettv->vval.v_string = NULL;
8864 rettv->v_type = VAR_STRING;
8865 }
8866 else if (rettv_list_alloc(rettv) == OK)
8867 for (idx = 0; idx < ARGCOUNT; ++idx)
8868 list_append_string(rettv->vval.v_list,
8869 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008873/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008874 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008875 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876 static void
8877f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881 float_T f;
8882
8883 rettv->v_type = VAR_FLOAT;
8884 if (get_float_arg(argvars, &f) == OK)
8885 rettv->vval.v_float = asin(f);
8886 else
8887 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008888}
8889
8890/*
8891 * "atan()" function
8892 */
8893 static void
8894f_atan(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 = atan(f);
8903 else
8904 rettv->vval.v_float = 0.0;
8905}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008906
8907/*
8908 * "atan2()" function
8909 */
8910 static void
8911f_atan2(argvars, rettv)
8912 typval_T *argvars;
8913 typval_T *rettv;
8914{
8915 float_T fx, fy;
8916
8917 rettv->v_type = VAR_FLOAT;
8918 if (get_float_arg(argvars, &fx) == OK
8919 && get_float_arg(&argvars[1], &fy) == OK)
8920 rettv->vval.v_float = atan2(fx, fy);
8921 else
8922 rettv->vval.v_float = 0.0;
8923}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926/*
8927 * "browse(save, title, initdir, default)" function
8928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934#ifdef FEAT_BROWSE
8935 int save;
8936 char_u *title;
8937 char_u *initdir;
8938 char_u *defname;
8939 char_u buf[NUMBUFLEN];
8940 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 save = get_tv_number_chk(&argvars[0], &error);
8944 title = get_tv_string_chk(&argvars[1]);
8945 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8946 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 if (error || title == NULL || initdir == NULL || defname == NULL)
8949 rettv->vval.v_string = NULL;
8950 else
8951 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008952 do_browse(save ? BROWSE_SAVE : 0,
8953 title, defname, NULL, initdir, NULL, curbuf);
8954#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008958}
8959
8960/*
8961 * "browsedir(title, initdir)" function
8962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967{
8968#ifdef FEAT_BROWSE
8969 char_u *title;
8970 char_u *initdir;
8971 char_u buf[NUMBUFLEN];
8972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 title = get_tv_string_chk(&argvars[0]);
8974 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 if (title == NULL || initdir == NULL)
8977 rettv->vval.v_string = NULL;
8978 else
8979 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008980 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985}
8986
Bram Moolenaar33570922005-01-25 22:26:29 +00008987static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989/*
8990 * Find a buffer by number or exact name.
8991 */
8992 static buf_T *
8993find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008998 if (avar->v_type == VAR_NUMBER)
8999 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009000 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009003 if (buf == NULL)
9004 {
9005 /* No full path name match, try a match with a URL or a "nofile"
9006 * buffer, these don't use the full path. */
9007 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9008 if (buf->b_fname != NULL
9009 && (path_with_url(buf->b_fname)
9010#ifdef FEAT_QUICKFIX
9011 || bt_nofile(buf)
9012#endif
9013 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009015 break;
9016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018 return buf;
9019}
9020
9021/*
9022 * "bufexists(expr)" function
9023 */
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030}
9031
9032/*
9033 * "buflisted(expr)" function
9034 */
9035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 buf_T *buf;
9041
9042 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "bufloaded(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
9056 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
9063 * Get buffer by number or pattern.
9064 */
9065 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009066get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009068 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 int save_magic;
9072 char_u *save_cpo;
9073 buf_T *buf;
9074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 if (tv->v_type == VAR_NUMBER)
9076 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009077 if (tv->v_type != VAR_STRING)
9078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (name == NULL || *name == NUL)
9080 return curbuf;
9081 if (name[0] == '$' && name[1] == NUL)
9082 return lastbuf;
9083
9084 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9085 save_magic = p_magic;
9086 p_magic = TRUE;
9087 save_cpo = p_cpo;
9088 p_cpo = (char_u *)"";
9089
9090 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009091 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
9093 p_magic = save_magic;
9094 p_cpo = save_cpo;
9095
9096 /* If not found, try expanding the name, like done for bufexists(). */
9097 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
9100 return buf;
9101}
9102
9103/*
9104 * "bufname(expr)" function
9105 */
9106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 buf_T *buf;
9112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009115 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 --emsg_off;
9122}
9123
9124/*
9125 * "bufnr(expr)" function
9126 */
9127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009133 int error = FALSE;
9134 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009138 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009139 --emsg_off;
9140
9141 /* If the buffer isn't found and the second argument is not zero create a
9142 * new buffer. */
9143 if (buf == NULL
9144 && argvars[1].v_type != VAR_UNKNOWN
9145 && get_tv_number_chk(&argvars[1], &error) != 0
9146 && !error
9147 && (name = get_tv_string_chk(&argvars[0])) != NULL
9148 && !error)
9149 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155}
9156
9157/*
9158 * "bufwinnr(nr)" function
9159 */
9160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165#ifdef FEAT_WINDOWS
9166 win_T *wp;
9167 int winnr = 0;
9168#endif
9169 buf_T *buf;
9170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009173 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#ifdef FEAT_WINDOWS
9175 for (wp = firstwin; wp; wp = wp->w_next)
9176 {
9177 ++winnr;
9178 if (wp->w_buffer == buf)
9179 break;
9180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#endif
9185 --emsg_off;
9186}
9187
9188/*
9189 * "byte2line(byte)" function
9190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#else
9199 long boff = 0;
9200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 (linenr_T)0, &boff);
9207#endif
9208}
9209
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009210 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009211byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009212 typval_T *argvars;
9213 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009214 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009215{
9216#ifdef FEAT_MBYTE
9217 char_u *t;
9218#endif
9219 char_u *str;
9220 long idx;
9221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009222 str = get_tv_string_chk(&argvars[0]);
9223 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009226 return;
9227
9228#ifdef FEAT_MBYTE
9229 t = str;
9230 for ( ; idx > 0; idx--)
9231 {
9232 if (*t == NUL) /* EOL reached */
9233 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009234 if (enc_utf8 && comp)
9235 t += utf_ptr2len(t);
9236 else
9237 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009239 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009241 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009243#endif
9244}
9245
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009246/*
9247 * "byteidx()" function
9248 */
9249 static void
9250f_byteidx(argvars, rettv)
9251 typval_T *argvars;
9252 typval_T *rettv;
9253{
9254 byteidx(argvars, rettv, FALSE);
9255}
9256
9257/*
9258 * "byteidxcomp()" function
9259 */
9260 static void
9261f_byteidxcomp(argvars, rettv)
9262 typval_T *argvars;
9263 typval_T *rettv;
9264{
9265 byteidx(argvars, rettv, TRUE);
9266}
9267
Bram Moolenaardb913952012-06-29 12:54:53 +02009268 int
9269func_call(name, args, selfdict, rettv)
9270 char_u *name;
9271 typval_T *args;
9272 dict_T *selfdict;
9273 typval_T *rettv;
9274{
9275 listitem_T *item;
9276 typval_T argv[MAX_FUNC_ARGS + 1];
9277 int argc = 0;
9278 int dummy;
9279 int r = 0;
9280
9281 for (item = args->vval.v_list->lv_first; item != NULL;
9282 item = item->li_next)
9283 {
9284 if (argc == MAX_FUNC_ARGS)
9285 {
9286 EMSG(_("E699: Too many arguments"));
9287 break;
9288 }
9289 /* Make a copy of each argument. This is needed to be able to set
9290 * v_lock to VAR_FIXED in the copy without changing the original list.
9291 */
9292 copy_tv(&item->li_tv, &argv[argc++]);
9293 }
9294
9295 if (item == NULL)
9296 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9297 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9298 &dummy, TRUE, selfdict);
9299
9300 /* Free the arguments. */
9301 while (argc > 0)
9302 clear_tv(&argv[--argc]);
9303
9304 return r;
9305}
9306
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009307/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009308 * "call(func, arglist)" function
9309 */
9310 static void
9311f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009314{
9315 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009317
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009318 if (argvars[1].v_type != VAR_LIST)
9319 {
9320 EMSG(_(e_listreq));
9321 return;
9322 }
9323 if (argvars[1].vval.v_list == NULL)
9324 return;
9325
9326 if (argvars[0].v_type == VAR_FUNC)
9327 func = argvars[0].vval.v_string;
9328 else
9329 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 if (*func == NUL)
9331 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 if (argvars[2].v_type != VAR_UNKNOWN)
9334 {
9335 if (argvars[2].v_type != VAR_DICT)
9336 {
9337 EMSG(_(e_dictreq));
9338 return;
9339 }
9340 selfdict = argvars[2].vval.v_dict;
9341 }
9342
Bram Moolenaardb913952012-06-29 12:54:53 +02009343 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009344}
9345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009346#ifdef FEAT_FLOAT
9347/*
9348 * "ceil({float})" function
9349 */
9350 static void
9351f_ceil(argvars, rettv)
9352 typval_T *argvars;
9353 typval_T *rettv;
9354{
9355 float_T f;
9356
9357 rettv->v_type = VAR_FLOAT;
9358 if (get_float_arg(argvars, &f) == OK)
9359 rettv->vval.v_float = ceil(f);
9360 else
9361 rettv->vval.v_float = 0.0;
9362}
9363#endif
9364
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009365/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009366 * "changenr()" function
9367 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009368 static void
9369f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009370 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009371 typval_T *rettv;
9372{
9373 rettv->vval.v_number = curbuf->b_u_seq_cur;
9374}
9375
9376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 * "char2nr(string)" function
9378 */
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384#ifdef FEAT_MBYTE
9385 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009386 {
9387 int utf8 = 0;
9388
9389 if (argvars[1].v_type != VAR_UNKNOWN)
9390 utf8 = get_tv_number_chk(&argvars[1], NULL);
9391
9392 if (utf8)
9393 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9394 else
9395 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 else
9398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
9403 * "cindent(lnum)" function
9404 */
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410#ifdef FEAT_CINDENT
9411 pos_T pos;
9412 linenr_T lnum;
9413
9414 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9417 {
9418 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 curwin->w_cursor = pos;
9421 }
9422 else
9423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425}
9426
9427/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009428 * "clearmatches()" function
9429 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009430 static void
9431f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009432 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009433 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009434{
9435#ifdef FEAT_SEARCH_EXTRA
9436 clear_matches(curwin);
9437#endif
9438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * "col(string)" function
9442 */
9443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 colnr_T col = 0;
9449 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 fp = var2fpos(&argvars[0], FALSE, &fnum);
9453 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 {
9455 if (fp->col == MAXCOL)
9456 {
9457 /* '> can be MAXCOL, get the length of the line then */
9458 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009459 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 else
9461 col = MAXCOL;
9462 }
9463 else
9464 {
9465 col = fp->col + 1;
9466#ifdef FEAT_VIRTUALEDIT
9467 /* col(".") when the cursor is on the NUL at the end of the line
9468 * because of "coladd" can be seen as an extra column. */
9469 if (virtual_active() && fp == &curwin->w_cursor)
9470 {
9471 char_u *p = ml_get_cursor();
9472
9473 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9474 curwin->w_virtcol - curwin->w_cursor.coladd))
9475 {
9476# ifdef FEAT_MBYTE
9477 int l;
9478
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009479 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 col += l;
9481# else
9482 if (*p != NUL && p[1] == NUL)
9483 ++col;
9484# endif
9485 }
9486 }
9487#endif
9488 }
9489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492
Bram Moolenaar572cb562005-08-05 21:35:02 +00009493#if defined(FEAT_INS_EXPAND)
9494/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009495 * "complete()" function
9496 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009497 static void
9498f_complete(argvars, rettv)
9499 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009500 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009501{
9502 int startcol;
9503
9504 if ((State & INSERT) == 0)
9505 {
9506 EMSG(_("E785: complete() can only be used in Insert mode"));
9507 return;
9508 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009509
9510 /* Check for undo allowed here, because if something was already inserted
9511 * the line was already saved for undo and this check isn't done. */
9512 if (!undo_allowed())
9513 return;
9514
Bram Moolenaarade00832006-03-10 21:46:58 +00009515 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9516 {
9517 EMSG(_(e_invarg));
9518 return;
9519 }
9520
9521 startcol = get_tv_number_chk(&argvars[0], NULL);
9522 if (startcol <= 0)
9523 return;
9524
9525 set_completion(startcol - 1, argvars[1].vval.v_list);
9526}
9527
9528/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529 * "complete_add()" function
9530 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009531 static void
9532f_complete_add(argvars, rettv)
9533 typval_T *argvars;
9534 typval_T *rettv;
9535{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009536 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009537}
9538
9539/*
9540 * "complete_check()" function
9541 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009542 static void
9543f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009544 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009545 typval_T *rettv;
9546{
9547 int saved = RedrawingDisabled;
9548
9549 RedrawingDisabled = 0;
9550 ins_compl_check_keys(0);
9551 rettv->vval.v_number = compl_interrupted;
9552 RedrawingDisabled = saved;
9553}
9554#endif
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556/*
9557 * "confirm(message, buttons[, default [, type]])" function
9558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009561 typval_T *argvars UNUSED;
9562 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9565 char_u *message;
9566 char_u *buttons = NULL;
9567 char_u buf[NUMBUFLEN];
9568 char_u buf2[NUMBUFLEN];
9569 int def = 1;
9570 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 char_u *typestr;
9572 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 message = get_tv_string_chk(&argvars[0]);
9575 if (message == NULL)
9576 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9580 if (buttons == NULL)
9581 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9588 if (typestr == NULL)
9589 error = TRUE;
9590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 switch (TOUPPER_ASC(*typestr))
9593 {
9594 case 'E': type = VIM_ERROR; break;
9595 case 'Q': type = VIM_QUESTION; break;
9596 case 'I': type = VIM_INFO; break;
9597 case 'W': type = VIM_WARNING; break;
9598 case 'G': type = VIM_GENERIC; break;
9599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
9601 }
9602 }
9603 }
9604
9605 if (buttons == NULL || *buttons == NUL)
9606 buttons = (char_u *)_("&Ok");
9607
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009608 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009610 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611#endif
9612}
9613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009614/*
9615 * "copy()" function
9616 */
9617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009619 typval_T *argvars;
9620 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009622 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009625#ifdef FEAT_FLOAT
9626/*
9627 * "cos()" function
9628 */
9629 static void
9630f_cos(argvars, rettv)
9631 typval_T *argvars;
9632 typval_T *rettv;
9633{
9634 float_T f;
9635
9636 rettv->v_type = VAR_FLOAT;
9637 if (get_float_arg(argvars, &f) == OK)
9638 rettv->vval.v_float = cos(f);
9639 else
9640 rettv->vval.v_float = 0.0;
9641}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009642
9643/*
9644 * "cosh()" function
9645 */
9646 static void
9647f_cosh(argvars, rettv)
9648 typval_T *argvars;
9649 typval_T *rettv;
9650{
9651 float_T f;
9652
9653 rettv->v_type = VAR_FLOAT;
9654 if (get_float_arg(argvars, &f) == OK)
9655 rettv->vval.v_float = cosh(f);
9656 else
9657 rettv->vval.v_float = 0.0;
9658}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009659#endif
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009662 * "count()" function
9663 */
9664 static void
9665f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 typval_T *argvars;
9667 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009668{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009669 long n = 0;
9670 int ic = FALSE;
9671
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 listitem_T *li;
9675 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009676 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009677
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if ((l = argvars[0].vval.v_list) != NULL)
9679 {
9680 li = l->lv_first;
9681 if (argvars[2].v_type != VAR_UNKNOWN)
9682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009683 int error = FALSE;
9684
9685 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 if (argvars[3].v_type != VAR_UNKNOWN)
9687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 idx = get_tv_number_chk(&argvars[3], &error);
9689 if (!error)
9690 {
9691 li = list_find(l, idx);
9692 if (li == NULL)
9693 EMSGN(_(e_listidx), idx);
9694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 if (error)
9697 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009698 }
9699
9700 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009701 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009702 ++n;
9703 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 else if (argvars[0].v_type == VAR_DICT)
9706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 int todo;
9708 dict_T *d;
9709 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009711 if ((d = argvars[0].vval.v_dict) != NULL)
9712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009713 int error = FALSE;
9714
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715 if (argvars[2].v_type != VAR_UNKNOWN)
9716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009718 if (argvars[3].v_type != VAR_UNKNOWN)
9719 EMSG(_(e_invarg));
9720 }
9721
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009722 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009724 {
9725 if (!HASHITEM_EMPTY(hi))
9726 {
9727 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009728 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009729 ++n;
9730 }
9731 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 }
9733 }
9734 else
9735 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009736 rettv->vval.v_number = n;
9737}
9738
9739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9741 *
9742 * Checks the existence of a cscope connection.
9743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009746 typval_T *argvars UNUSED;
9747 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifdef FEAT_CSCOPE
9750 int num = 0;
9751 char_u *dbpath = NULL;
9752 char_u *prepend = NULL;
9753 char_u buf[NUMBUFLEN];
9754
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009755 if (argvars[0].v_type != VAR_UNKNOWN
9756 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 num = (int)get_tv_number(&argvars[0]);
9759 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 }
9763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
9766}
9767
9768/*
9769 * "cursor(lnum, col)" function
9770 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009771 * Moves the cursor to the specified line and column.
9772 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009780#ifdef FEAT_VIRTUALEDIT
9781 long coladd = 0;
9782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009784 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785 if (argvars[1].v_type == VAR_UNKNOWN)
9786 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009787 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009788
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009789 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009790 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009791 line = pos.lnum;
9792 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009793#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009794 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009795#endif
9796 }
9797 else
9798 {
9799 line = get_tv_lnum(argvars);
9800 col = get_tv_number_chk(&argvars[1], NULL);
9801#ifdef FEAT_VIRTUALEDIT
9802 if (argvars[2].v_type != VAR_UNKNOWN)
9803 coladd = get_tv_number_chk(&argvars[2], NULL);
9804#endif
9805 }
9806 if (line < 0 || col < 0
9807#ifdef FEAT_VIRTUALEDIT
9808 || coladd < 0
9809#endif
9810 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (line > 0)
9813 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (col > 0)
9815 curwin->w_cursor.col = col - 1;
9816#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009817 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#endif
9819
9820 /* Make sure the cursor is in a valid position. */
9821 check_cursor();
9822#ifdef FEAT_MBYTE
9823 /* Correct cursor for multi-byte character. */
9824 if (has_mbyte)
9825 mb_adjust_cursor();
9826#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009827
9828 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
9832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 */
9835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009837 typval_T *argvars;
9838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009840 int noref = 0;
9841
9842 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009844 if (noref < 0 || noref > 1)
9845 EMSG(_(e_invarg));
9846 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009847 {
9848 current_copyID += COPYID_INC;
9849 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
9853/*
9854 * "delete()" function
9855 */
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * "did_filetype()" function
9869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009872 typval_T *argvars UNUSED;
9873 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877#endif
9878}
9879
9880/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881 * "diff_filler()" function
9882 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009885 typval_T *argvars UNUSED;
9886 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009887{
9888#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009890#endif
9891}
9892
9893/*
9894 * "diff_hlID()" function
9895 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009898 typval_T *argvars UNUSED;
9899 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009900{
9901#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009903 static linenr_T prev_lnum = 0;
9904 static int changedtick = 0;
9905 static int fnum = 0;
9906 static int change_start = 0;
9907 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009908 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009909 int filler_lines;
9910 int col;
9911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 if (lnum < 0) /* ignore type error in {lnum} arg */
9913 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914 if (lnum != prev_lnum
9915 || changedtick != curbuf->b_changedtick
9916 || fnum != curbuf->b_fnum)
9917 {
9918 /* New line, buffer, change: need to get the values. */
9919 filler_lines = diff_check(curwin, lnum);
9920 if (filler_lines < 0)
9921 {
9922 if (filler_lines == -1)
9923 {
9924 change_start = MAXCOL;
9925 change_end = -1;
9926 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9927 hlID = HLF_ADD; /* added line */
9928 else
9929 hlID = HLF_CHD; /* changed line */
9930 }
9931 else
9932 hlID = HLF_ADD; /* added line */
9933 }
9934 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009935 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009936 prev_lnum = lnum;
9937 changedtick = curbuf->b_changedtick;
9938 fnum = curbuf->b_fnum;
9939 }
9940
9941 if (hlID == HLF_CHD || hlID == HLF_TXD)
9942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009944 if (col >= change_start && col <= change_end)
9945 hlID = HLF_TXD; /* changed text */
9946 else
9947 hlID = HLF_CHD; /* changed line */
9948 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009949 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009950#endif
9951}
9952
9953/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009954 * "empty({expr})" function
9955 */
9956 static void
9957f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009960{
9961 int n;
9962
9963 switch (argvars[0].v_type)
9964 {
9965 case VAR_STRING:
9966 case VAR_FUNC:
9967 n = argvars[0].vval.v_string == NULL
9968 || *argvars[0].vval.v_string == NUL;
9969 break;
9970 case VAR_NUMBER:
9971 n = argvars[0].vval.v_number == 0;
9972 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009973#ifdef FEAT_FLOAT
9974 case VAR_FLOAT:
9975 n = argvars[0].vval.v_float == 0.0;
9976 break;
9977#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009978 case VAR_LIST:
9979 n = argvars[0].vval.v_list == NULL
9980 || argvars[0].vval.v_list->lv_first == NULL;
9981 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009982 case VAR_DICT:
9983 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009986 default:
9987 EMSG2(_(e_intern2), "f_empty()");
9988 n = 0;
9989 }
9990
9991 rettv->vval.v_number = n;
9992}
9993
9994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 * "escape({string}, {chars})" function
9996 */
9997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 typval_T *argvars;
10000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
10002 char_u buf[NUMBUFLEN];
10003
Bram Moolenaar758711c2005-02-02 23:11:38 +000010004 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10005 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007}
10008
10009/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010010 * "eval()" function
10011 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010012 static void
10013f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010014 typval_T *argvars;
10015 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010016{
10017 char_u *s;
10018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 s = get_tv_string_chk(&argvars[0]);
10020 if (s != NULL)
10021 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10024 {
10025 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010026 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010028 else if (*s != NUL)
10029 EMSG(_(e_trailing));
10030}
10031
10032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 * "eventhandler()" function
10034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043/*
10044 * "executable()" function
10045 */
10046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010048 typval_T *argvars;
10049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010051 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10052}
10053
10054/*
10055 * "exepath()" function
10056 */
10057 static void
10058f_exepath(argvars, rettv)
10059 typval_T *argvars;
10060 typval_T *rettv;
10061{
10062 char_u *p = NULL;
10063
10064 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10065 rettv->v_type = VAR_STRING;
10066 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067}
10068
10069/*
10070 * "exists()" function
10071 */
10072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010074 typval_T *argvars;
10075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 char_u *p;
10078 char_u *name;
10079 int n = FALSE;
10080 int len = 0;
10081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (*p == '$') /* environment variable */
10084 {
10085 /* first try "normal" environment variables (fast) */
10086 if (mch_getenv(p + 1) != NULL)
10087 n = TRUE;
10088 else
10089 {
10090 /* try expanding things like $VIM and ${HOME} */
10091 p = expand_env_save(p);
10092 if (p != NULL && *p != '$')
10093 n = TRUE;
10094 vim_free(p);
10095 }
10096 }
10097 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010100 if (*skipwhite(p) != NUL)
10101 n = FALSE; /* trailing garbage */
10102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else if (*p == '*') /* internal or user defined function */
10104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010105 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107 else if (*p == ':')
10108 {
10109 n = cmd_exists(p + 1);
10110 }
10111 else if (*p == '#')
10112 {
10113#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010114 if (p[1] == '#')
10115 n = autocmd_supported(p + 2);
10116 else
10117 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#endif
10119 }
10120 else /* internal variable */
10121 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010122 char_u *tofree;
10123 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010125 /* get_name_len() takes care of expanding curly braces */
10126 name = p;
10127 len = get_name_len(&p, &tofree, TRUE, FALSE);
10128 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010130 if (tofree != NULL)
10131 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010132 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010133 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010135 /* handle d.key, l[idx], f(expr) */
10136 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10137 if (n)
10138 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 }
10140 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010141 if (*p != NUL)
10142 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010144 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
10146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148}
10149
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010150#ifdef FEAT_FLOAT
10151/*
10152 * "exp()" function
10153 */
10154 static void
10155f_exp(argvars, rettv)
10156 typval_T *argvars;
10157 typval_T *rettv;
10158{
10159 float_T f;
10160
10161 rettv->v_type = VAR_FLOAT;
10162 if (get_float_arg(argvars, &f) == OK)
10163 rettv->vval.v_float = exp(f);
10164 else
10165 rettv->vval.v_float = 0.0;
10166}
10167#endif
10168
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169/*
10170 * "expand()" function
10171 */
10172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177 char_u *s;
10178 int len;
10179 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010180 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010183 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010186 if (argvars[1].v_type != VAR_UNKNOWN
10187 && argvars[2].v_type != VAR_UNKNOWN
10188 && get_tv_number_chk(&argvars[2], &error)
10189 && !error)
10190 {
10191 rettv->v_type = VAR_LIST;
10192 rettv->vval.v_list = NULL;
10193 }
10194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 if (*s == '%' || *s == '#' || *s == '<')
10197 {
10198 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010199 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 if (rettv->v_type == VAR_LIST)
10202 {
10203 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10204 list_append_string(rettv->vval.v_list, result, -1);
10205 else
10206 vim_free(result);
10207 }
10208 else
10209 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 }
10211 else
10212 {
10213 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010214 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (argvars[1].v_type != VAR_UNKNOWN
10216 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010217 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 if (!error)
10219 {
10220 ExpandInit(&xpc);
10221 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010222 if (p_wic)
10223 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010224 if (rettv->v_type == VAR_STRING)
10225 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10226 options, WILD_ALL);
10227 else if (rettv_list_alloc(rettv) != FAIL)
10228 {
10229 int i;
10230
10231 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10232 for (i = 0; i < xpc.xp_numfiles; i++)
10233 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10234 ExpandCleanup(&xpc);
10235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 }
10237 else
10238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240}
10241
10242/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010243 * Go over all entries in "d2" and add them to "d1".
10244 * When "action" is "error" then a duplicate key is an error.
10245 * When "action" is "force" then a duplicate key is overwritten.
10246 * Otherwise duplicate keys are ignored ("action" is "keep").
10247 */
10248 void
10249dict_extend(d1, d2, action)
10250 dict_T *d1;
10251 dict_T *d2;
10252 char_u *action;
10253{
10254 dictitem_T *di1;
10255 hashitem_T *hi2;
10256 int todo;
10257
10258 todo = (int)d2->dv_hashtab.ht_used;
10259 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10260 {
10261 if (!HASHITEM_EMPTY(hi2))
10262 {
10263 --todo;
10264 di1 = dict_find(d1, hi2->hi_key, -1);
10265 if (d1->dv_scope != 0)
10266 {
10267 /* Disallow replacing a builtin function in l: and g:.
10268 * Check the key to be valid when adding to any
10269 * scope. */
10270 if (d1->dv_scope == VAR_DEF_SCOPE
10271 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10272 && var_check_func_name(hi2->hi_key,
10273 di1 == NULL))
10274 break;
10275 if (!valid_varname(hi2->hi_key))
10276 break;
10277 }
10278 if (di1 == NULL)
10279 {
10280 di1 = dictitem_copy(HI2DI(hi2));
10281 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10282 dictitem_free(di1);
10283 }
10284 else if (*action == 'e')
10285 {
10286 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10287 break;
10288 }
10289 else if (*action == 'f' && HI2DI(hi2) != di1)
10290 {
10291 clear_tv(&di1->di_tv);
10292 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10293 }
10294 }
10295 }
10296}
10297
10298/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010299 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010301 */
10302 static void
10303f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010304 typval_T *argvars;
10305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010307 char *arg_errmsg = N_("extend() argument");
10308
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 list_T *l1, *l2;
10312 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315
Bram Moolenaare9a41262005-01-15 22:18:47 +000010316 l1 = argvars[0].vval.v_list;
10317 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010318 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010319 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 {
10321 if (argvars[2].v_type != VAR_UNKNOWN)
10322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 before = get_tv_number_chk(&argvars[2], &error);
10324 if (error)
10325 return; /* type error; errmsg already given */
10326
Bram Moolenaar758711c2005-02-02 23:11:38 +000010327 if (before == l1->lv_len)
10328 item = NULL;
10329 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010331 item = list_find(l1, before);
10332 if (item == NULL)
10333 {
10334 EMSGN(_(e_listidx), before);
10335 return;
10336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010337 }
10338 }
10339 else
10340 item = NULL;
10341 list_extend(l1, l2, item);
10342
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343 copy_tv(&argvars[0], rettv);
10344 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10347 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010348 dict_T *d1, *d2;
10349 char_u *action;
10350 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351
10352 d1 = argvars[0].vval.v_dict;
10353 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010354 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010355 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 {
10357 /* Check the third argument. */
10358 if (argvars[2].v_type != VAR_UNKNOWN)
10359 {
10360 static char *(av[]) = {"keep", "force", "error"};
10361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010362 action = get_tv_string_chk(&argvars[2]);
10363 if (action == NULL)
10364 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 for (i = 0; i < 3; ++i)
10366 if (STRCMP(action, av[i]) == 0)
10367 break;
10368 if (i == 3)
10369 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010370 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 return;
10372 }
10373 }
10374 else
10375 action = (char_u *)"force";
10376
Bram Moolenaara9922d62013-05-30 13:01:18 +020010377 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010378
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 copy_tv(&argvars[0], rettv);
10380 }
10381 }
10382 else
10383 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384}
10385
10386/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010387 * "feedkeys()" function
10388 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010389 static void
10390f_feedkeys(argvars, rettv)
10391 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010392 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010393{
10394 int remap = TRUE;
10395 char_u *keys, *flags;
10396 char_u nbuf[NUMBUFLEN];
10397 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010398 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010399
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010400 /* This is not allowed in the sandbox. If the commands would still be
10401 * executed in the sandbox it would be OK, but it probably happens later,
10402 * when "sandbox" is no longer set. */
10403 if (check_secure())
10404 return;
10405
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 keys = get_tv_string(&argvars[0]);
10407 if (*keys != NUL)
10408 {
10409 if (argvars[1].v_type != VAR_UNKNOWN)
10410 {
10411 flags = get_tv_string_buf(&argvars[1], nbuf);
10412 for ( ; *flags != NUL; ++flags)
10413 {
10414 switch (*flags)
10415 {
10416 case 'n': remap = FALSE; break;
10417 case 'm': remap = TRUE; break;
10418 case 't': typed = TRUE; break;
10419 }
10420 }
10421 }
10422
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010423 /* Need to escape K_SPECIAL and CSI before putting the string in the
10424 * typeahead buffer. */
10425 keys_esc = vim_strsave_escape_csi(keys);
10426 if (keys_esc != NULL)
10427 {
10428 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010429 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010430 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010431 if (vgetc_busy)
10432 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010433 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010434 }
10435}
10436
10437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 * "filereadable()" function
10439 */
10440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 typval_T *argvars;
10443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010445 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 char_u *p;
10447 int n;
10448
Bram Moolenaarc236c162008-07-13 17:41:49 +000010449#ifndef O_NONBLOCK
10450# define O_NONBLOCK 0
10451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010453 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10454 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 {
10456 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010457 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 }
10459 else
10460 n = FALSE;
10461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463}
10464
10465/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010466 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * rights to write into.
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010474 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475}
10476
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010477static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
10479 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010480findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010482 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010483 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010484{
10485#ifdef FEAT_SEARCHPATH
10486 char_u *fname;
10487 char_u *fresult = NULL;
10488 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10489 char_u *p;
10490 char_u pathbuf[NUMBUFLEN];
10491 int count = 1;
10492 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010493 int error = FALSE;
10494#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010495
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010496 rettv->vval.v_string = NULL;
10497 rettv->v_type = VAR_STRING;
10498
10499#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010501
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10505 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010506 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 else
10508 {
10509 if (*p != NUL)
10510 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010513 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010515 }
10516
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010517 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10518 error = TRUE;
10519
10520 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 do
10523 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010524 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010525 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 fresult = find_file_in_path_option(first ? fname : NULL,
10527 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010528 0, first, path,
10529 find_what,
10530 curbuf->b_ffname,
10531 find_what == FINDFILE_DIR
10532 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010534
10535 if (fresult != NULL && rettv->v_type == VAR_LIST)
10536 list_append_string(rettv->vval.v_list, fresult, -1);
10537
10538 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010540
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010541 if (rettv->v_type == VAR_STRING)
10542 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010543#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010544}
10545
Bram Moolenaar33570922005-01-25 22:26:29 +000010546static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10547static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548
10549/*
10550 * Implementation of map() and filter().
10551 */
10552 static void
10553filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010556 int map;
10557{
10558 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 listitem_T *li, *nli;
10561 list_T *l = NULL;
10562 dictitem_T *di;
10563 hashtab_T *ht;
10564 hashitem_T *hi;
10565 dict_T *d = NULL;
10566 typval_T save_val;
10567 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010569 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010570 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10571 char *arg_errmsg = (map ? N_("map() argument")
10572 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010573 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010574 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010578 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010579 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 return;
10581 }
10582 else if (argvars[0].v_type == VAR_DICT)
10583 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010584 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010585 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 return;
10587 }
10588 else
10589 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010590 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 return;
10592 }
10593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 expr = get_tv_string_buf_chk(&argvars[1], buf);
10595 /* On type errors, the preceding call has already displayed an error
10596 * message. Avoid a misleading error message for an empty string that
10597 * was not passed as argument. */
10598 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 prepare_vimvar(VV_VAL, &save_val);
10601 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010602
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010603 /* We reset "did_emsg" to be able to detect whether an error
10604 * occurred during evaluation of the expression. */
10605 save_did_emsg = did_emsg;
10606 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010607
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010608 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 vimvars[VV_KEY].vv_type = VAR_STRING;
10612
10613 ht = &d->dv_hashtab;
10614 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 if (!HASHITEM_EMPTY(hi))
10619 {
10620 --todo;
10621 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010622 if (tv_check_lock(di->di_tv.v_lock,
10623 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 break;
10625 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010626 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010627 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 break;
10629 if (!map && rem)
10630 dictitem_remove(d, di);
10631 clear_tv(&vimvars[VV_KEY].vv_tv);
10632 }
10633 }
10634 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 }
10636 else
10637 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010638 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 for (li = l->lv_first; li != NULL; li = nli)
10641 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010642 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010643 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010645 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010646 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010647 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010648 break;
10649 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010651 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010653 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010654
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010655 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010657
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010658 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660
10661 copy_tv(&argvars[0], rettv);
10662}
10663
10664 static int
10665filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010666 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010667 char_u *expr;
10668 int map;
10669 int *remp;
10670{
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010673 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010676 s = expr;
10677 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 if (*s != NUL) /* check for trailing chars after expr */
10680 {
10681 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010682 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010683 }
10684 if (map)
10685 {
10686 /* map(): replace the list item value */
10687 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010688 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010689 *tv = rettv;
10690 }
10691 else
10692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 int error = FALSE;
10694
Bram Moolenaare9a41262005-01-15 22:18:47 +000010695 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 /* On type error, nothing has been removed; return FAIL to stop the
10699 * loop. The error message was given by get_tv_number_chk(). */
10700 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010701 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010702 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010703 retval = OK;
10704theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010705 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010706 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707}
10708
10709/*
10710 * "filter()" function
10711 */
10712 static void
10713f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010716{
10717 filter_map(argvars, rettv, FALSE);
10718}
10719
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010721 * "finddir({fname}[, {path}[, {count}]])" function
10722 */
10723 static void
10724f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010727{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010728 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010729}
10730
10731/*
10732 * "findfile({fname}[, {path}[, {count}]])" function
10733 */
10734 static void
10735f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010738{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010739 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740}
10741
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010742#ifdef FEAT_FLOAT
10743/*
10744 * "float2nr({float})" function
10745 */
10746 static void
10747f_float2nr(argvars, rettv)
10748 typval_T *argvars;
10749 typval_T *rettv;
10750{
10751 float_T f;
10752
10753 if (get_float_arg(argvars, &f) == OK)
10754 {
10755 if (f < -0x7fffffff)
10756 rettv->vval.v_number = -0x7fffffff;
10757 else if (f > 0x7fffffff)
10758 rettv->vval.v_number = 0x7fffffff;
10759 else
10760 rettv->vval.v_number = (varnumber_T)f;
10761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010762}
10763
10764/*
10765 * "floor({float})" function
10766 */
10767 static void
10768f_floor(argvars, rettv)
10769 typval_T *argvars;
10770 typval_T *rettv;
10771{
10772 float_T f;
10773
10774 rettv->v_type = VAR_FLOAT;
10775 if (get_float_arg(argvars, &f) == OK)
10776 rettv->vval.v_float = floor(f);
10777 else
10778 rettv->vval.v_float = 0.0;
10779}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010780
10781/*
10782 * "fmod()" function
10783 */
10784 static void
10785f_fmod(argvars, rettv)
10786 typval_T *argvars;
10787 typval_T *rettv;
10788{
10789 float_T fx, fy;
10790
10791 rettv->v_type = VAR_FLOAT;
10792 if (get_float_arg(argvars, &fx) == OK
10793 && get_float_arg(&argvars[1], &fy) == OK)
10794 rettv->vval.v_float = fmod(fx, fy);
10795 else
10796 rettv->vval.v_float = 0.0;
10797}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010798#endif
10799
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010801 * "fnameescape({string})" function
10802 */
10803 static void
10804f_fnameescape(argvars, rettv)
10805 typval_T *argvars;
10806 typval_T *rettv;
10807{
10808 rettv->vval.v_string = vim_strsave_fnameescape(
10809 get_tv_string(&argvars[0]), FALSE);
10810 rettv->v_type = VAR_STRING;
10811}
10812
10813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 * "fnamemodify({fname}, {mods})" function
10815 */
10816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 char_u *fname;
10822 char_u *mods;
10823 int usedlen = 0;
10824 int len;
10825 char_u *fbuf = NULL;
10826 char_u buf[NUMBUFLEN];
10827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 fname = get_tv_string_chk(&argvars[0]);
10829 mods = get_tv_string_buf_chk(&argvars[1], buf);
10830 if (fname == NULL || mods == NULL)
10831 fname = NULL;
10832 else
10833 {
10834 len = (int)STRLEN(fname);
10835 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 vim_free(fbuf);
10844}
10845
Bram Moolenaar33570922005-01-25 22:26:29 +000010846static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
10848/*
10849 * "foldclosed()" function
10850 */
10851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010855 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857#ifdef FEAT_FOLDING
10858 linenr_T lnum;
10859 linenr_T first, last;
10860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10863 {
10864 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10865 {
10866 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 return;
10871 }
10872 }
10873#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878 * "foldclosed()" function
10879 */
10880 static void
10881f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010882 typval_T *argvars;
10883 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010884{
10885 foldclosed_both(argvars, rettv, FALSE);
10886}
10887
10888/*
10889 * "foldclosedend()" function
10890 */
10891 static void
10892f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895{
10896 foldclosed_both(argvars, rettv, TRUE);
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "foldlevel()" function
10901 */
10902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010904 typval_T *argvars UNUSED;
10905 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907#ifdef FEAT_FOLDING
10908 linenr_T lnum;
10909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914}
10915
10916/*
10917 * "foldtext()" function
10918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924#ifdef FEAT_FOLDING
10925 linenr_T lnum;
10926 char_u *s;
10927 char_u *r;
10928 int len;
10929 char *txt;
10930#endif
10931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 rettv->v_type = VAR_STRING;
10933 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10936 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10937 <= curbuf->b_ml.ml_line_count
10938 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 {
10940 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10942 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 {
10944 if (!linewhite(lnum))
10945 break;
10946 ++lnum;
10947 }
10948
10949 /* Find interesting text in this line. */
10950 s = skipwhite(ml_get(lnum));
10951 /* skip C comment-start */
10952 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010955 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010957 {
10958 s = skipwhite(ml_get(lnum + 1));
10959 if (*s == '*')
10960 s = skipwhite(s + 1);
10961 }
10962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 txt = _("+-%s%3ld lines: ");
10964 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010965 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 + 20 /* for %3ld */
10967 + STRLEN(s))); /* concatenated */
10968 if (r != NULL)
10969 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10971 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10972 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 len = (int)STRLEN(r);
10974 STRCAT(r, s);
10975 /* remove 'foldmarker' and 'commentstring' */
10976 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 }
10979 }
10980#endif
10981}
10982
10983/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010984 * "foldtextresult(lnum)" function
10985 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010990{
10991#ifdef FEAT_FOLDING
10992 linenr_T lnum;
10993 char_u *text;
10994 char_u buf[51];
10995 foldinfo_T foldinfo;
10996 int fold_count;
10997#endif
10998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 rettv->v_type = VAR_STRING;
11000 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011001#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 /* treat illegal types and illegal string values for {lnum} the same */
11004 if (lnum < 0)
11005 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011006 fold_count = foldedCount(curwin, lnum, &foldinfo);
11007 if (fold_count > 0)
11008 {
11009 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11010 &foldinfo, buf);
11011 if (text == buf)
11012 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011014 }
11015#endif
11016}
11017
11018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 * "foreground()" function
11020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011023 typval_T *argvars UNUSED;
11024 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#ifdef FEAT_GUI
11027 if (gui.in_use)
11028 gui_mch_set_foreground();
11029#else
11030# ifdef WIN32
11031 win32_set_foreground();
11032# endif
11033#endif
11034}
11035
11036/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011037 * "function()" function
11038 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *argvars;
11042 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043{
11044 char_u *s;
11045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011047 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011049 /* Don't check an autoload name for existence here. */
11050 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011051 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 else
11053 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011054 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011055 {
11056 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011057 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011058
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011059 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11060 * also be called from another script. Using trans_function_name()
11061 * would also work, but some plugins depend on the name being
11062 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011064 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011065 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011066 if (rettv->vval.v_string != NULL)
11067 {
11068 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011069 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011070 }
11071 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011072 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011073 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 }
11076}
11077
11078/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011079 * "garbagecollect()" function
11080 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011081 static void
11082f_garbagecollect(argvars, rettv)
11083 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011084 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011085{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011086 /* This is postponed until we are back at the toplevel, because we may be
11087 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11088 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011089
11090 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11091 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011092}
11093
11094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 * "get()" function
11096 */
11097 static void
11098f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101{
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 listitem_T *li;
11103 list_T *l;
11104 dictitem_T *di;
11105 dict_T *d;
11106 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 int error = FALSE;
11113
11114 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11115 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 else if (argvars[0].v_type == VAR_DICT)
11120 {
11121 if ((d = argvars[0].vval.v_dict) != NULL)
11122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011123 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 if (di != NULL)
11125 tv = &di->di_tv;
11126 }
11127 }
11128 else
11129 EMSG2(_(e_listdictarg), "get()");
11130
11131 if (tv == NULL)
11132 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 copy_tv(&argvars[2], rettv);
11135 }
11136 else
11137 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138}
11139
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140static 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 +000011141
11142/*
11143 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 * Return a range (from start to end) of lines in rettv from the specified
11145 * buffer.
11146 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011147 */
11148 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011150 buf_T *buf;
11151 linenr_T start;
11152 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011153 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011154 typval_T *rettv;
11155{
11156 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011157
Bram Moolenaar959a1432013-12-14 12:17:38 +010011158 rettv->v_type = VAR_STRING;
11159 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011160 if (retlist && rettv_list_alloc(rettv) == FAIL)
11161 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011162
11163 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11164 return;
11165
11166 if (!retlist)
11167 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011168 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11169 p = ml_get_buf(buf, start, FALSE);
11170 else
11171 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011172 rettv->vval.v_string = vim_strsave(p);
11173 }
11174 else
11175 {
11176 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011177 return;
11178
11179 if (start < 1)
11180 start = 1;
11181 if (end > buf->b_ml.ml_line_count)
11182 end = buf->b_ml.ml_line_count;
11183 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011184 if (list_append_string(rettv->vval.v_list,
11185 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011186 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 }
11188}
11189
11190/*
11191 * "getbufline()" function
11192 */
11193 static void
11194f_getbufline(argvars, rettv)
11195 typval_T *argvars;
11196 typval_T *rettv;
11197{
11198 linenr_T lnum;
11199 linenr_T end;
11200 buf_T *buf;
11201
11202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11203 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011205 --emsg_off;
11206
Bram Moolenaar661b1822005-07-28 22:36:45 +000011207 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011208 if (argvars[2].v_type == VAR_UNKNOWN)
11209 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011210 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011211 end = get_tv_lnum_buf(&argvars[2], buf);
11212
Bram Moolenaar342337a2005-07-21 21:11:17 +000011213 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011214}
11215
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216/*
11217 * "getbufvar()" function
11218 */
11219 static void
11220f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011223{
11224 buf_T *buf;
11225 buf_T *save_curbuf;
11226 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011228 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11231 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011235 rettv->v_type = VAR_STRING;
11236 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237
11238 if (buf != NULL && varname != NULL)
11239 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011240 /* set curbuf to be our buf, temporarily */
11241 save_curbuf = curbuf;
11242 curbuf = buf;
11243
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011245 {
11246 if (get_option_tv(&varname, rettv, TRUE) == OK)
11247 done = TRUE;
11248 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011249 else if (STRCMP(varname, "changedtick") == 0)
11250 {
11251 rettv->v_type = VAR_NUMBER;
11252 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011253 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011254 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 else
11256 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011257 /* Look up the variable. */
11258 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11259 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11260 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011261 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011264 done = TRUE;
11265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011267
11268 /* restore previous notion of curbuf */
11269 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011270 }
11271
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011272 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11273 /* use the default value */
11274 copy_tv(&argvars[2], rettv);
11275
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276 --emsg_off;
11277}
11278
11279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "getchar()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011290 /* Position the cursor. Needed after a message that ends in a space. */
11291 windgoto(msg_row, msg_col);
11292
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 ++no_mapping;
11294 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011295 for (;;)
11296 {
11297 if (argvars[0].v_type == VAR_UNKNOWN)
11298 /* getchar(): blocking wait. */
11299 n = safe_vgetc();
11300 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11301 /* getchar(1): only check if char avail */
11302 n = vpeekc();
11303 else if (error || vpeekc() == NUL)
11304 /* illegal argument or getchar(0) and no char avail: return zero */
11305 n = 0;
11306 else
11307 /* getchar(0) and char avail: return char */
11308 n = safe_vgetc();
11309 if (n == K_IGNORE)
11310 continue;
11311 break;
11312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 --no_mapping;
11314 --allow_keys;
11315
Bram Moolenaar219b8702006-11-01 14:32:36 +000011316 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11317 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11318 vimvars[VV_MOUSE_COL].vv_nr = 0;
11319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 if (IS_SPECIAL(n) || mod_mask != 0)
11322 {
11323 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11324 int i = 0;
11325
11326 /* Turn a special key into three bytes, plus modifier. */
11327 if (mod_mask != 0)
11328 {
11329 temp[i++] = K_SPECIAL;
11330 temp[i++] = KS_MODIFIER;
11331 temp[i++] = mod_mask;
11332 }
11333 if (IS_SPECIAL(n))
11334 {
11335 temp[i++] = K_SPECIAL;
11336 temp[i++] = K_SECOND(n);
11337 temp[i++] = K_THIRD(n);
11338 }
11339#ifdef FEAT_MBYTE
11340 else if (has_mbyte)
11341 i += (*mb_char2bytes)(n, temp + i);
11342#endif
11343 else
11344 temp[i++] = n;
11345 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011348
11349#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011350 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011351 {
11352 int row = mouse_row;
11353 int col = mouse_col;
11354 win_T *win;
11355 linenr_T lnum;
11356# ifdef FEAT_WINDOWS
11357 win_T *wp;
11358# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011359 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011360
11361 if (row >= 0 && col >= 0)
11362 {
11363 /* Find the window at the mouse coordinates and compute the
11364 * text position. */
11365 win = mouse_find_win(&row, &col);
11366 (void)mouse_comp_pos(win, &row, &col, &lnum);
11367# ifdef FEAT_WINDOWS
11368 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011369 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011370# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011371 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011372 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11373 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11374 }
11375 }
11376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 }
11378}
11379
11380/*
11381 * "getcharmod()" function
11382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
11392 * "getcmdline()" function
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
11404 * "getcmdpos()" function
11405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
11414/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011415 * "getcmdtype()" function
11416 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011417 static void
11418f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011419 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011420 typval_T *rettv;
11421{
11422 rettv->v_type = VAR_STRING;
11423 rettv->vval.v_string = alloc(2);
11424 if (rettv->vval.v_string != NULL)
11425 {
11426 rettv->vval.v_string[0] = get_cmdline_type();
11427 rettv->vval.v_string[1] = NUL;
11428 }
11429}
11430
11431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 * "getcwd()" function
11433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011439 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011442 rettv->vval.v_string = NULL;
11443 cwd = alloc(MAXPATHL);
11444 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011446 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11447 {
11448 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011450 if (rettv->vval.v_string != NULL)
11451 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011453 }
11454 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 }
11456}
11457
11458/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011459 * "getfontname()" function
11460 */
11461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011468#ifdef FEAT_GUI
11469 if (gui.in_use)
11470 {
11471 GuiFont font;
11472 char_u *name = NULL;
11473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011475 {
11476 /* Get the "Normal" font. Either the name saved by
11477 * hl_set_font_name() or from the font ID. */
11478 font = gui.norm_font;
11479 name = hl_get_font_name();
11480 }
11481 else
11482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011484 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11485 return;
11486 font = gui_mch_get_font(name, FALSE);
11487 if (font == NOFONT)
11488 return; /* Invalid font name, return empty string. */
11489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011492 gui_mch_free_font(font);
11493 }
11494#endif
11495}
11496
11497/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011498 * "getfperm({fname})" function
11499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504{
11505 char_u *fname;
11506 struct stat st;
11507 char_u *perm = NULL;
11508 char_u flags[] = "rwx";
11509 int i;
11510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011514 if (mch_stat((char *)fname, &st) >= 0)
11515 {
11516 perm = vim_strsave((char_u *)"---------");
11517 if (perm != NULL)
11518 {
11519 for (i = 0; i < 9; i++)
11520 {
11521 if (st.st_mode & (1 << (8 - i)))
11522 perm[i] = flags[i % 3];
11523 }
11524 }
11525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011527}
11528
11529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 * "getfsize({fname})" function
11531 */
11532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011534 typval_T *argvars;
11535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536{
11537 char_u *fname;
11538 struct stat st;
11539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543
11544 if (mch_stat((char *)fname, &st) >= 0)
11545 {
11546 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011551
11552 /* non-perfect check for overflow */
11553 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11554 rettv->vval.v_number = -2;
11555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 }
11557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559}
11560
11561/*
11562 * "getftime({fname})" function
11563 */
11564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 typval_T *argvars;
11567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568{
11569 char_u *fname;
11570 struct stat st;
11571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573
11574 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578}
11579
11580/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011581 * "getftype({fname})" function
11582 */
11583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011587{
11588 char_u *fname;
11589 struct stat st;
11590 char_u *type = NULL;
11591 char *t;
11592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011596 if (mch_lstat((char *)fname, &st) >= 0)
11597 {
11598#ifdef S_ISREG
11599 if (S_ISREG(st.st_mode))
11600 t = "file";
11601 else if (S_ISDIR(st.st_mode))
11602 t = "dir";
11603# ifdef S_ISLNK
11604 else if (S_ISLNK(st.st_mode))
11605 t = "link";
11606# endif
11607# ifdef S_ISBLK
11608 else if (S_ISBLK(st.st_mode))
11609 t = "bdev";
11610# endif
11611# ifdef S_ISCHR
11612 else if (S_ISCHR(st.st_mode))
11613 t = "cdev";
11614# endif
11615# ifdef S_ISFIFO
11616 else if (S_ISFIFO(st.st_mode))
11617 t = "fifo";
11618# endif
11619# ifdef S_ISSOCK
11620 else if (S_ISSOCK(st.st_mode))
11621 t = "fifo";
11622# endif
11623 else
11624 t = "other";
11625#else
11626# ifdef S_IFMT
11627 switch (st.st_mode & S_IFMT)
11628 {
11629 case S_IFREG: t = "file"; break;
11630 case S_IFDIR: t = "dir"; break;
11631# ifdef S_IFLNK
11632 case S_IFLNK: t = "link"; break;
11633# endif
11634# ifdef S_IFBLK
11635 case S_IFBLK: t = "bdev"; break;
11636# endif
11637# ifdef S_IFCHR
11638 case S_IFCHR: t = "cdev"; break;
11639# endif
11640# ifdef S_IFIFO
11641 case S_IFIFO: t = "fifo"; break;
11642# endif
11643# ifdef S_IFSOCK
11644 case S_IFSOCK: t = "socket"; break;
11645# endif
11646 default: t = "other";
11647 }
11648# else
11649 if (mch_isdir(fname))
11650 t = "dir";
11651 else
11652 t = "file";
11653# endif
11654#endif
11655 type = vim_strsave((char_u *)t);
11656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011658}
11659
11660/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011661 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 */
11663 static void
11664f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667{
11668 linenr_T lnum;
11669 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011670 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671
11672 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011673 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011674 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011675 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011676 retlist = FALSE;
11677 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 retlist = TRUE;
11682 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011683
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685}
11686
11687/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011688 * "getmatches()" function
11689 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011690 static void
11691f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011692 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011693 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011694{
11695#ifdef FEAT_SEARCH_EXTRA
11696 dict_T *dict;
11697 matchitem_T *cur = curwin->w_match_head;
11698
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011699 if (rettv_list_alloc(rettv) == OK)
11700 {
11701 while (cur != NULL)
11702 {
11703 dict = dict_alloc();
11704 if (dict == NULL)
11705 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011706 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11707 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11708 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11709 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11710 list_append_dict(rettv->vval.v_list, dict);
11711 cur = cur->next;
11712 }
11713 }
11714#endif
11715}
11716
11717/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011718 * "getpid()" function
11719 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011720 static void
11721f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011722 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011723 typval_T *rettv;
11724{
11725 rettv->vval.v_number = mch_get_pid();
11726}
11727
11728/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011729 * "getpos(string)" function
11730 */
11731 static void
11732f_getpos(argvars, rettv)
11733 typval_T *argvars;
11734 typval_T *rettv;
11735{
11736 pos_T *fp;
11737 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011738 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011739
11740 if (rettv_list_alloc(rettv) == OK)
11741 {
11742 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011743 fp = var2fpos(&argvars[0], TRUE, &fnum);
11744 if (fnum != -1)
11745 list_append_number(l, (varnumber_T)fnum);
11746 else
11747 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011748 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11749 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011750 list_append_number(l, (fp != NULL)
11751 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011752 : (varnumber_T)0);
11753 list_append_number(l,
11754#ifdef FEAT_VIRTUALEDIT
11755 (fp != NULL) ? (varnumber_T)fp->coladd :
11756#endif
11757 (varnumber_T)0);
11758 }
11759 else
11760 rettv->vval.v_number = FALSE;
11761}
11762
11763/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011764 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011765 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011766 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011767f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011768 typval_T *argvars UNUSED;
11769 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011770{
11771#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011772 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011773#endif
11774
Bram Moolenaar2641f772005-03-25 21:58:17 +000011775#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011776 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011777 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011778 wp = NULL;
11779 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11780 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011781 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011782 if (wp == NULL)
11783 return;
11784 }
11785
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011786 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011787 }
11788#endif
11789}
11790
11791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 * "getreg()" function
11793 */
11794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 typval_T *argvars;
11797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
11799 char_u *strregname;
11800 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011801 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 strregname = get_tv_string_chk(&argvars[0]);
11807 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011808 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011812 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 regname = (strregname == NULL ? '"' : *strregname);
11814 if (regname == 0)
11815 regname = '"';
11816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 rettv->vval.v_string = error ? NULL :
11819 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820}
11821
11822/*
11823 * "getregtype()" function
11824 */
11825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *argvars;
11828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829{
11830 char_u *strregname;
11831 int regname;
11832 char_u buf[NUMBUFLEN + 2];
11833 long reglen = 0;
11834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 {
11837 strregname = get_tv_string_chk(&argvars[0]);
11838 if (strregname == NULL) /* type error; errmsg already given */
11839 {
11840 rettv->v_type = VAR_STRING;
11841 rettv->vval.v_string = NULL;
11842 return;
11843 }
11844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 else
11846 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011847 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848
11849 regname = (strregname == NULL ? '"' : *strregname);
11850 if (regname == 0)
11851 regname = '"';
11852
11853 buf[0] = NUL;
11854 buf[1] = NUL;
11855 switch (get_reg_type(regname, &reglen))
11856 {
11857 case MLINE: buf[0] = 'V'; break;
11858 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 case MBLOCK:
11860 buf[0] = Ctrl_V;
11861 sprintf((char *)buf + 1, "%ld", reglen + 1);
11862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->v_type = VAR_STRING;
11865 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011869 * "gettabvar()" function
11870 */
11871 static void
11872f_gettabvar(argvars, rettv)
11873 typval_T *argvars;
11874 typval_T *rettv;
11875{
11876 tabpage_T *tp;
11877 dictitem_T *v;
11878 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011879 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011880
11881 rettv->v_type = VAR_STRING;
11882 rettv->vval.v_string = NULL;
11883
11884 varname = get_tv_string_chk(&argvars[1]);
11885 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11886 if (tp != NULL && varname != NULL)
11887 {
11888 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011889 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011890 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011891 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011892 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011893 done = TRUE;
11894 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011895 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011896
11897 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011898 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011899}
11900
11901/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902 * "gettabwinvar()" function
11903 */
11904 static void
11905f_gettabwinvar(argvars, rettv)
11906 typval_T *argvars;
11907 typval_T *rettv;
11908{
11909 getwinvar(argvars, rettv, 1);
11910}
11911
11912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 * "getwinposx()" function
11914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921#ifdef FEAT_GUI
11922 if (gui.in_use)
11923 {
11924 int x, y;
11925
11926 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 }
11929#endif
11930}
11931
11932/*
11933 * "getwinposy()" function
11934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011937 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941#ifdef FEAT_GUI
11942 if (gui.in_use)
11943 {
11944 int x, y;
11945
11946 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
11949#endif
11950}
11951
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011952/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011953 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011954 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011955 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011956find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011957 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011958 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011959{
11960#ifdef FEAT_WINDOWS
11961 win_T *wp;
11962#endif
11963 int nr;
11964
11965 nr = get_tv_number_chk(vp, NULL);
11966
11967#ifdef FEAT_WINDOWS
11968 if (nr < 0)
11969 return NULL;
11970 if (nr == 0)
11971 return curwin;
11972
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011973 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11974 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011975 if (--nr <= 0)
11976 break;
11977 return wp;
11978#else
11979 if (nr == 0 || nr == 1)
11980 return curwin;
11981 return NULL;
11982#endif
11983}
11984
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985/*
11986 * "getwinvar()" function
11987 */
11988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 typval_T *argvars;
11991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011993 getwinvar(argvars, rettv, 0);
11994}
11995
11996/*
11997 * getwinvar() and gettabwinvar()
11998 */
11999 static void
12000getwinvar(argvars, rettv, off)
12001 typval_T *argvars;
12002 typval_T *rettv;
12003 int off; /* 1 for gettabwinvar() */
12004{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 win_T *win, *oldcurwin;
12006 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012007 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012008 tabpage_T *tp = NULL;
12009 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012010 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012012#ifdef FEAT_WINDOWS
12013 if (off == 1)
12014 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12015 else
12016 tp = curtab;
12017#endif
12018 win = find_win_by_nr(&argvars[off], tp);
12019 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012022 rettv->v_type = VAR_STRING;
12023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025 if (win != NULL && varname != NULL)
12026 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012027 /* Set curwin to be our win, temporarily. Also set the tabpage,
12028 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012029 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012030
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012032 {
12033 if (get_option_tv(&varname, rettv, 1) == OK)
12034 done = TRUE;
12035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 else
12037 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012038 /* Look up the variable. */
12039 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12040 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012044 done = TRUE;
12045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012047
12048 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012049 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 }
12051
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012052 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12053 /* use the default return value */
12054 copy_tv(&argvars[off + 2], rettv);
12055
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 --emsg_off;
12057}
12058
12059/*
12060 * "glob()" function
12061 */
12062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012067 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012069 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012071 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012072 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012074 if (argvars[1].v_type != VAR_UNKNOWN)
12075 {
12076 if (get_tv_number_chk(&argvars[1], &error))
12077 options |= WILD_KEEP_ALL;
12078 if (argvars[2].v_type != VAR_UNKNOWN
12079 && get_tv_number_chk(&argvars[2], &error))
12080 {
12081 rettv->v_type = VAR_LIST;
12082 rettv->vval.v_list = NULL;
12083 }
12084 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012085 if (!error)
12086 {
12087 ExpandInit(&xpc);
12088 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012089 if (p_wic)
12090 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012091 if (rettv->v_type == VAR_STRING)
12092 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012093 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012094 else if (rettv_list_alloc(rettv) != FAIL)
12095 {
12096 int i;
12097
12098 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12099 NULL, options, WILD_ALL_KEEP);
12100 for (i = 0; i < xpc.xp_numfiles; i++)
12101 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12102
12103 ExpandCleanup(&xpc);
12104 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012105 }
12106 else
12107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108}
12109
12110/*
12111 * "globpath()" function
12112 */
12113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012115 typval_T *argvars;
12116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012118 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012120 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012121 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012123 /* When the optional second argument is non-zero, don't remove matches
12124 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12125 if (argvars[2].v_type != VAR_UNKNOWN
12126 && get_tv_number_chk(&argvars[2], &error))
12127 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012129 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130 rettv->vval.v_string = NULL;
12131 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012132 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12133 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134}
12135
12136/*
12137 * "has()" function
12138 */
12139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 typval_T *argvars;
12142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143{
12144 int i;
12145 char_u *name;
12146 int n = FALSE;
12147 static char *(has_list[]) =
12148 {
12149#ifdef AMIGA
12150 "amiga",
12151# ifdef FEAT_ARP
12152 "arp",
12153# endif
12154#endif
12155#ifdef __BEOS__
12156 "beos",
12157#endif
12158#ifdef MSDOS
12159# ifdef DJGPP
12160 "dos32",
12161# else
12162 "dos16",
12163# endif
12164#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012165#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 "mac",
12167#endif
12168#if defined(MACOS_X_UNIX)
12169 "macunix",
12170#endif
12171#ifdef OS2
12172 "os2",
12173#endif
12174#ifdef __QNX__
12175 "qnx",
12176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef UNIX
12178 "unix",
12179#endif
12180#ifdef VMS
12181 "vms",
12182#endif
12183#ifdef WIN16
12184 "win16",
12185#endif
12186#ifdef WIN32
12187 "win32",
12188#endif
12189#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12190 "win32unix",
12191#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012192#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 "win64",
12194#endif
12195#ifdef EBCDIC
12196 "ebcdic",
12197#endif
12198#ifndef CASE_INSENSITIVE_FILENAME
12199 "fname_case",
12200#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012201#ifdef HAVE_ACL
12202 "acl",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_ARABIC
12205 "arabic",
12206#endif
12207#ifdef FEAT_AUTOCMD
12208 "autocmd",
12209#endif
12210#ifdef FEAT_BEVAL
12211 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012212# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12213 "balloon_multiline",
12214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#endif
12216#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12217 "builtin_terms",
12218# ifdef ALL_BUILTIN_TCAPS
12219 "all_builtin_terms",
12220# endif
12221#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012222#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12223 || defined(FEAT_GUI_W32) \
12224 || defined(FEAT_GUI_MOTIF))
12225 "browsefilter",
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#ifdef FEAT_BYTEOFF
12228 "byte_offset",
12229#endif
12230#ifdef FEAT_CINDENT
12231 "cindent",
12232#endif
12233#ifdef FEAT_CLIENTSERVER
12234 "clientserver",
12235#endif
12236#ifdef FEAT_CLIPBOARD
12237 "clipboard",
12238#endif
12239#ifdef FEAT_CMDL_COMPL
12240 "cmdline_compl",
12241#endif
12242#ifdef FEAT_CMDHIST
12243 "cmdline_hist",
12244#endif
12245#ifdef FEAT_COMMENTS
12246 "comments",
12247#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012248#ifdef FEAT_CONCEAL
12249 "conceal",
12250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251#ifdef FEAT_CRYPT
12252 "cryptv",
12253#endif
12254#ifdef FEAT_CSCOPE
12255 "cscope",
12256#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012257#ifdef FEAT_CURSORBIND
12258 "cursorbind",
12259#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012260#ifdef CURSOR_SHAPE
12261 "cursorshape",
12262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263#ifdef DEBUG
12264 "debug",
12265#endif
12266#ifdef FEAT_CON_DIALOG
12267 "dialog_con",
12268#endif
12269#ifdef FEAT_GUI_DIALOG
12270 "dialog_gui",
12271#endif
12272#ifdef FEAT_DIFF
12273 "diff",
12274#endif
12275#ifdef FEAT_DIGRAPHS
12276 "digraphs",
12277#endif
12278#ifdef FEAT_DND
12279 "dnd",
12280#endif
12281#ifdef FEAT_EMACS_TAGS
12282 "emacs_tags",
12283#endif
12284 "eval", /* always present, of course! */
12285#ifdef FEAT_EX_EXTRA
12286 "ex_extra",
12287#endif
12288#ifdef FEAT_SEARCH_EXTRA
12289 "extra_search",
12290#endif
12291#ifdef FEAT_FKMAP
12292 "farsi",
12293#endif
12294#ifdef FEAT_SEARCHPATH
12295 "file_in_path",
12296#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012297#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012298 "filterpipe",
12299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#ifdef FEAT_FIND_ID
12301 "find_in_path",
12302#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012303#ifdef FEAT_FLOAT
12304 "float",
12305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#ifdef FEAT_FOLDING
12307 "folding",
12308#endif
12309#ifdef FEAT_FOOTER
12310 "footer",
12311#endif
12312#if !defined(USE_SYSTEM) && defined(UNIX)
12313 "fork",
12314#endif
12315#ifdef FEAT_GETTEXT
12316 "gettext",
12317#endif
12318#ifdef FEAT_GUI
12319 "gui",
12320#endif
12321#ifdef FEAT_GUI_ATHENA
12322# ifdef FEAT_GUI_NEXTAW
12323 "gui_neXtaw",
12324# else
12325 "gui_athena",
12326# endif
12327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328#ifdef FEAT_GUI_GTK
12329 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012332#ifdef FEAT_GUI_GNOME
12333 "gui_gnome",
12334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#ifdef FEAT_GUI_MAC
12336 "gui_mac",
12337#endif
12338#ifdef FEAT_GUI_MOTIF
12339 "gui_motif",
12340#endif
12341#ifdef FEAT_GUI_PHOTON
12342 "gui_photon",
12343#endif
12344#ifdef FEAT_GUI_W16
12345 "gui_win16",
12346#endif
12347#ifdef FEAT_GUI_W32
12348 "gui_win32",
12349#endif
12350#ifdef FEAT_HANGULIN
12351 "hangul_input",
12352#endif
12353#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12354 "iconv",
12355#endif
12356#ifdef FEAT_INS_EXPAND
12357 "insert_expand",
12358#endif
12359#ifdef FEAT_JUMPLIST
12360 "jumplist",
12361#endif
12362#ifdef FEAT_KEYMAP
12363 "keymap",
12364#endif
12365#ifdef FEAT_LANGMAP
12366 "langmap",
12367#endif
12368#ifdef FEAT_LIBCALL
12369 "libcall",
12370#endif
12371#ifdef FEAT_LINEBREAK
12372 "linebreak",
12373#endif
12374#ifdef FEAT_LISP
12375 "lispindent",
12376#endif
12377#ifdef FEAT_LISTCMDS
12378 "listcmds",
12379#endif
12380#ifdef FEAT_LOCALMAP
12381 "localmap",
12382#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012383#ifdef FEAT_LUA
12384# ifndef DYNAMIC_LUA
12385 "lua",
12386# endif
12387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388#ifdef FEAT_MENU
12389 "menu",
12390#endif
12391#ifdef FEAT_SESSION
12392 "mksession",
12393#endif
12394#ifdef FEAT_MODIFY_FNAME
12395 "modify_fname",
12396#endif
12397#ifdef FEAT_MOUSE
12398 "mouse",
12399#endif
12400#ifdef FEAT_MOUSESHAPE
12401 "mouseshape",
12402#endif
12403#if defined(UNIX) || defined(VMS)
12404# ifdef FEAT_MOUSE_DEC
12405 "mouse_dec",
12406# endif
12407# ifdef FEAT_MOUSE_GPM
12408 "mouse_gpm",
12409# endif
12410# ifdef FEAT_MOUSE_JSB
12411 "mouse_jsbterm",
12412# endif
12413# ifdef FEAT_MOUSE_NET
12414 "mouse_netterm",
12415# endif
12416# ifdef FEAT_MOUSE_PTERM
12417 "mouse_pterm",
12418# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012419# ifdef FEAT_MOUSE_SGR
12420 "mouse_sgr",
12421# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012422# ifdef FEAT_SYSMOUSE
12423 "mouse_sysmouse",
12424# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012425# ifdef FEAT_MOUSE_URXVT
12426 "mouse_urxvt",
12427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428# ifdef FEAT_MOUSE_XTERM
12429 "mouse_xterm",
12430# endif
12431#endif
12432#ifdef FEAT_MBYTE
12433 "multi_byte",
12434#endif
12435#ifdef FEAT_MBYTE_IME
12436 "multi_byte_ime",
12437#endif
12438#ifdef FEAT_MULTI_LANG
12439 "multi_lang",
12440#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012441#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012442#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012443 "mzscheme",
12444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446#ifdef FEAT_OLE
12447 "ole",
12448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#ifdef FEAT_PATH_EXTRA
12450 "path_extra",
12451#endif
12452#ifdef FEAT_PERL
12453#ifndef DYNAMIC_PERL
12454 "perl",
12455#endif
12456#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012457#ifdef FEAT_PERSISTENT_UNDO
12458 "persistent_undo",
12459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460#ifdef FEAT_PYTHON
12461#ifndef DYNAMIC_PYTHON
12462 "python",
12463#endif
12464#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012465#ifdef FEAT_PYTHON3
12466#ifndef DYNAMIC_PYTHON3
12467 "python3",
12468#endif
12469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470#ifdef FEAT_POSTSCRIPT
12471 "postscript",
12472#endif
12473#ifdef FEAT_PRINTER
12474 "printer",
12475#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012476#ifdef FEAT_PROFILE
12477 "profile",
12478#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012479#ifdef FEAT_RELTIME
12480 "reltime",
12481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482#ifdef FEAT_QUICKFIX
12483 "quickfix",
12484#endif
12485#ifdef FEAT_RIGHTLEFT
12486 "rightleft",
12487#endif
12488#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12489 "ruby",
12490#endif
12491#ifdef FEAT_SCROLLBIND
12492 "scrollbind",
12493#endif
12494#ifdef FEAT_CMDL_INFO
12495 "showcmd",
12496 "cmdline_info",
12497#endif
12498#ifdef FEAT_SIGNS
12499 "signs",
12500#endif
12501#ifdef FEAT_SMARTINDENT
12502 "smartindent",
12503#endif
12504#ifdef FEAT_SNIFF
12505 "sniff",
12506#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012507#ifdef STARTUPTIME
12508 "startuptime",
12509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510#ifdef FEAT_STL_OPT
12511 "statusline",
12512#endif
12513#ifdef FEAT_SUN_WORKSHOP
12514 "sun_workshop",
12515#endif
12516#ifdef FEAT_NETBEANS_INTG
12517 "netbeans_intg",
12518#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012519#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012520 "spell",
12521#endif
12522#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 "syntax",
12524#endif
12525#if defined(USE_SYSTEM) || !defined(UNIX)
12526 "system",
12527#endif
12528#ifdef FEAT_TAG_BINS
12529 "tag_binary",
12530#endif
12531#ifdef FEAT_TAG_OLDSTATIC
12532 "tag_old_static",
12533#endif
12534#ifdef FEAT_TAG_ANYWHITE
12535 "tag_any_white",
12536#endif
12537#ifdef FEAT_TCL
12538# ifndef DYNAMIC_TCL
12539 "tcl",
12540# endif
12541#endif
12542#ifdef TERMINFO
12543 "terminfo",
12544#endif
12545#ifdef FEAT_TERMRESPONSE
12546 "termresponse",
12547#endif
12548#ifdef FEAT_TEXTOBJ
12549 "textobjects",
12550#endif
12551#ifdef HAVE_TGETENT
12552 "tgetent",
12553#endif
12554#ifdef FEAT_TITLE
12555 "title",
12556#endif
12557#ifdef FEAT_TOOLBAR
12558 "toolbar",
12559#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012560#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12561 "unnamedplus",
12562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563#ifdef FEAT_USR_CMDS
12564 "user-commands", /* was accidentally included in 5.4 */
12565 "user_commands",
12566#endif
12567#ifdef FEAT_VIMINFO
12568 "viminfo",
12569#endif
12570#ifdef FEAT_VERTSPLIT
12571 "vertsplit",
12572#endif
12573#ifdef FEAT_VIRTUALEDIT
12574 "virtualedit",
12575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577#ifdef FEAT_VISUALEXTRA
12578 "visualextra",
12579#endif
12580#ifdef FEAT_VREPLACE
12581 "vreplace",
12582#endif
12583#ifdef FEAT_WILDIGN
12584 "wildignore",
12585#endif
12586#ifdef FEAT_WILDMENU
12587 "wildmenu",
12588#endif
12589#ifdef FEAT_WINDOWS
12590 "windows",
12591#endif
12592#ifdef FEAT_WAK
12593 "winaltkeys",
12594#endif
12595#ifdef FEAT_WRITEBACKUP
12596 "writebackup",
12597#endif
12598#ifdef FEAT_XIM
12599 "xim",
12600#endif
12601#ifdef FEAT_XFONTSET
12602 "xfontset",
12603#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012604#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012605 "xpm",
12606 "xpm_w32", /* for backward compatibility */
12607#else
12608# if defined(HAVE_XPM)
12609 "xpm",
12610# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612#ifdef USE_XSMP
12613 "xsmp",
12614#endif
12615#ifdef USE_XSMP_INTERACT
12616 "xsmp_interact",
12617#endif
12618#ifdef FEAT_XCLIPBOARD
12619 "xterm_clipboard",
12620#endif
12621#ifdef FEAT_XTERM_SAVE
12622 "xterm_save",
12623#endif
12624#if defined(UNIX) && defined(FEAT_X11)
12625 "X11",
12626#endif
12627 NULL
12628 };
12629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 for (i = 0; has_list[i] != NULL; ++i)
12632 if (STRICMP(name, has_list[i]) == 0)
12633 {
12634 n = TRUE;
12635 break;
12636 }
12637
12638 if (n == FALSE)
12639 {
12640 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012641 {
12642 if (name[5] == '-'
12643 && STRLEN(name) > 11
12644 && vim_isdigit(name[6])
12645 && vim_isdigit(name[8])
12646 && vim_isdigit(name[10]))
12647 {
12648 int major = atoi((char *)name + 6);
12649 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012650
12651 /* Expect "patch-9.9.01234". */
12652 n = (major < VIM_VERSION_MAJOR
12653 || (major == VIM_VERSION_MAJOR
12654 && (minor < VIM_VERSION_MINOR
12655 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012656 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012657 }
12658 else
12659 n = has_patch(atoi((char *)name + 5));
12660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 else if (STRICMP(name, "vim_starting") == 0)
12662 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012663#ifdef FEAT_MBYTE
12664 else if (STRICMP(name, "multi_byte_encoding") == 0)
12665 n = has_mbyte;
12666#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012667#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12668 else if (STRICMP(name, "balloon_multiline") == 0)
12669 n = multiline_balloon_available();
12670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671#ifdef DYNAMIC_TCL
12672 else if (STRICMP(name, "tcl") == 0)
12673 n = tcl_enabled(FALSE);
12674#endif
12675#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12676 else if (STRICMP(name, "iconv") == 0)
12677 n = iconv_enabled(FALSE);
12678#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012679#ifdef DYNAMIC_LUA
12680 else if (STRICMP(name, "lua") == 0)
12681 n = lua_enabled(FALSE);
12682#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012683#ifdef DYNAMIC_MZSCHEME
12684 else if (STRICMP(name, "mzscheme") == 0)
12685 n = mzscheme_enabled(FALSE);
12686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687#ifdef DYNAMIC_RUBY
12688 else if (STRICMP(name, "ruby") == 0)
12689 n = ruby_enabled(FALSE);
12690#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012691#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692#ifdef DYNAMIC_PYTHON
12693 else if (STRICMP(name, "python") == 0)
12694 n = python_enabled(FALSE);
12695#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012696#endif
12697#ifdef FEAT_PYTHON3
12698#ifdef DYNAMIC_PYTHON3
12699 else if (STRICMP(name, "python3") == 0)
12700 n = python3_enabled(FALSE);
12701#endif
12702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703#ifdef DYNAMIC_PERL
12704 else if (STRICMP(name, "perl") == 0)
12705 n = perl_enabled(FALSE);
12706#endif
12707#ifdef FEAT_GUI
12708 else if (STRICMP(name, "gui_running") == 0)
12709 n = (gui.in_use || gui.starting);
12710# ifdef FEAT_GUI_W32
12711 else if (STRICMP(name, "gui_win32s") == 0)
12712 n = gui_is_win32s();
12713# endif
12714# ifdef FEAT_BROWSE
12715 else if (STRICMP(name, "browse") == 0)
12716 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12717# endif
12718#endif
12719#ifdef FEAT_SYN_HL
12720 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012721 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#endif
12723#if defined(WIN3264)
12724 else if (STRICMP(name, "win95") == 0)
12725 n = mch_windows95();
12726#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012727#ifdef FEAT_NETBEANS_INTG
12728 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012729 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 }
12732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734}
12735
12736/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012737 * "has_key()" function
12738 */
12739 static void
12740f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012741 typval_T *argvars;
12742 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012743{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012744 if (argvars[0].v_type != VAR_DICT)
12745 {
12746 EMSG(_(e_dictreq));
12747 return;
12748 }
12749 if (argvars[0].vval.v_dict == NULL)
12750 return;
12751
12752 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012753 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012754}
12755
12756/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012757 * "haslocaldir()" function
12758 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012759 static void
12760f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012761 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012762 typval_T *rettv;
12763{
12764 rettv->vval.v_number = (curwin->w_localdir != NULL);
12765}
12766
12767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 * "hasmapto()" function
12769 */
12770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012772 typval_T *argvars;
12773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774{
12775 char_u *name;
12776 char_u *mode;
12777 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012778 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012781 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 mode = (char_u *)"nvo";
12783 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012786 if (argvars[2].v_type != VAR_UNKNOWN)
12787 abbr = get_tv_number(&argvars[2]);
12788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789
Bram Moolenaar2c932302006-03-18 21:42:09 +000012790 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012793 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794}
12795
12796/*
12797 * "histadd()" function
12798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012801 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803{
12804#ifdef FEAT_CMDHIST
12805 int histype;
12806 char_u *str;
12807 char_u buf[NUMBUFLEN];
12808#endif
12809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 if (check_restricted() || check_secure())
12812 return;
12813#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012814 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12815 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 if (histype >= 0)
12817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 if (*str != NUL)
12820 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012821 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 return;
12825 }
12826 }
12827#endif
12828}
12829
12830/*
12831 * "histdel()" function
12832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012835 typval_T *argvars UNUSED;
12836 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
12838#ifdef FEAT_CMDHIST
12839 int n;
12840 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012841 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12844 if (str == NULL)
12845 n = 0;
12846 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012848 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012849 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012851 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 else
12854 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012855 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 get_tv_string_buf(&argvars[1], buf));
12857 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858#endif
12859}
12860
12861/*
12862 * "histget()" function
12863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012866 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868{
12869#ifdef FEAT_CMDHIST
12870 int type;
12871 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012872 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12875 if (str == NULL)
12876 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 {
12879 type = get_histtype(str);
12880 if (argvars[1].v_type == VAR_UNKNOWN)
12881 idx = get_history_idx(type);
12882 else
12883 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12884 /* -1 on type error */
12885 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891}
12892
12893/*
12894 * "histnr()" function
12895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900{
12901 int i;
12902
12903#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 char_u *history = get_tv_string_chk(&argvars[0]);
12905
12906 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 if (i >= HIST_CMD && i < HIST_COUNT)
12908 i = get_history_idx(i);
12909 else
12910#endif
12911 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913}
12914
12915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 * "highlightID(name)" function
12917 */
12918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924}
12925
12926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012927 * "highlight_exists()" function
12928 */
12929 static void
12930f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012931 typval_T *argvars;
12932 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012933{
12934 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12935}
12936
12937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 * "hostname()" function
12939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012942 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944{
12945 char_u hostname[256];
12946
12947 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950}
12951
12952/*
12953 * iconv() function
12954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959{
12960#ifdef FEAT_MBYTE
12961 char_u buf1[NUMBUFLEN];
12962 char_u buf2[NUMBUFLEN];
12963 char_u *from, *to, *str;
12964 vimconv_T vimconv;
12965#endif
12966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->v_type = VAR_STRING;
12968 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969
12970#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 str = get_tv_string(&argvars[0]);
12972 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12973 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 vimconv.vc_type = CONV_NONE;
12975 convert_setup(&vimconv, from, to);
12976
12977 /* If the encodings are equal, no conversion needed. */
12978 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982
12983 convert_setup(&vimconv, NULL, NULL);
12984 vim_free(from);
12985 vim_free(to);
12986#endif
12987}
12988
12989/*
12990 * "indent()" function
12991 */
12992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012994 typval_T *argvars;
12995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
12997 linenr_T lnum;
12998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004}
13005
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013006/*
13007 * "index()" function
13008 */
13009 static void
13010f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013013{
Bram Moolenaar33570922005-01-25 22:26:29 +000013014 list_T *l;
13015 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013016 long idx = 0;
13017 int ic = FALSE;
13018
13019 rettv->vval.v_number = -1;
13020 if (argvars[0].v_type != VAR_LIST)
13021 {
13022 EMSG(_(e_listreq));
13023 return;
13024 }
13025 l = argvars[0].vval.v_list;
13026 if (l != NULL)
13027 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013028 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 int error = FALSE;
13032
Bram Moolenaar758711c2005-02-02 23:11:38 +000013033 /* Start at specified item. Use the cached index that list_find()
13034 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013036 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013037 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 ic = get_tv_number_chk(&argvars[3], &error);
13039 if (error)
13040 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013041 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013042
Bram Moolenaar758711c2005-02-02 23:11:38 +000013043 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013044 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013045 {
13046 rettv->vval.v_number = idx;
13047 break;
13048 }
13049 }
13050}
13051
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052static int inputsecret_flag = 0;
13053
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013054static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13055
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013057 * This function is used by f_input() and f_inputdialog() functions. The third
13058 * argument to f_input() specifies the type of completion to use at the
13059 * prompt. The third argument to f_inputdialog() specifies the value to return
13060 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 */
13062 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013063get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013064 typval_T *argvars;
13065 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013066 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 char_u *p = NULL;
13070 int c;
13071 char_u buf[NUMBUFLEN];
13072 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013073 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013074 int xp_type = EXPAND_NOTHING;
13075 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013078 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079
13080#ifdef NO_CONSOLE_INPUT
13081 /* While starting up, there is no place to enter text. */
13082 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084#endif
13085
13086 cmd_silent = FALSE; /* Want to see the prompt. */
13087 if (prompt != NULL)
13088 {
13089 /* Only the part of the message after the last NL is considered as
13090 * prompt for the command line */
13091 p = vim_strrchr(prompt, '\n');
13092 if (p == NULL)
13093 p = prompt;
13094 else
13095 {
13096 ++p;
13097 c = *p;
13098 *p = NUL;
13099 msg_start();
13100 msg_clr_eos();
13101 msg_puts_attr(prompt, echo_attr);
13102 msg_didout = FALSE;
13103 msg_starthere();
13104 *p = c;
13105 }
13106 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 if (argvars[1].v_type != VAR_UNKNOWN)
13109 {
13110 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13111 if (defstr != NULL)
13112 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013114 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013115 {
13116 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013117 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013118 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013119
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013120 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013121 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013122
Bram Moolenaar4463f292005-09-25 22:20:24 +000013123 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13124 if (xp_name == NULL)
13125 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013126
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013127 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013128
Bram Moolenaar4463f292005-09-25 22:20:24 +000013129 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13130 &xp_arg) == FAIL)
13131 return;
13132 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013133 }
13134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013135 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013136 {
13137# ifdef FEAT_EX_EXTRA
13138 int save_ex_normal_busy = ex_normal_busy;
13139 ex_normal_busy = 0;
13140# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013142 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13143 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013144# ifdef FEAT_EX_EXTRA
13145 ex_normal_busy = save_ex_normal_busy;
13146# endif
13147 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013148 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013149 && argvars[1].v_type != VAR_UNKNOWN
13150 && argvars[2].v_type != VAR_UNKNOWN)
13151 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13152 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013153
13154 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013156 /* since the user typed this, no need to wait for return */
13157 need_wait_return = FALSE;
13158 msg_didout = FALSE;
13159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 cmd_silent = cmd_silent_save;
13161}
13162
13163/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013164 * "input()" function
13165 * Also handles inputsecret() when inputsecret is set.
13166 */
13167 static void
13168f_input(argvars, rettv)
13169 typval_T *argvars;
13170 typval_T *rettv;
13171{
13172 get_user_input(argvars, rettv, FALSE);
13173}
13174
13175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 * "inputdialog()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
13183#if defined(FEAT_GUI_TEXTDIALOG)
13184 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13185 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13186 {
13187 char_u *message;
13188 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191 message = get_tv_string_chk(&argvars[0]);
13192 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013193 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013194 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 else
13196 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 if (message != NULL && defstr != NULL
13198 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013199 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013200 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 else
13202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 if (message != NULL && defstr != NULL
13204 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 rettv->vval.v_string = vim_strsave(
13207 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 }
13213 else
13214#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013215 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216}
13217
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013218/*
13219 * "inputlist()" function
13220 */
13221 static void
13222f_inputlist(argvars, rettv)
13223 typval_T *argvars;
13224 typval_T *rettv;
13225{
13226 listitem_T *li;
13227 int selected;
13228 int mouse_used;
13229
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013230#ifdef NO_CONSOLE_INPUT
13231 /* While starting up, there is no place to enter text. */
13232 if (no_console_input())
13233 return;
13234#endif
13235 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13236 {
13237 EMSG2(_(e_listarg), "inputlist()");
13238 return;
13239 }
13240
13241 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013242 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013243 lines_left = Rows; /* avoid more prompt */
13244 msg_scroll = TRUE;
13245 msg_clr_eos();
13246
13247 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13248 {
13249 msg_puts(get_tv_string(&li->li_tv));
13250 msg_putchar('\n');
13251 }
13252
13253 /* Ask for choice. */
13254 selected = prompt_for_number(&mouse_used);
13255 if (mouse_used)
13256 selected -= lines_left;
13257
13258 rettv->vval.v_number = selected;
13259}
13260
13261
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13263
13264/*
13265 * "inputrestore()" function
13266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271{
13272 if (ga_userinput.ga_len > 0)
13273 {
13274 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13276 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013277 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 }
13279 else if (p_verbose > 1)
13280 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013281 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 }
13284}
13285
13286/*
13287 * "inputsave()" function
13288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013291 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013294 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 if (ga_grow(&ga_userinput, 1) == OK)
13296 {
13297 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13298 + ga_userinput.ga_len);
13299 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013300 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 }
13302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304}
13305
13306/*
13307 * "inputsecret()" function
13308 */
13309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *argvars;
13312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313{
13314 ++cmdline_star;
13315 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 --cmdline_star;
13318 --inputsecret_flag;
13319}
13320
13321/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013322 * "insert()" function
13323 */
13324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013328{
13329 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 listitem_T *item;
13331 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013333
13334 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013335 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013336 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013337 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013338 {
13339 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 before = get_tv_number_chk(&argvars[2], &error);
13341 if (error)
13342 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013343
Bram Moolenaar758711c2005-02-02 23:11:38 +000013344 if (before == l->lv_len)
13345 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013346 else
13347 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013348 item = list_find(l, before);
13349 if (item == NULL)
13350 {
13351 EMSGN(_(e_listidx), before);
13352 l = NULL;
13353 }
13354 }
13355 if (l != NULL)
13356 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013357 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013358 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013359 }
13360 }
13361}
13362
13363/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013364 * "invert(expr)" function
13365 */
13366 static void
13367f_invert(argvars, rettv)
13368 typval_T *argvars;
13369 typval_T *rettv;
13370{
13371 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13372}
13373
13374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 * "isdirectory()" function
13376 */
13377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013378f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013379 typval_T *argvars;
13380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013382 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383}
13384
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013385/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013386 * "islocked()" function
13387 */
13388 static void
13389f_islocked(argvars, rettv)
13390 typval_T *argvars;
13391 typval_T *rettv;
13392{
13393 lval_T lv;
13394 char_u *end;
13395 dictitem_T *di;
13396
13397 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013398 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13399 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013400 if (end != NULL && lv.ll_name != NULL)
13401 {
13402 if (*end != NUL)
13403 EMSG(_(e_trailing));
13404 else
13405 {
13406 if (lv.ll_tv == NULL)
13407 {
13408 if (check_changedtick(lv.ll_name))
13409 rettv->vval.v_number = 1; /* always locked */
13410 else
13411 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013412 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013413 if (di != NULL)
13414 {
13415 /* Consider a variable locked when:
13416 * 1. the variable itself is locked
13417 * 2. the value of the variable is locked.
13418 * 3. the List or Dict value is locked.
13419 */
13420 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13421 || tv_islocked(&di->di_tv));
13422 }
13423 }
13424 }
13425 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013426 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013427 else if (lv.ll_newkey != NULL)
13428 EMSG2(_(e_dictkey), lv.ll_newkey);
13429 else if (lv.ll_list != NULL)
13430 /* List item. */
13431 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13432 else
13433 /* Dictionary item. */
13434 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13435 }
13436 }
13437
13438 clear_lval(&lv);
13439}
13440
Bram Moolenaar33570922005-01-25 22:26:29 +000013441static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013442
13443/*
13444 * Turn a dict into a list:
13445 * "what" == 0: list of keys
13446 * "what" == 1: list of values
13447 * "what" == 2: list of items
13448 */
13449 static void
13450dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013453 int what;
13454{
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 list_T *l2;
13456 dictitem_T *di;
13457 hashitem_T *hi;
13458 listitem_T *li;
13459 listitem_T *li2;
13460 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013461 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013462
Bram Moolenaar8c711452005-01-14 21:53:12 +000013463 if (argvars[0].v_type != VAR_DICT)
13464 {
13465 EMSG(_(e_dictreq));
13466 return;
13467 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013468 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013469 return;
13470
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013471 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013472 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013473
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013474 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013479 --todo;
13480 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013481
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013482 li = listitem_alloc();
13483 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013484 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013485 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013486
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013487 if (what == 0)
13488 {
13489 /* keys() */
13490 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013491 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013492 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13493 }
13494 else if (what == 1)
13495 {
13496 /* values() */
13497 copy_tv(&di->di_tv, &li->li_tv);
13498 }
13499 else
13500 {
13501 /* items() */
13502 l2 = list_alloc();
13503 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013504 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013505 li->li_tv.vval.v_list = l2;
13506 if (l2 == NULL)
13507 break;
13508 ++l2->lv_refcount;
13509
13510 li2 = listitem_alloc();
13511 if (li2 == NULL)
13512 break;
13513 list_append(l2, li2);
13514 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013515 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013516 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13517
13518 li2 = listitem_alloc();
13519 if (li2 == NULL)
13520 break;
13521 list_append(l2, li2);
13522 copy_tv(&di->di_tv, &li2->li_tv);
13523 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013524 }
13525 }
13526}
13527
13528/*
13529 * "items(dict)" function
13530 */
13531 static void
13532f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013533 typval_T *argvars;
13534 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013535{
13536 dict_list(argvars, rettv, 2);
13537}
13538
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013540 * "join()" function
13541 */
13542 static void
13543f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *argvars;
13545 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013546{
13547 garray_T ga;
13548 char_u *sep;
13549
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013550 if (argvars[0].v_type != VAR_LIST)
13551 {
13552 EMSG(_(e_listreq));
13553 return;
13554 }
13555 if (argvars[0].vval.v_list == NULL)
13556 return;
13557 if (argvars[1].v_type == VAR_UNKNOWN)
13558 sep = (char_u *)" ";
13559 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013560 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013561
13562 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013563
13564 if (sep != NULL)
13565 {
13566 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013567 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 ga_append(&ga, NUL);
13569 rettv->vval.v_string = (char_u *)ga.ga_data;
13570 }
13571 else
13572 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573}
13574
13575/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013576 * "keys()" function
13577 */
13578 static void
13579f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013580 typval_T *argvars;
13581 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013582{
13583 dict_list(argvars, rettv, 0);
13584}
13585
13586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 * "last_buffer_nr()" function.
13588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
13594 int n = 0;
13595 buf_T *buf;
13596
13597 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13598 if (n < buf->b_fnum)
13599 n = buf->b_fnum;
13600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013602}
13603
13604/*
13605 * "len()" function
13606 */
13607 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013609 typval_T *argvars;
13610 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013611{
13612 switch (argvars[0].v_type)
13613 {
13614 case VAR_STRING:
13615 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013616 rettv->vval.v_number = (varnumber_T)STRLEN(
13617 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013618 break;
13619 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013621 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013622 case VAR_DICT:
13623 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13624 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013626 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013627 break;
13628 }
13629}
13630
Bram Moolenaar33570922005-01-25 22:26:29 +000013631static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013632
13633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013637 int type;
13638{
13639#ifdef FEAT_LIBCALL
13640 char_u *string_in;
13641 char_u **string_result;
13642 int nr_result;
13643#endif
13644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013646 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013647 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013648
13649 if (check_restricted() || check_secure())
13650 return;
13651
13652#ifdef FEAT_LIBCALL
13653 /* The first two args must be strings, otherwise its meaningless */
13654 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13655 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013656 string_in = NULL;
13657 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013658 string_in = argvars[2].vval.v_string;
13659 if (type == VAR_NUMBER)
13660 string_result = NULL;
13661 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013663 if (mch_libcall(argvars[0].vval.v_string,
13664 argvars[1].vval.v_string,
13665 string_in,
13666 argvars[2].vval.v_number,
13667 string_result,
13668 &nr_result) == OK
13669 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013671 }
13672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673}
13674
13675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013676 * "libcall()" function
13677 */
13678 static void
13679f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *argvars;
13681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013682{
13683 libcall_common(argvars, rettv, VAR_STRING);
13684}
13685
13686/*
13687 * "libcallnr()" function
13688 */
13689 static void
13690f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *argvars;
13692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013693{
13694 libcall_common(argvars, rettv, VAR_NUMBER);
13695}
13696
13697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 * "line(string)" function
13699 */
13700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 typval_T *argvars;
13703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704{
13705 linenr_T lnum = 0;
13706 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013707 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013709 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 if (fp != NULL)
13711 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713}
13714
13715/*
13716 * "line2byte(lnum)" function
13717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
13723#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725#else
13726 linenr_T lnum;
13727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13733 if (rettv->vval.v_number >= 0)
13734 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735#endif
13736}
13737
13738/*
13739 * "lispindent(lnum)" function
13740 */
13741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013742f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013743 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745{
13746#ifdef FEAT_LISP
13747 pos_T pos;
13748 linenr_T lnum;
13749
13750 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13753 {
13754 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756 curwin->w_cursor = pos;
13757 }
13758 else
13759#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761}
13762
13763/*
13764 * "localtime()" function
13765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013767f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013768 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013771 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772}
13773
Bram Moolenaar33570922005-01-25 22:26:29 +000013774static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775
13776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013777get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *argvars;
13779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 int exact;
13781{
13782 char_u *keys;
13783 char_u *which;
13784 char_u buf[NUMBUFLEN];
13785 char_u *keys_buf = NULL;
13786 char_u *rhs;
13787 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013788 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013789 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013790 mapblock_T *mp;
13791 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792
13793 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794 rettv->v_type = VAR_STRING;
13795 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 if (*keys == NUL)
13799 return;
13800
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013801 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013805 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013806 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013807 if (argvars[3].v_type != VAR_UNKNOWN)
13808 get_dict = get_tv_number(&argvars[3]);
13809 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 else
13812 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013813 if (which == NULL)
13814 return;
13815
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816 mode = get_map_mode(&which, 0);
13817
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013818 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013819 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013821
13822 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013824 /* Return a string. */
13825 if (rhs != NULL)
13826 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaarbd743252010-10-20 21:23:33 +020013828 }
13829 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13830 {
13831 /* Return a dictionary. */
13832 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13833 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13834 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835
Bram Moolenaarbd743252010-10-20 21:23:33 +020013836 dict_add_nr_str(dict, "lhs", 0L, lhs);
13837 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13838 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13839 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13840 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13841 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13842 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013843 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013844 dict_add_nr_str(dict, "mode", 0L, mapmode);
13845
13846 vim_free(lhs);
13847 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 }
13849}
13850
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013851#ifdef FEAT_FLOAT
13852/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013853 * "log()" function
13854 */
13855 static void
13856f_log(argvars, rettv)
13857 typval_T *argvars;
13858 typval_T *rettv;
13859{
13860 float_T f;
13861
13862 rettv->v_type = VAR_FLOAT;
13863 if (get_float_arg(argvars, &f) == OK)
13864 rettv->vval.v_float = log(f);
13865 else
13866 rettv->vval.v_float = 0.0;
13867}
13868
13869/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870 * "log10()" function
13871 */
13872 static void
13873f_log10(argvars, rettv)
13874 typval_T *argvars;
13875 typval_T *rettv;
13876{
13877 float_T f;
13878
13879 rettv->v_type = VAR_FLOAT;
13880 if (get_float_arg(argvars, &f) == OK)
13881 rettv->vval.v_float = log10(f);
13882 else
13883 rettv->vval.v_float = 0.0;
13884}
13885#endif
13886
Bram Moolenaar1dced572012-04-05 16:54:08 +020013887#ifdef FEAT_LUA
13888/*
13889 * "luaeval()" function
13890 */
13891 static void
13892f_luaeval(argvars, rettv)
13893 typval_T *argvars;
13894 typval_T *rettv;
13895{
13896 char_u *str;
13897 char_u buf[NUMBUFLEN];
13898
13899 str = get_tv_string_buf(&argvars[0], buf);
13900 do_luaeval(str, argvars + 1, rettv);
13901}
13902#endif
13903
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 * "map()" function
13906 */
13907 static void
13908f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013909 typval_T *argvars;
13910 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013911{
13912 filter_map(argvars, rettv, TRUE);
13913}
13914
13915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013916 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 */
13918 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013919f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013920 typval_T *argvars;
13921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013923 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924}
13925
13926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013927 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 */
13929 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013930f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013931 typval_T *argvars;
13932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013934 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935}
13936
Bram Moolenaar33570922005-01-25 22:26:29 +000013937static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938
13939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013941 typval_T *argvars;
13942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 int type;
13944{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013946 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013947 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 char_u *pat;
13949 regmatch_T regmatch;
13950 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 char_u *save_cpo;
13953 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013954 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013955 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013956 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013957 list_T *l = NULL;
13958 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013959 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013960 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961
13962 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13963 save_cpo = p_cpo;
13964 p_cpo = (char_u *)"";
13965
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013966 rettv->vval.v_number = -1;
13967 if (type == 3)
13968 {
13969 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013970 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013971 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013972 }
13973 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->v_type = VAR_STRING;
13976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013979 if (argvars[0].v_type == VAR_LIST)
13980 {
13981 if ((l = argvars[0].vval.v_list) == NULL)
13982 goto theend;
13983 li = l->lv_first;
13984 }
13985 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013986 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013987 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013988 len = (long)STRLEN(str);
13989 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013990
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13992 if (pat == NULL)
13993 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013994
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013995 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 int error = FALSE;
13998
13999 start = get_tv_number_chk(&argvars[2], &error);
14000 if (error)
14001 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 if (l != NULL)
14003 {
14004 li = list_find(l, start);
14005 if (li == NULL)
14006 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014007 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008 }
14009 else
14010 {
14011 if (start < 0)
14012 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014013 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014014 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014015 /* When "count" argument is there ignore matches before "start",
14016 * otherwise skip part of the string. Differs when pattern is "^"
14017 * or "\<". */
14018 if (argvars[3].v_type != VAR_UNKNOWN)
14019 startcol = start;
14020 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014021 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014022 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014023 len -= start;
14024 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014027 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014028 nth = get_tv_number_chk(&argvars[3], &error);
14029 if (error)
14030 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 }
14032
14033 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14034 if (regmatch.regprog != NULL)
14035 {
14036 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014037
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014038 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014039 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014040 if (l != NULL)
14041 {
14042 if (li == NULL)
14043 {
14044 match = FALSE;
14045 break;
14046 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014047 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014048 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014049 if (str == NULL)
14050 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014051 }
14052
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014053 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014054
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014055 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014056 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014057 if (l == NULL && !match)
14058 break;
14059
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014060 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014061 if (l != NULL)
14062 {
14063 li = li->li_next;
14064 ++idx;
14065 }
14066 else
14067 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014068#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014069 startcol = (colnr_T)(regmatch.startp[0]
14070 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014071#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014072 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014073#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014074 if (startcol > (colnr_T)len
14075 || str + startcol <= regmatch.startp[0])
14076 {
14077 match = FALSE;
14078 break;
14079 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014081 }
14082
14083 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014085 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014086 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014087 int i;
14088
14089 /* return list with matched string and submatches */
14090 for (i = 0; i < NSUBEXP; ++i)
14091 {
14092 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014093 {
14094 if (list_append_string(rettv->vval.v_list,
14095 (char_u *)"", 0) == FAIL)
14096 break;
14097 }
14098 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014099 regmatch.startp[i],
14100 (int)(regmatch.endp[i] - regmatch.startp[i]))
14101 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014102 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014103 }
14104 }
14105 else if (type == 2)
14106 {
14107 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014108 if (l != NULL)
14109 copy_tv(&li->li_tv, rettv);
14110 else
14111 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014113 }
14114 else if (l != NULL)
14115 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 else
14117 {
14118 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 (varnumber_T)(regmatch.startp[0] - str);
14121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014124 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 }
14126 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014127 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 }
14129
14130theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014131 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 p_cpo = save_cpo;
14133}
14134
14135/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136 * "match()" function
14137 */
14138 static void
14139f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 typval_T *argvars;
14141 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014142{
14143 find_some_match(argvars, rettv, 1);
14144}
14145
14146/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014147 * "matchadd()" function
14148 */
14149 static void
14150f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014151 typval_T *argvars UNUSED;
14152 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014153{
14154#ifdef FEAT_SEARCH_EXTRA
14155 char_u buf[NUMBUFLEN];
14156 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14157 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14158 int prio = 10; /* default priority */
14159 int id = -1;
14160 int error = FALSE;
14161
14162 rettv->vval.v_number = -1;
14163
14164 if (grp == NULL || pat == NULL)
14165 return;
14166 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014168 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014169 if (argvars[3].v_type != VAR_UNKNOWN)
14170 id = get_tv_number_chk(&argvars[3], &error);
14171 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014172 if (error == TRUE)
14173 return;
14174 if (id >= 1 && id <= 3)
14175 {
14176 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14177 return;
14178 }
14179
14180 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14181#endif
14182}
14183
14184/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014185 * "matcharg()" function
14186 */
14187 static void
14188f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014189 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014190 typval_T *rettv;
14191{
14192 if (rettv_list_alloc(rettv) == OK)
14193 {
14194#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014195 int id = get_tv_number(&argvars[0]);
14196 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014197
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014198 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014199 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014200 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14201 {
14202 list_append_string(rettv->vval.v_list,
14203 syn_id2name(m->hlg_id), -1);
14204 list_append_string(rettv->vval.v_list, m->pattern, -1);
14205 }
14206 else
14207 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014208 list_append_string(rettv->vval.v_list, NULL, -1);
14209 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014210 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014211 }
14212#endif
14213 }
14214}
14215
14216/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014217 * "matchdelete()" function
14218 */
14219 static void
14220f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014221 typval_T *argvars UNUSED;
14222 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014223{
14224#ifdef FEAT_SEARCH_EXTRA
14225 rettv->vval.v_number = match_delete(curwin,
14226 (int)get_tv_number(&argvars[0]), TRUE);
14227#endif
14228}
14229
14230/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014231 * "matchend()" function
14232 */
14233 static void
14234f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 typval_T *argvars;
14236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014237{
14238 find_some_match(argvars, rettv, 0);
14239}
14240
14241/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014242 * "matchlist()" function
14243 */
14244 static void
14245f_matchlist(argvars, rettv)
14246 typval_T *argvars;
14247 typval_T *rettv;
14248{
14249 find_some_match(argvars, rettv, 3);
14250}
14251
14252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014253 * "matchstr()" function
14254 */
14255 static void
14256f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014257 typval_T *argvars;
14258 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259{
14260 find_some_match(argvars, rettv, 2);
14261}
14262
Bram Moolenaar33570922005-01-25 22:26:29 +000014263static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014264
14265 static void
14266max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014267 typval_T *argvars;
14268 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014269 int domax;
14270{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014271 long n = 0;
14272 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014274
14275 if (argvars[0].v_type == VAR_LIST)
14276 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 list_T *l;
14278 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014279
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014280 l = argvars[0].vval.v_list;
14281 if (l != NULL)
14282 {
14283 li = l->lv_first;
14284 if (li != NULL)
14285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014287 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014288 {
14289 li = li->li_next;
14290 if (li == NULL)
14291 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014293 if (domax ? i > n : i < n)
14294 n = i;
14295 }
14296 }
14297 }
14298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014299 else if (argvars[0].v_type == VAR_DICT)
14300 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014302 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014303 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014304 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014305
14306 d = argvars[0].vval.v_dict;
14307 if (d != NULL)
14308 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014309 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014310 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014311 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014312 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014313 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014314 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014316 if (first)
14317 {
14318 n = i;
14319 first = FALSE;
14320 }
14321 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014322 n = i;
14323 }
14324 }
14325 }
14326 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014327 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014328 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014330}
14331
14332/*
14333 * "max()" function
14334 */
14335 static void
14336f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 typval_T *argvars;
14338 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014339{
14340 max_min(argvars, rettv, TRUE);
14341}
14342
14343/*
14344 * "min()" function
14345 */
14346 static void
14347f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014348 typval_T *argvars;
14349 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014350{
14351 max_min(argvars, rettv, FALSE);
14352}
14353
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014354static int mkdir_recurse __ARGS((char_u *dir, int prot));
14355
14356/*
14357 * Create the directory in which "dir" is located, and higher levels when
14358 * needed.
14359 */
14360 static int
14361mkdir_recurse(dir, prot)
14362 char_u *dir;
14363 int prot;
14364{
14365 char_u *p;
14366 char_u *updir;
14367 int r = FAIL;
14368
14369 /* Get end of directory name in "dir".
14370 * We're done when it's "/" or "c:/". */
14371 p = gettail_sep(dir);
14372 if (p <= get_past_head(dir))
14373 return OK;
14374
14375 /* If the directory exists we're done. Otherwise: create it.*/
14376 updir = vim_strnsave(dir, (int)(p - dir));
14377 if (updir == NULL)
14378 return FAIL;
14379 if (mch_isdir(updir))
14380 r = OK;
14381 else if (mkdir_recurse(updir, prot) == OK)
14382 r = vim_mkdir_emsg(updir, prot);
14383 vim_free(updir);
14384 return r;
14385}
14386
14387#ifdef vim_mkdir
14388/*
14389 * "mkdir()" function
14390 */
14391 static void
14392f_mkdir(argvars, rettv)
14393 typval_T *argvars;
14394 typval_T *rettv;
14395{
14396 char_u *dir;
14397 char_u buf[NUMBUFLEN];
14398 int prot = 0755;
14399
14400 rettv->vval.v_number = FAIL;
14401 if (check_restricted() || check_secure())
14402 return;
14403
14404 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014405 if (*dir == NUL)
14406 rettv->vval.v_number = FAIL;
14407 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014408 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014409 if (*gettail(dir) == NUL)
14410 /* remove trailing slashes */
14411 *gettail_sep(dir) = NUL;
14412
14413 if (argvars[1].v_type != VAR_UNKNOWN)
14414 {
14415 if (argvars[2].v_type != VAR_UNKNOWN)
14416 prot = get_tv_number_chk(&argvars[2], NULL);
14417 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14418 mkdir_recurse(dir, prot);
14419 }
14420 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014421 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014422}
14423#endif
14424
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 * "mode()" function
14427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014433 char_u buf[3];
14434
14435 buf[1] = NUL;
14436 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 if (VIsual_active)
14439 {
14440 if (VIsual_select)
14441 buf[0] = VIsual_mode + 's' - 'v';
14442 else
14443 buf[0] = VIsual_mode;
14444 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014445 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014446 || State == CONFIRM)
14447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014449 if (State == ASKMORE)
14450 buf[1] = 'm';
14451 else if (State == CONFIRM)
14452 buf[1] = '?';
14453 }
14454 else if (State == EXTERNCMD)
14455 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 else if (State & INSERT)
14457 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014458#ifdef FEAT_VREPLACE
14459 if (State & VREPLACE_FLAG)
14460 {
14461 buf[0] = 'R';
14462 buf[1] = 'v';
14463 }
14464 else
14465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466 if (State & REPLACE_FLAG)
14467 buf[0] = 'R';
14468 else
14469 buf[0] = 'i';
14470 }
14471 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014474 if (exmode_active)
14475 buf[1] = 'v';
14476 }
14477 else if (exmode_active)
14478 {
14479 buf[0] = 'c';
14480 buf[1] = 'e';
14481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014485 if (finish_op)
14486 buf[1] = 'o';
14487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014489 /* Clear out the minor mode when the argument is not a non-zero number or
14490 * non-empty string. */
14491 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014492 buf[1] = NUL;
14493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_string = vim_strsave(buf);
14495 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496}
14497
Bram Moolenaar429fa852013-04-15 12:27:36 +020014498#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014499/*
14500 * "mzeval()" function
14501 */
14502 static void
14503f_mzeval(argvars, rettv)
14504 typval_T *argvars;
14505 typval_T *rettv;
14506{
14507 char_u *str;
14508 char_u buf[NUMBUFLEN];
14509
14510 str = get_tv_string_buf(&argvars[0], buf);
14511 do_mzeval(str, rettv);
14512}
Bram Moolenaar75676462013-01-30 14:55:42 +010014513
14514 void
14515mzscheme_call_vim(name, args, rettv)
14516 char_u *name;
14517 typval_T *args;
14518 typval_T *rettv;
14519{
14520 typval_T argvars[3];
14521
14522 argvars[0].v_type = VAR_STRING;
14523 argvars[0].vval.v_string = name;
14524 copy_tv(args, &argvars[1]);
14525 argvars[2].v_type = VAR_UNKNOWN;
14526 f_call(argvars, rettv);
14527 clear_tv(&argvars[1]);
14528}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014529#endif
14530
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014532 * "nextnonblank()" function
14533 */
14534 static void
14535f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014536 typval_T *argvars;
14537 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538{
14539 linenr_T lnum;
14540
14541 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 {
14545 lnum = 0;
14546 break;
14547 }
14548 if (*skipwhite(ml_get(lnum)) != NUL)
14549 break;
14550 }
14551 rettv->vval.v_number = lnum;
14552}
14553
14554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 * "nr2char()" function
14556 */
14557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014559 typval_T *argvars;
14560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
14562 char_u buf[NUMBUFLEN];
14563
14564#ifdef FEAT_MBYTE
14565 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014566 {
14567 int utf8 = 0;
14568
14569 if (argvars[1].v_type != VAR_UNKNOWN)
14570 utf8 = get_tv_number_chk(&argvars[1], NULL);
14571 if (utf8)
14572 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14573 else
14574 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 else
14577#endif
14578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 buf[1] = NUL;
14581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 rettv->v_type = VAR_STRING;
14583 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014584}
14585
14586/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014587 * "or(expr, expr)" function
14588 */
14589 static void
14590f_or(argvars, rettv)
14591 typval_T *argvars;
14592 typval_T *rettv;
14593{
14594 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14595 | get_tv_number_chk(&argvars[1], NULL);
14596}
14597
14598/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014599 * "pathshorten()" function
14600 */
14601 static void
14602f_pathshorten(argvars, rettv)
14603 typval_T *argvars;
14604 typval_T *rettv;
14605{
14606 char_u *p;
14607
14608 rettv->v_type = VAR_STRING;
14609 p = get_tv_string_chk(&argvars[0]);
14610 if (p == NULL)
14611 rettv->vval.v_string = NULL;
14612 else
14613 {
14614 p = vim_strsave(p);
14615 rettv->vval.v_string = p;
14616 if (p != NULL)
14617 shorten_dir(p);
14618 }
14619}
14620
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014621#ifdef FEAT_FLOAT
14622/*
14623 * "pow()" function
14624 */
14625 static void
14626f_pow(argvars, rettv)
14627 typval_T *argvars;
14628 typval_T *rettv;
14629{
14630 float_T fx, fy;
14631
14632 rettv->v_type = VAR_FLOAT;
14633 if (get_float_arg(argvars, &fx) == OK
14634 && get_float_arg(&argvars[1], &fy) == OK)
14635 rettv->vval.v_float = pow(fx, fy);
14636 else
14637 rettv->vval.v_float = 0.0;
14638}
14639#endif
14640
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014641/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642 * "prevnonblank()" function
14643 */
14644 static void
14645f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014646 typval_T *argvars;
14647 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648{
14649 linenr_T lnum;
14650
14651 lnum = get_tv_lnum(argvars);
14652 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14653 lnum = 0;
14654 else
14655 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14656 --lnum;
14657 rettv->vval.v_number = lnum;
14658}
14659
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014660#ifdef HAVE_STDARG_H
14661/* This dummy va_list is here because:
14662 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14663 * - locally in the function results in a "used before set" warning
14664 * - using va_start() to initialize it gives "function with fixed args" error */
14665static va_list ap;
14666#endif
14667
Bram Moolenaar8c711452005-01-14 21:53:12 +000014668/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014669 * "printf()" function
14670 */
14671 static void
14672f_printf(argvars, rettv)
14673 typval_T *argvars;
14674 typval_T *rettv;
14675{
14676 rettv->v_type = VAR_STRING;
14677 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014678#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014679 {
14680 char_u buf[NUMBUFLEN];
14681 int len;
14682 char_u *s;
14683 int saved_did_emsg = did_emsg;
14684 char *fmt;
14685
14686 /* Get the required length, allocate the buffer and do it for real. */
14687 did_emsg = FALSE;
14688 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014689 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014690 if (!did_emsg)
14691 {
14692 s = alloc(len + 1);
14693 if (s != NULL)
14694 {
14695 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014696 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014697 }
14698 }
14699 did_emsg |= saved_did_emsg;
14700 }
14701#endif
14702}
14703
14704/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014705 * "pumvisible()" function
14706 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014707 static void
14708f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014709 typval_T *argvars UNUSED;
14710 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014711{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014712#ifdef FEAT_INS_EXPAND
14713 if (pum_visible())
14714 rettv->vval.v_number = 1;
14715#endif
14716}
14717
Bram Moolenaardb913952012-06-29 12:54:53 +020014718#ifdef FEAT_PYTHON3
14719/*
14720 * "py3eval()" function
14721 */
14722 static void
14723f_py3eval(argvars, rettv)
14724 typval_T *argvars;
14725 typval_T *rettv;
14726{
14727 char_u *str;
14728 char_u buf[NUMBUFLEN];
14729
14730 str = get_tv_string_buf(&argvars[0], buf);
14731 do_py3eval(str, rettv);
14732}
14733#endif
14734
14735#ifdef FEAT_PYTHON
14736/*
14737 * "pyeval()" function
14738 */
14739 static void
14740f_pyeval(argvars, rettv)
14741 typval_T *argvars;
14742 typval_T *rettv;
14743{
14744 char_u *str;
14745 char_u buf[NUMBUFLEN];
14746
14747 str = get_tv_string_buf(&argvars[0], buf);
14748 do_pyeval(str, rettv);
14749}
14750#endif
14751
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753 * "range()" function
14754 */
14755 static void
14756f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014757 typval_T *argvars;
14758 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014759{
14760 long start;
14761 long end;
14762 long stride = 1;
14763 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014766 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014767 if (argvars[1].v_type == VAR_UNKNOWN)
14768 {
14769 end = start - 1;
14770 start = 0;
14771 }
14772 else
14773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014775 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014777 }
14778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779 if (error)
14780 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014781 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014782 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014783 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014784 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785 else
14786 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014787 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014788 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014789 if (list_append_number(rettv->vval.v_list,
14790 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014791 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014792 }
14793}
14794
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014795/*
14796 * "readfile()" function
14797 */
14798 static void
14799f_readfile(argvars, rettv)
14800 typval_T *argvars;
14801 typval_T *rettv;
14802{
14803 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014804 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014805 char_u *fname;
14806 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014807 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14808 int io_size = sizeof(buf);
14809 int readlen; /* size of last fread() */
14810 char_u *prev = NULL; /* previously read bytes, if any */
14811 long prevlen = 0; /* length of data in prev */
14812 long prevsize = 0; /* size of prev buffer */
14813 long maxline = MAXLNUM;
14814 long cnt = 0;
14815 char_u *p; /* position in buf */
14816 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014817
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014818 if (argvars[1].v_type != VAR_UNKNOWN)
14819 {
14820 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14821 binary = TRUE;
14822 if (argvars[2].v_type != VAR_UNKNOWN)
14823 maxline = get_tv_number(&argvars[2]);
14824 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014826 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014828
14829 /* Always open the file in binary mode, library functions have a mind of
14830 * their own about CR-LF conversion. */
14831 fname = get_tv_string(&argvars[0]);
14832 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14833 {
14834 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14835 return;
14836 }
14837
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014838 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014842 /* This for loop processes what was read, but is also entered at end
14843 * of file so that either:
14844 * - an incomplete line gets written
14845 * - a "binary" file gets an empty line at the end if it ends in a
14846 * newline. */
14847 for (p = buf, start = buf;
14848 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14849 ++p)
14850 {
14851 if (*p == '\n' || readlen <= 0)
14852 {
14853 listitem_T *li;
14854 char_u *s = NULL;
14855 long_u len = p - start;
14856
14857 /* Finished a line. Remove CRs before NL. */
14858 if (readlen > 0 && !binary)
14859 {
14860 while (len > 0 && start[len - 1] == '\r')
14861 --len;
14862 /* removal may cross back to the "prev" string */
14863 if (len == 0)
14864 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14865 --prevlen;
14866 }
14867 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014868 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869 else
14870 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014871 /* Change "prev" buffer to be the right size. This way
14872 * the bytes are only copied once, and very long lines are
14873 * allocated only once. */
14874 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014875 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014876 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014877 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014878 prev = NULL; /* the list will own the string */
14879 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014880 }
14881 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014882 if (s == NULL)
14883 {
14884 do_outofmem_msg((long_u) prevlen + len + 1);
14885 failed = TRUE;
14886 break;
14887 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014888
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014889 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014890 {
14891 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014892 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014893 break;
14894 }
14895 li->li_tv.v_type = VAR_STRING;
14896 li->li_tv.v_lock = 0;
14897 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014898 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014900 start = p + 1; /* step over newline */
14901 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014902 break;
14903 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014904 else if (*p == NUL)
14905 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014906#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014907 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14908 * when finding the BF and check the previous two bytes. */
14909 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014910 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14912 * + 1, these may be in the "prev" string. */
14913 char_u back1 = p >= buf + 1 ? p[-1]
14914 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14915 char_u back2 = p >= buf + 2 ? p[-2]
14916 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14917 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014918
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014919 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014920 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014921 char_u *dest = p - 2;
14922
14923 /* Usually a BOM is at the beginning of a file, and so at
14924 * the beginning of a line; then we can just step over it.
14925 */
14926 if (start == dest)
14927 start = p + 1;
14928 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014929 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 /* have to shuffle buf to close gap */
14931 int adjust_prevlen = 0;
14932
14933 if (dest < buf)
14934 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014935 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014936 dest = buf;
14937 }
14938 if (readlen > p - buf + 1)
14939 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14940 readlen -= 3 - adjust_prevlen;
14941 prevlen -= adjust_prevlen;
14942 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014943 }
14944 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014946#endif
14947 } /* for */
14948
14949 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14950 break;
14951 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014952 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014953 /* There's part of a line in buf, store it in "prev". */
14954 if (p - start + prevlen >= prevsize)
14955 {
14956 /* need bigger "prev" buffer */
14957 char_u *newprev;
14958
14959 /* A common use case is ordinary text files and "prev" gets a
14960 * fragment of a line, so the first allocation is made
14961 * small, to avoid repeatedly 'allocing' large and
14962 * 'reallocing' small. */
14963 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014964 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014965 else
14966 {
14967 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014968 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014969 prevsize = grow50pc > growmin ? grow50pc : growmin;
14970 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014971 newprev = prev == NULL ? alloc(prevsize)
14972 : vim_realloc(prev, prevsize);
14973 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014974 {
14975 do_outofmem_msg((long_u)prevsize);
14976 failed = TRUE;
14977 break;
14978 }
14979 prev = newprev;
14980 }
14981 /* Add the line part to end of "prev". */
14982 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014983 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014984 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014985 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014986
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014987 /*
14988 * For a negative line count use only the lines at the end of the file,
14989 * free the rest.
14990 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014991 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014992 while (cnt > -maxline)
14993 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014994 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014995 --cnt;
14996 }
14997
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014998 if (failed)
14999 {
15000 list_free(rettv->vval.v_list, TRUE);
15001 /* readfile doc says an empty list is returned on error */
15002 rettv->vval.v_list = list_alloc();
15003 }
15004
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015005 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015006 fclose(fd);
15007}
15008
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015009#if defined(FEAT_RELTIME)
15010static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15011
15012/*
15013 * Convert a List to proftime_T.
15014 * Return FAIL when there is something wrong.
15015 */
15016 static int
15017list2proftime(arg, tm)
15018 typval_T *arg;
15019 proftime_T *tm;
15020{
15021 long n1, n2;
15022 int error = FALSE;
15023
15024 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15025 || arg->vval.v_list->lv_len != 2)
15026 return FAIL;
15027 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15028 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15029# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015030 tm->HighPart = n1;
15031 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015032# else
15033 tm->tv_sec = n1;
15034 tm->tv_usec = n2;
15035# endif
15036 return error ? FAIL : OK;
15037}
15038#endif /* FEAT_RELTIME */
15039
15040/*
15041 * "reltime()" function
15042 */
15043 static void
15044f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015045 typval_T *argvars UNUSED;
15046 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015047{
15048#ifdef FEAT_RELTIME
15049 proftime_T res;
15050 proftime_T start;
15051
15052 if (argvars[0].v_type == VAR_UNKNOWN)
15053 {
15054 /* No arguments: get current time. */
15055 profile_start(&res);
15056 }
15057 else if (argvars[1].v_type == VAR_UNKNOWN)
15058 {
15059 if (list2proftime(&argvars[0], &res) == FAIL)
15060 return;
15061 profile_end(&res);
15062 }
15063 else
15064 {
15065 /* Two arguments: compute the difference. */
15066 if (list2proftime(&argvars[0], &start) == FAIL
15067 || list2proftime(&argvars[1], &res) == FAIL)
15068 return;
15069 profile_sub(&res, &start);
15070 }
15071
15072 if (rettv_list_alloc(rettv) == OK)
15073 {
15074 long n1, n2;
15075
15076# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015077 n1 = res.HighPart;
15078 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015079# else
15080 n1 = res.tv_sec;
15081 n2 = res.tv_usec;
15082# endif
15083 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15084 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15085 }
15086#endif
15087}
15088
15089/*
15090 * "reltimestr()" function
15091 */
15092 static void
15093f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015094 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015095 typval_T *rettv;
15096{
15097#ifdef FEAT_RELTIME
15098 proftime_T tm;
15099#endif
15100
15101 rettv->v_type = VAR_STRING;
15102 rettv->vval.v_string = NULL;
15103#ifdef FEAT_RELTIME
15104 if (list2proftime(&argvars[0], &tm) == OK)
15105 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15106#endif
15107}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015108
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15110static void make_connection __ARGS((void));
15111static int check_connection __ARGS((void));
15112
15113 static void
15114make_connection()
15115{
15116 if (X_DISPLAY == NULL
15117# ifdef FEAT_GUI
15118 && !gui.in_use
15119# endif
15120 )
15121 {
15122 x_force_connect = TRUE;
15123 setup_term_clip();
15124 x_force_connect = FALSE;
15125 }
15126}
15127
15128 static int
15129check_connection()
15130{
15131 make_connection();
15132 if (X_DISPLAY == NULL)
15133 {
15134 EMSG(_("E240: No connection to Vim server"));
15135 return FAIL;
15136 }
15137 return OK;
15138}
15139#endif
15140
15141#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015142static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143
15144 static void
15145remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 typval_T *argvars;
15147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148 int expr;
15149{
15150 char_u *server_name;
15151 char_u *keys;
15152 char_u *r = NULL;
15153 char_u buf[NUMBUFLEN];
15154# ifdef WIN32
15155 HWND w;
15156# else
15157 Window w;
15158# endif
15159
15160 if (check_restricted() || check_secure())
15161 return;
15162
15163# ifdef FEAT_X11
15164 if (check_connection() == FAIL)
15165 return;
15166# endif
15167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 server_name = get_tv_string_chk(&argvars[0]);
15169 if (server_name == NULL)
15170 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171 keys = get_tv_string_buf(&argvars[1], buf);
15172# ifdef WIN32
15173 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15174# else
15175 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15176 < 0)
15177# endif
15178 {
15179 if (r != NULL)
15180 EMSG(r); /* sending worked but evaluation failed */
15181 else
15182 EMSG2(_("E241: Unable to send to %s"), server_name);
15183 return;
15184 }
15185
15186 rettv->vval.v_string = r;
15187
15188 if (argvars[2].v_type != VAR_UNKNOWN)
15189 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015191 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015194 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 v.di_tv.v_type = VAR_STRING;
15196 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 idvar = get_tv_string_chk(&argvars[2]);
15198 if (idvar != NULL)
15199 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 }
15202}
15203#endif
15204
15205/*
15206 * "remote_expr()" function
15207 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208 static void
15209f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015210 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015211 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212{
15213 rettv->v_type = VAR_STRING;
15214 rettv->vval.v_string = NULL;
15215#ifdef FEAT_CLIENTSERVER
15216 remote_common(argvars, rettv, TRUE);
15217#endif
15218}
15219
15220/*
15221 * "remote_foreground()" function
15222 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 static void
15224f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015225 typval_T *argvars UNUSED;
15226 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228#ifdef FEAT_CLIENTSERVER
15229# ifdef WIN32
15230 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015231 {
15232 char_u *server_name = get_tv_string_chk(&argvars[0]);
15233
15234 if (server_name != NULL)
15235 serverForeground(server_name);
15236 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237# else
15238 /* Send a foreground() expression to the server. */
15239 argvars[1].v_type = VAR_STRING;
15240 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15241 argvars[2].v_type = VAR_UNKNOWN;
15242 remote_common(argvars, rettv, TRUE);
15243 vim_free(argvars[1].vval.v_string);
15244# endif
15245#endif
15246}
15247
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248 static void
15249f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015251 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252{
15253#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015254 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255 char_u *s = NULL;
15256# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015257 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015259 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260
15261 if (check_restricted() || check_secure())
15262 {
15263 rettv->vval.v_number = -1;
15264 return;
15265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 serverid = get_tv_string_chk(&argvars[0]);
15267 if (serverid == NULL)
15268 {
15269 rettv->vval.v_number = -1;
15270 return; /* type error; errmsg already given */
15271 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015273 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 if (n == 0)
15275 rettv->vval.v_number = -1;
15276 else
15277 {
15278 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15279 rettv->vval.v_number = (s != NULL);
15280 }
15281# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282 if (check_connection() == FAIL)
15283 return;
15284
15285 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287# endif
15288
15289 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 char_u *retvar;
15292
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 v.di_tv.v_type = VAR_STRING;
15294 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015295 retvar = get_tv_string_chk(&argvars[1]);
15296 if (retvar != NULL)
15297 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015298 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015299 }
15300#else
15301 rettv->vval.v_number = -1;
15302#endif
15303}
15304
Bram Moolenaar0d660222005-01-07 21:51:51 +000015305 static void
15306f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015307 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015308 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309{
15310 char_u *r = NULL;
15311
15312#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 char_u *serverid = get_tv_string_chk(&argvars[0]);
15314
15315 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316 {
15317# ifdef WIN32
15318 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015319 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015321 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 if (n != 0)
15323 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15324 if (r == NULL)
15325# else
15326 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328# endif
15329 EMSG(_("E277: Unable to read a server reply"));
15330 }
15331#endif
15332 rettv->v_type = VAR_STRING;
15333 rettv->vval.v_string = r;
15334}
15335
15336/*
15337 * "remote_send()" function
15338 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339 static void
15340f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015341 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015342 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015343{
15344 rettv->v_type = VAR_STRING;
15345 rettv->vval.v_string = NULL;
15346#ifdef FEAT_CLIENTSERVER
15347 remote_common(argvars, rettv, FALSE);
15348#endif
15349}
15350
15351/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015352 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015353 */
15354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015356 typval_T *argvars;
15357 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015358{
Bram Moolenaar33570922005-01-25 22:26:29 +000015359 list_T *l;
15360 listitem_T *item, *item2;
15361 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015363 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015364 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 dict_T *d;
15366 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015367 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015368
Bram Moolenaar8c711452005-01-14 21:53:12 +000015369 if (argvars[0].v_type == VAR_DICT)
15370 {
15371 if (argvars[2].v_type != VAR_UNKNOWN)
15372 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015373 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015374 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015376 key = get_tv_string_chk(&argvars[1]);
15377 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015379 di = dict_find(d, key, -1);
15380 if (di == NULL)
15381 EMSG2(_(e_dictkey), key);
15382 else
15383 {
15384 *rettv = di->di_tv;
15385 init_tv(&di->di_tv);
15386 dictitem_remove(d, di);
15387 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015388 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015389 }
15390 }
15391 else if (argvars[0].v_type != VAR_LIST)
15392 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015393 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015394 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 int error = FALSE;
15397
15398 idx = get_tv_number_chk(&argvars[1], &error);
15399 if (error)
15400 ; /* type error: do nothing, errmsg already given */
15401 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015402 EMSGN(_(e_listidx), idx);
15403 else
15404 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015405 if (argvars[2].v_type == VAR_UNKNOWN)
15406 {
15407 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015408 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015409 *rettv = item->li_tv;
15410 vim_free(item);
15411 }
15412 else
15413 {
15414 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 end = get_tv_number_chk(&argvars[2], &error);
15416 if (error)
15417 ; /* type error: do nothing */
15418 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015419 EMSGN(_(e_listidx), end);
15420 else
15421 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015422 int cnt = 0;
15423
15424 for (li = item; li != NULL; li = li->li_next)
15425 {
15426 ++cnt;
15427 if (li == item2)
15428 break;
15429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015430 if (li == NULL) /* didn't find "item2" after "item" */
15431 EMSG(_(e_invrange));
15432 else
15433 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015434 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015435 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015436 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015437 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015438 l->lv_first = item;
15439 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015440 item->li_prev = NULL;
15441 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015442 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015443 }
15444 }
15445 }
15446 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 }
15448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449}
15450
15451/*
15452 * "rename({from}, {to})" function
15453 */
15454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 typval_T *argvars;
15457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458{
15459 char_u buf[NUMBUFLEN];
15460
15461 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15465 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466}
15467
15468/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015469 * "repeat()" function
15470 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015471 static void
15472f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015473 typval_T *argvars;
15474 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015475{
15476 char_u *p;
15477 int n;
15478 int slen;
15479 int len;
15480 char_u *r;
15481 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015482
15483 n = get_tv_number(&argvars[1]);
15484 if (argvars[0].v_type == VAR_LIST)
15485 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015486 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015487 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015488 if (list_extend(rettv->vval.v_list,
15489 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015490 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015491 }
15492 else
15493 {
15494 p = get_tv_string(&argvars[0]);
15495 rettv->v_type = VAR_STRING;
15496 rettv->vval.v_string = NULL;
15497
15498 slen = (int)STRLEN(p);
15499 len = slen * n;
15500 if (len <= 0)
15501 return;
15502
15503 r = alloc(len + 1);
15504 if (r != NULL)
15505 {
15506 for (i = 0; i < n; i++)
15507 mch_memmove(r + i * slen, p, (size_t)slen);
15508 r[len] = NUL;
15509 }
15510
15511 rettv->vval.v_string = r;
15512 }
15513}
15514
15515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 * "resolve()" function
15517 */
15518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015520 typval_T *argvars;
15521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522{
15523 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015524#ifdef HAVE_READLINK
15525 char_u *buf = NULL;
15526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529#ifdef FEAT_SHORTCUT
15530 {
15531 char_u *v = NULL;
15532
15533 v = mch_resolve_shortcut(p);
15534 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 }
15539#else
15540# ifdef HAVE_READLINK
15541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 char_u *cpy;
15543 int len;
15544 char_u *remain = NULL;
15545 char_u *q;
15546 int is_relative_to_current = FALSE;
15547 int has_trailing_pathsep = FALSE;
15548 int limit = 100;
15549
15550 p = vim_strsave(p);
15551
15552 if (p[0] == '.' && (vim_ispathsep(p[1])
15553 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15554 is_relative_to_current = TRUE;
15555
15556 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015557 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015560 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562
15563 q = getnextcomp(p);
15564 if (*q != NUL)
15565 {
15566 /* Separate the first path component in "p", and keep the
15567 * remainder (beginning with the path separator). */
15568 remain = vim_strsave(q - 1);
15569 q[-1] = NUL;
15570 }
15571
Bram Moolenaard9462e32011-04-11 21:35:11 +020015572 buf = alloc(MAXPATHL + 1);
15573 if (buf == NULL)
15574 goto fail;
15575
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 for (;;)
15577 {
15578 for (;;)
15579 {
15580 len = readlink((char *)p, (char *)buf, MAXPATHL);
15581 if (len <= 0)
15582 break;
15583 buf[len] = NUL;
15584
15585 if (limit-- == 0)
15586 {
15587 vim_free(p);
15588 vim_free(remain);
15589 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591 goto fail;
15592 }
15593
15594 /* Ensure that the result will have a trailing path separator
15595 * if the argument has one. */
15596 if (remain == NULL && has_trailing_pathsep)
15597 add_pathsep(buf);
15598
15599 /* Separate the first path component in the link value and
15600 * concatenate the remainders. */
15601 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15602 if (*q != NUL)
15603 {
15604 if (remain == NULL)
15605 remain = vim_strsave(q - 1);
15606 else
15607 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015608 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 if (cpy != NULL)
15610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 vim_free(remain);
15612 remain = cpy;
15613 }
15614 }
15615 q[-1] = NUL;
15616 }
15617
15618 q = gettail(p);
15619 if (q > p && *q == NUL)
15620 {
15621 /* Ignore trailing path separator. */
15622 q[-1] = NUL;
15623 q = gettail(p);
15624 }
15625 if (q > p && !mch_isFullName(buf))
15626 {
15627 /* symlink is relative to directory of argument */
15628 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15629 if (cpy != NULL)
15630 {
15631 STRCPY(cpy, p);
15632 STRCPY(gettail(cpy), buf);
15633 vim_free(p);
15634 p = cpy;
15635 }
15636 }
15637 else
15638 {
15639 vim_free(p);
15640 p = vim_strsave(buf);
15641 }
15642 }
15643
15644 if (remain == NULL)
15645 break;
15646
15647 /* Append the first path component of "remain" to "p". */
15648 q = getnextcomp(remain + 1);
15649 len = q - remain - (*q != NUL);
15650 cpy = vim_strnsave(p, STRLEN(p) + len);
15651 if (cpy != NULL)
15652 {
15653 STRNCAT(cpy, remain, len);
15654 vim_free(p);
15655 p = cpy;
15656 }
15657 /* Shorten "remain". */
15658 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015659 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 else
15661 {
15662 vim_free(remain);
15663 remain = NULL;
15664 }
15665 }
15666
15667 /* If the result is a relative path name, make it explicitly relative to
15668 * the current directory if and only if the argument had this form. */
15669 if (!vim_ispathsep(*p))
15670 {
15671 if (is_relative_to_current
15672 && *p != NUL
15673 && !(p[0] == '.'
15674 && (p[1] == NUL
15675 || vim_ispathsep(p[1])
15676 || (p[1] == '.'
15677 && (p[2] == NUL
15678 || vim_ispathsep(p[2]))))))
15679 {
15680 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015681 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 if (cpy != NULL)
15683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 vim_free(p);
15685 p = cpy;
15686 }
15687 }
15688 else if (!is_relative_to_current)
15689 {
15690 /* Strip leading "./". */
15691 q = p;
15692 while (q[0] == '.' && vim_ispathsep(q[1]))
15693 q += 2;
15694 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015695 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 }
15697 }
15698
15699 /* Ensure that the result will have no trailing path separator
15700 * if the argument had none. But keep "/" or "//". */
15701 if (!has_trailing_pathsep)
15702 {
15703 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015704 if (after_pathsep(p, q))
15705 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 }
15707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015708 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 }
15710# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015711 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712# endif
15713#endif
15714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716
15717#ifdef HAVE_READLINK
15718fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015719 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722}
15723
15724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 */
15727 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 typval_T *argvars;
15730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731{
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 list_T *l;
15733 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735 if (argvars[0].v_type != VAR_LIST)
15736 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015737 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015738 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015739 {
15740 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015741 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015742 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 while (li != NULL)
15744 {
15745 ni = li->li_prev;
15746 list_append(l, li);
15747 li = ni;
15748 }
15749 rettv->vval.v_list = l;
15750 rettv->v_type = VAR_LIST;
15751 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015752 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754}
15755
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015756#define SP_NOMOVE 0x01 /* don't move cursor */
15757#define SP_REPEAT 0x02 /* repeat to find outer pair */
15758#define SP_RETCOUNT 0x04 /* return matchcount */
15759#define SP_SETPCMARK 0x08 /* set previous context mark */
15760#define SP_START 0x10 /* accept match at start position */
15761#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15762#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015763
Bram Moolenaar33570922005-01-25 22:26:29 +000015764static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015765
15766/*
15767 * Get flags for a search function.
15768 * Possibly sets "p_ws".
15769 * Returns BACKWARD, FORWARD or zero (for an error).
15770 */
15771 static int
15772get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774 int *flagsp;
15775{
15776 int dir = FORWARD;
15777 char_u *flags;
15778 char_u nbuf[NUMBUFLEN];
15779 int mask;
15780
15781 if (varp->v_type != VAR_UNKNOWN)
15782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 flags = get_tv_string_buf_chk(varp, nbuf);
15784 if (flags == NULL)
15785 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015786 while (*flags != NUL)
15787 {
15788 switch (*flags)
15789 {
15790 case 'b': dir = BACKWARD; break;
15791 case 'w': p_ws = TRUE; break;
15792 case 'W': p_ws = FALSE; break;
15793 default: mask = 0;
15794 if (flagsp != NULL)
15795 switch (*flags)
15796 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015797 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015798 case 'e': mask = SP_END; break;
15799 case 'm': mask = SP_RETCOUNT; break;
15800 case 'n': mask = SP_NOMOVE; break;
15801 case 'p': mask = SP_SUBPAT; break;
15802 case 'r': mask = SP_REPEAT; break;
15803 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 }
15805 if (mask == 0)
15806 {
15807 EMSG2(_(e_invarg2), flags);
15808 dir = 0;
15809 }
15810 else
15811 *flagsp |= mask;
15812 }
15813 if (dir == 0)
15814 break;
15815 ++flags;
15816 }
15817 }
15818 return dir;
15819}
15820
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015822 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015824 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015825search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015827 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015828 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015830 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 char_u *pat;
15832 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015833 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 int save_p_ws = p_ws;
15835 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015836 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015837 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015838 proftime_T tm;
15839#ifdef FEAT_RELTIME
15840 long time_limit = 0;
15841#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015842 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015843 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015846 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015847 if (dir == 0)
15848 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015849 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015850 if (flags & SP_START)
15851 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015852 if (flags & SP_END)
15853 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015854
Bram Moolenaar76929292008-01-06 19:07:36 +000015855 /* Optional arguments: line number to stop searching and timeout. */
15856 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015857 {
15858 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15859 if (lnum_stop < 0)
15860 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015861#ifdef FEAT_RELTIME
15862 if (argvars[3].v_type != VAR_UNKNOWN)
15863 {
15864 time_limit = get_tv_number_chk(&argvars[3], NULL);
15865 if (time_limit < 0)
15866 goto theend;
15867 }
15868#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015869 }
15870
Bram Moolenaar76929292008-01-06 19:07:36 +000015871#ifdef FEAT_RELTIME
15872 /* Set the time limit, if there is one. */
15873 profile_setlimit(time_limit, &tm);
15874#endif
15875
Bram Moolenaar231334e2005-07-25 20:46:57 +000015876 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015877 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015878 * Check to make sure only those flags are set.
15879 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15880 * flags cannot be set. Check for that condition also.
15881 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015882 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015883 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015885 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015886 goto theend;
15887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015889 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015890 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015891 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015892 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015894 if (flags & SP_SUBPAT)
15895 retval = subpatnum;
15896 else
15897 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015898 if (flags & SP_SETPCMARK)
15899 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015901 if (match_pos != NULL)
15902 {
15903 /* Store the match cursor position */
15904 match_pos->lnum = pos.lnum;
15905 match_pos->col = pos.col + 1;
15906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 /* "/$" will put the cursor after the end of the line, may need to
15908 * correct that here */
15909 check_cursor();
15910 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015911
15912 /* If 'n' flag is used: restore cursor position. */
15913 if (flags & SP_NOMOVE)
15914 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015915 else
15916 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015917theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015919
15920 return retval;
15921}
15922
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015923#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015924
15925/*
15926 * round() is not in C90, use ceil() or floor() instead.
15927 */
15928 float_T
15929vim_round(f)
15930 float_T f;
15931{
15932 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15933}
15934
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015935/*
15936 * "round({float})" function
15937 */
15938 static void
15939f_round(argvars, rettv)
15940 typval_T *argvars;
15941 typval_T *rettv;
15942{
15943 float_T f;
15944
15945 rettv->v_type = VAR_FLOAT;
15946 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015947 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015948 else
15949 rettv->vval.v_float = 0.0;
15950}
15951#endif
15952
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015953/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015954 * "screenattr()" function
15955 */
15956 static void
15957f_screenattr(argvars, rettv)
15958 typval_T *argvars UNUSED;
15959 typval_T *rettv;
15960{
15961 int row;
15962 int col;
15963 int c;
15964
15965 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15966 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15967 if (row < 0 || row >= screen_Rows
15968 || col < 0 || col >= screen_Columns)
15969 c = -1;
15970 else
15971 c = ScreenAttrs[LineOffset[row] + col];
15972 rettv->vval.v_number = c;
15973}
15974
15975/*
15976 * "screenchar()" function
15977 */
15978 static void
15979f_screenchar(argvars, rettv)
15980 typval_T *argvars UNUSED;
15981 typval_T *rettv;
15982{
15983 int row;
15984 int col;
15985 int off;
15986 int c;
15987
15988 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15989 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15990 if (row < 0 || row >= screen_Rows
15991 || col < 0 || col >= screen_Columns)
15992 c = -1;
15993 else
15994 {
15995 off = LineOffset[row] + col;
15996#ifdef FEAT_MBYTE
15997 if (enc_utf8 && ScreenLinesUC[off] != 0)
15998 c = ScreenLinesUC[off];
15999 else
16000#endif
16001 c = ScreenLines[off];
16002 }
16003 rettv->vval.v_number = c;
16004}
16005
16006/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016007 * "screencol()" function
16008 *
16009 * First column is 1 to be consistent with virtcol().
16010 */
16011 static void
16012f_screencol(argvars, rettv)
16013 typval_T *argvars UNUSED;
16014 typval_T *rettv;
16015{
16016 rettv->vval.v_number = screen_screencol() + 1;
16017}
16018
16019/*
16020 * "screenrow()" function
16021 */
16022 static void
16023f_screenrow(argvars, rettv)
16024 typval_T *argvars UNUSED;
16025 typval_T *rettv;
16026{
16027 rettv->vval.v_number = screen_screenrow() + 1;
16028}
16029
16030/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016031 * "search()" function
16032 */
16033 static void
16034f_search(argvars, rettv)
16035 typval_T *argvars;
16036 typval_T *rettv;
16037{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016038 int flags = 0;
16039
16040 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041}
16042
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016044 * "searchdecl()" function
16045 */
16046 static void
16047f_searchdecl(argvars, rettv)
16048 typval_T *argvars;
16049 typval_T *rettv;
16050{
16051 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016052 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016053 int error = FALSE;
16054 char_u *name;
16055
16056 rettv->vval.v_number = 1; /* default: FAIL */
16057
16058 name = get_tv_string_chk(&argvars[0]);
16059 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016060 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016061 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016062 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16063 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16064 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016065 if (!error && name != NULL)
16066 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016067 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016068}
16069
16070/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016071 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016073 static int
16074searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016075 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016076 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077{
16078 char_u *spat, *mpat, *epat;
16079 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016080 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016081 int dir;
16082 int flags = 0;
16083 char_u nbuf1[NUMBUFLEN];
16084 char_u nbuf2[NUMBUFLEN];
16085 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016086 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016088 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016091 spat = get_tv_string_chk(&argvars[0]);
16092 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16093 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16094 if (spat == NULL || mpat == NULL || epat == NULL)
16095 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097 /* Handle the optional fourth argument: flags */
16098 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016099 if (dir == 0)
16100 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016101
16102 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016103 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16104 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016105 if ((flags & (SP_END | SP_SUBPAT)) != 0
16106 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016107 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016108 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016109 goto theend;
16110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016112 /* Using 'r' implies 'W', otherwise it doesn't work. */
16113 if (flags & SP_REPEAT)
16114 p_ws = FALSE;
16115
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016116 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016117 if (argvars[3].v_type == VAR_UNKNOWN
16118 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 skip = (char_u *)"";
16120 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016122 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016123 if (argvars[5].v_type != VAR_UNKNOWN)
16124 {
16125 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16126 if (lnum_stop < 0)
16127 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016128#ifdef FEAT_RELTIME
16129 if (argvars[6].v_type != VAR_UNKNOWN)
16130 {
16131 time_limit = get_tv_number_chk(&argvars[6], NULL);
16132 if (time_limit < 0)
16133 goto theend;
16134 }
16135#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016136 }
16137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 if (skip == NULL)
16139 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016141 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016142 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016143
16144theend:
16145 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016146
16147 return retval;
16148}
16149
16150/*
16151 * "searchpair()" function
16152 */
16153 static void
16154f_searchpair(argvars, rettv)
16155 typval_T *argvars;
16156 typval_T *rettv;
16157{
16158 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16159}
16160
16161/*
16162 * "searchpairpos()" function
16163 */
16164 static void
16165f_searchpairpos(argvars, rettv)
16166 typval_T *argvars;
16167 typval_T *rettv;
16168{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016169 pos_T match_pos;
16170 int lnum = 0;
16171 int col = 0;
16172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016174 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016175
16176 if (searchpair_cmn(argvars, &match_pos) > 0)
16177 {
16178 lnum = match_pos.lnum;
16179 col = match_pos.col;
16180 }
16181
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016182 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16183 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016184}
16185
16186/*
16187 * Search for a start/middle/end thing.
16188 * Used by searchpair(), see its documentation for the details.
16189 * Returns 0 or -1 for no match,
16190 */
16191 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016192do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16193 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016194 char_u *spat; /* start pattern */
16195 char_u *mpat; /* middle pattern */
16196 char_u *epat; /* end pattern */
16197 int dir; /* BACKWARD or FORWARD */
16198 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016199 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016200 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016201 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016202 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016203{
16204 char_u *save_cpo;
16205 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16206 long retval = 0;
16207 pos_T pos;
16208 pos_T firstpos;
16209 pos_T foundpos;
16210 pos_T save_cursor;
16211 pos_T save_pos;
16212 int n;
16213 int r;
16214 int nest = 1;
16215 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016216 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016217 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016218
16219 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16220 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016221 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016222
Bram Moolenaar76929292008-01-06 19:07:36 +000016223#ifdef FEAT_RELTIME
16224 /* Set the time limit, if there is one. */
16225 profile_setlimit(time_limit, &tm);
16226#endif
16227
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016228 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16229 * start/middle/end (pat3, for the top pair). */
16230 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16231 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16232 if (pat2 == NULL || pat3 == NULL)
16233 goto theend;
16234 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16235 if (*mpat == NUL)
16236 STRCPY(pat3, pat2);
16237 else
16238 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16239 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016240 if (flags & SP_START)
16241 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016242
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 save_cursor = curwin->w_cursor;
16244 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016245 clearpos(&firstpos);
16246 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 pat = pat3;
16248 for (;;)
16249 {
16250 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016251 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16253 /* didn't find it or found the first match again: FAIL */
16254 break;
16255
16256 if (firstpos.lnum == 0)
16257 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016258 if (equalpos(pos, foundpos))
16259 {
16260 /* Found the same position again. Can happen with a pattern that
16261 * has "\zs" at the end and searching backwards. Advance one
16262 * character and try again. */
16263 if (dir == BACKWARD)
16264 decl(&pos);
16265 else
16266 incl(&pos);
16267 }
16268 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016270 /* clear the start flag to avoid getting stuck here */
16271 options &= ~SEARCH_START;
16272
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 /* If the skip pattern matches, ignore this match. */
16274 if (*skip != NUL)
16275 {
16276 save_pos = curwin->w_cursor;
16277 curwin->w_cursor = pos;
16278 r = eval_to_bool(skip, &err, NULL, FALSE);
16279 curwin->w_cursor = save_pos;
16280 if (err)
16281 {
16282 /* Evaluating {skip} caused an error, break here. */
16283 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016284 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 break;
16286 }
16287 if (r)
16288 continue;
16289 }
16290
16291 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16292 {
16293 /* Found end when searching backwards or start when searching
16294 * forward: nested pair. */
16295 ++nest;
16296 pat = pat2; /* nested, don't search for middle */
16297 }
16298 else
16299 {
16300 /* Found end when searching forward or start when searching
16301 * backward: end of (nested) pair; or found middle in outer pair. */
16302 if (--nest == 1)
16303 pat = pat3; /* outer level, search for middle */
16304 }
16305
16306 if (nest == 0)
16307 {
16308 /* Found the match: return matchcount or line number. */
16309 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016310 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016312 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016313 if (flags & SP_SETPCMARK)
16314 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 curwin->w_cursor = pos;
16316 if (!(flags & SP_REPEAT))
16317 break;
16318 nest = 1; /* search for next unmatched */
16319 }
16320 }
16321
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016322 if (match_pos != NULL)
16323 {
16324 /* Store the match cursor position */
16325 match_pos->lnum = curwin->w_cursor.lnum;
16326 match_pos->col = curwin->w_cursor.col + 1;
16327 }
16328
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016330 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 curwin->w_cursor = save_cursor;
16332
16333theend:
16334 vim_free(pat2);
16335 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016336 if (p_cpo == empty_option)
16337 p_cpo = save_cpo;
16338 else
16339 /* Darn, evaluating the {skip} expression changed the value. */
16340 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016341
16342 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343}
16344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345/*
16346 * "searchpos()" function
16347 */
16348 static void
16349f_searchpos(argvars, rettv)
16350 typval_T *argvars;
16351 typval_T *rettv;
16352{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016353 pos_T match_pos;
16354 int lnum = 0;
16355 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016356 int n;
16357 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016359 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016360 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016361
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016362 n = search_cmn(argvars, &match_pos, &flags);
16363 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016364 {
16365 lnum = match_pos.lnum;
16366 col = match_pos.col;
16367 }
16368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016369 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16370 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016371 if (flags & SP_SUBPAT)
16372 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373}
16374
16375
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 static void
16377f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016378 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381#ifdef FEAT_CLIENTSERVER
16382 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016383 char_u *server = get_tv_string_chk(&argvars[0]);
16384 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016387 if (server == NULL || reply == NULL)
16388 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 if (check_restricted() || check_secure())
16390 return;
16391# ifdef FEAT_X11
16392 if (check_connection() == FAIL)
16393 return;
16394# endif
16395
16396 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398 EMSG(_("E258: Unable to send to client"));
16399 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 rettv->vval.v_number = 0;
16402#else
16403 rettv->vval.v_number = -1;
16404#endif
16405}
16406
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407 static void
16408f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016410 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411{
16412 char_u *r = NULL;
16413
16414#ifdef FEAT_CLIENTSERVER
16415# ifdef WIN32
16416 r = serverGetVimNames();
16417# else
16418 make_connection();
16419 if (X_DISPLAY != NULL)
16420 r = serverGetVimNames(X_DISPLAY);
16421# endif
16422#endif
16423 rettv->v_type = VAR_STRING;
16424 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425}
16426
16427/*
16428 * "setbufvar()" function
16429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016432 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016433 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434{
16435 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 char_u nbuf[NUMBUFLEN];
16440
16441 if (check_restricted() || check_secure())
16442 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16444 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016445 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 varp = &argvars[2];
16447
16448 if (buf != NULL && varname != NULL && varp != NULL)
16449 {
16450 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452
16453 if (*varname == '&')
16454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016455 long numval;
16456 char_u *strval;
16457 int error = FALSE;
16458
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016460 numval = get_tv_number_chk(varp, &error);
16461 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 if (!error && strval != NULL)
16463 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 }
16465 else
16466 {
16467 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16468 if (bufvarname != NULL)
16469 {
16470 STRCPY(bufvarname, "b:");
16471 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016472 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 vim_free(bufvarname);
16474 }
16475 }
16476
16477 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480}
16481
16482/*
16483 * "setcmdpos()" function
16484 */
16485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016486f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016487 typval_T *argvars;
16488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016490 int pos = (int)get_tv_number(&argvars[0]) - 1;
16491
16492 if (pos >= 0)
16493 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494}
16495
16496/*
16497 * "setline()" function
16498 */
16499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016500f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 typval_T *argvars;
16502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503{
16504 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016505 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016506 list_T *l = NULL;
16507 listitem_T *li = NULL;
16508 long added = 0;
16509 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016511 lnum = get_tv_lnum(&argvars[0]);
16512 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016514 l = argvars[1].vval.v_list;
16515 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016517 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016519
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016520 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016521 for (;;)
16522 {
16523 if (l != NULL)
16524 {
16525 /* list argument, get next string */
16526 if (li == NULL)
16527 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016529 li = li->li_next;
16530 }
16531
16532 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016534 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016535
16536 /* When coming here from Insert mode, sync undo, so that this can be
16537 * undone separately from what was previously inserted. */
16538 if (u_sync_once == 2)
16539 {
16540 u_sync_once = 1; /* notify that u_sync() was called */
16541 u_sync(TRUE);
16542 }
16543
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016544 if (lnum <= curbuf->b_ml.ml_line_count)
16545 {
16546 /* existing line, replace it */
16547 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16548 {
16549 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016550 if (lnum == curwin->w_cursor.lnum)
16551 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016552 rettv->vval.v_number = 0; /* OK */
16553 }
16554 }
16555 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16556 {
16557 /* lnum is one past the last line, append the line */
16558 ++added;
16559 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16560 rettv->vval.v_number = 0; /* OK */
16561 }
16562
16563 if (l == NULL) /* only one string argument */
16564 break;
16565 ++lnum;
16566 }
16567
16568 if (added > 0)
16569 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570}
16571
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016572static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16573
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016575 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016576 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016577 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016578set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016579 win_T *wp UNUSED;
16580 typval_T *list_arg UNUSED;
16581 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016582 typval_T *rettv;
16583{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016584#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016585 char_u *act;
16586 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016587#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016588
Bram Moolenaar2641f772005-03-25 21:58:17 +000016589 rettv->vval.v_number = -1;
16590
16591#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016592 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016593 EMSG(_(e_listreq));
16594 else
16595 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016596 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016597
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016599 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016600 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016601 if (act == NULL)
16602 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016603 if (*act == 'a' || *act == 'r')
16604 action = *act;
16605 }
16606
Bram Moolenaar81484f42012-12-05 15:16:47 +010016607 if (l != NULL && set_errorlist(wp, l, action,
16608 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016609 rettv->vval.v_number = 0;
16610 }
16611#endif
16612}
16613
16614/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016615 * "setloclist()" function
16616 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016617 static void
16618f_setloclist(argvars, rettv)
16619 typval_T *argvars;
16620 typval_T *rettv;
16621{
16622 win_T *win;
16623
16624 rettv->vval.v_number = -1;
16625
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016626 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016627 if (win != NULL)
16628 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16629}
16630
16631/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016632 * "setmatches()" function
16633 */
16634 static void
16635f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016636 typval_T *argvars UNUSED;
16637 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016638{
16639#ifdef FEAT_SEARCH_EXTRA
16640 list_T *l;
16641 listitem_T *li;
16642 dict_T *d;
16643
16644 rettv->vval.v_number = -1;
16645 if (argvars[0].v_type != VAR_LIST)
16646 {
16647 EMSG(_(e_listreq));
16648 return;
16649 }
16650 if ((l = argvars[0].vval.v_list) != NULL)
16651 {
16652
16653 /* To some extent make sure that we are dealing with a list from
16654 * "getmatches()". */
16655 li = l->lv_first;
16656 while (li != NULL)
16657 {
16658 if (li->li_tv.v_type != VAR_DICT
16659 || (d = li->li_tv.vval.v_dict) == NULL)
16660 {
16661 EMSG(_(e_invarg));
16662 return;
16663 }
16664 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16665 && dict_find(d, (char_u *)"pattern", -1) != NULL
16666 && dict_find(d, (char_u *)"priority", -1) != NULL
16667 && dict_find(d, (char_u *)"id", -1) != NULL))
16668 {
16669 EMSG(_(e_invarg));
16670 return;
16671 }
16672 li = li->li_next;
16673 }
16674
16675 clear_matches(curwin);
16676 li = l->lv_first;
16677 while (li != NULL)
16678 {
16679 d = li->li_tv.vval.v_dict;
16680 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16681 get_dict_string(d, (char_u *)"pattern", FALSE),
16682 (int)get_dict_number(d, (char_u *)"priority"),
16683 (int)get_dict_number(d, (char_u *)"id"));
16684 li = li->li_next;
16685 }
16686 rettv->vval.v_number = 0;
16687 }
16688#endif
16689}
16690
16691/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016692 * "setpos()" function
16693 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016694 static void
16695f_setpos(argvars, rettv)
16696 typval_T *argvars;
16697 typval_T *rettv;
16698{
16699 pos_T pos;
16700 int fnum;
16701 char_u *name;
16702
Bram Moolenaar08250432008-02-13 11:42:46 +000016703 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016704 name = get_tv_string_chk(argvars);
16705 if (name != NULL)
16706 {
16707 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16708 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016709 if (--pos.col < 0)
16710 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016711 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016712 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016713 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016714 if (fnum == curbuf->b_fnum)
16715 {
16716 curwin->w_cursor = pos;
16717 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016718 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016719 }
16720 else
16721 EMSG(_(e_invarg));
16722 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016723 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16724 {
16725 /* set mark */
16726 if (setmark_pos(name[1], &pos, fnum) == OK)
16727 rettv->vval.v_number = 0;
16728 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016729 else
16730 EMSG(_(e_invarg));
16731 }
16732 }
16733}
16734
16735/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016736 * "setqflist()" function
16737 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016738 static void
16739f_setqflist(argvars, rettv)
16740 typval_T *argvars;
16741 typval_T *rettv;
16742{
16743 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16744}
16745
16746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747 * "setreg()" function
16748 */
16749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 typval_T *argvars;
16752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
16754 int regname;
16755 char_u *strregname;
16756 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 int append;
16759 char_u yank_type;
16760 long block_len;
16761
16762 block_len = -1;
16763 yank_type = MAUTO;
16764 append = FALSE;
16765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016766 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016767 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016769 if (strregname == NULL)
16770 return; /* type error; errmsg already given */
16771 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 if (regname == 0 || regname == '@')
16773 regname = '"';
16774 else if (regname == '=')
16775 return;
16776
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016777 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016779 stropt = get_tv_string_chk(&argvars[2]);
16780 if (stropt == NULL)
16781 return; /* type error */
16782 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 switch (*stropt)
16784 {
16785 case 'a': case 'A': /* append */
16786 append = TRUE;
16787 break;
16788 case 'v': case 'c': /* character-wise selection */
16789 yank_type = MCHAR;
16790 break;
16791 case 'V': case 'l': /* line-wise selection */
16792 yank_type = MLINE;
16793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794 case 'b': case Ctrl_V: /* block-wise selection */
16795 yank_type = MBLOCK;
16796 if (VIM_ISDIGIT(stropt[1]))
16797 {
16798 ++stropt;
16799 block_len = getdigits(&stropt) - 1;
16800 --stropt;
16801 }
16802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 }
16804 }
16805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016806 strval = get_tv_string_chk(&argvars[1]);
16807 if (strval != NULL)
16808 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811}
16812
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016813/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016814 * "settabvar()" function
16815 */
16816 static void
16817f_settabvar(argvars, rettv)
16818 typval_T *argvars;
16819 typval_T *rettv;
16820{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016821#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016822 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016823 tabpage_T *tp;
16824#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016825 char_u *varname, *tabvarname;
16826 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016827
16828 rettv->vval.v_number = 0;
16829
16830 if (check_restricted() || check_secure())
16831 return;
16832
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016833#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016834 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016835#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016836 varname = get_tv_string_chk(&argvars[1]);
16837 varp = &argvars[2];
16838
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016839 if (varname != NULL && varp != NULL
16840#ifdef FEAT_WINDOWS
16841 && tp != NULL
16842#endif
16843 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016844 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016845#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016846 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016847 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016848#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016849
16850 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16851 if (tabvarname != NULL)
16852 {
16853 STRCPY(tabvarname, "t:");
16854 STRCPY(tabvarname + 2, varname);
16855 set_var(tabvarname, varp, TRUE);
16856 vim_free(tabvarname);
16857 }
16858
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016859#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016860 /* Restore current tabpage */
16861 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016862 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016863#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016864 }
16865}
16866
16867/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016868 * "settabwinvar()" function
16869 */
16870 static void
16871f_settabwinvar(argvars, rettv)
16872 typval_T *argvars;
16873 typval_T *rettv;
16874{
16875 setwinvar(argvars, rettv, 1);
16876}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877
16878/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016879 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016883 typval_T *argvars;
16884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016886 setwinvar(argvars, rettv, 0);
16887}
16888
16889/*
16890 * "setwinvar()" and "settabwinvar()" functions
16891 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016892
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016893 static void
16894setwinvar(argvars, rettv, off)
16895 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016896 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016897 int off;
16898{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 win_T *win;
16900#ifdef FEAT_WINDOWS
16901 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016902 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903#endif
16904 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016907 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908
16909 if (check_restricted() || check_secure())
16910 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016911
16912#ifdef FEAT_WINDOWS
16913 if (off == 1)
16914 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16915 else
16916 tp = curtab;
16917#endif
16918 win = find_win_by_nr(&argvars[off], tp);
16919 varname = get_tv_string_chk(&argvars[off + 1]);
16920 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921
16922 if (win != NULL && varname != NULL && varp != NULL)
16923 {
16924#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016925 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016926 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927#endif
16928
16929 if (*varname == '&')
16930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016931 long numval;
16932 char_u *strval;
16933 int error = FALSE;
16934
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936 numval = get_tv_number_chk(varp, &error);
16937 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 if (!error && strval != NULL)
16939 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 }
16941 else
16942 {
16943 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16944 if (winvarname != NULL)
16945 {
16946 STRCPY(winvarname, "w:");
16947 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016948 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 vim_free(winvarname);
16950 }
16951 }
16952
16953#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016954 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955#endif
16956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957}
16958
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016959#ifdef FEAT_CRYPT
16960/*
16961 * "sha256({string})" function
16962 */
16963 static void
16964f_sha256(argvars, rettv)
16965 typval_T *argvars;
16966 typval_T *rettv;
16967{
16968 char_u *p;
16969
16970 p = get_tv_string(&argvars[0]);
16971 rettv->vval.v_string = vim_strsave(
16972 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16973 rettv->v_type = VAR_STRING;
16974}
16975#endif /* FEAT_CRYPT */
16976
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016978 * "shellescape({string})" function
16979 */
16980 static void
16981f_shellescape(argvars, rettv)
16982 typval_T *argvars;
16983 typval_T *rettv;
16984{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016985 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010016986 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016987 rettv->v_type = VAR_STRING;
16988}
16989
16990/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016991 * shiftwidth() function
16992 */
16993 static void
16994f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016995 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016996 typval_T *rettv;
16997{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016998 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016999}
17000
17001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 */
17004 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017006 typval_T *argvars;
17007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 p = get_tv_string(&argvars[0]);
17012 rettv->vval.v_string = vim_strsave(p);
17013 simplify_filename(rettv->vval.v_string); /* simplify in place */
17014 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017015}
17016
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017017#ifdef FEAT_FLOAT
17018/*
17019 * "sin()" function
17020 */
17021 static void
17022f_sin(argvars, rettv)
17023 typval_T *argvars;
17024 typval_T *rettv;
17025{
17026 float_T f;
17027
17028 rettv->v_type = VAR_FLOAT;
17029 if (get_float_arg(argvars, &f) == OK)
17030 rettv->vval.v_float = sin(f);
17031 else
17032 rettv->vval.v_float = 0.0;
17033}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017034
17035/*
17036 * "sinh()" function
17037 */
17038 static void
17039f_sinh(argvars, rettv)
17040 typval_T *argvars;
17041 typval_T *rettv;
17042{
17043 float_T f;
17044
17045 rettv->v_type = VAR_FLOAT;
17046 if (get_float_arg(argvars, &f) == OK)
17047 rettv->vval.v_float = sinh(f);
17048 else
17049 rettv->vval.v_float = 0.0;
17050}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017051#endif
17052
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053static int
17054#ifdef __BORLANDC__
17055 _RTLENTRYF
17056#endif
17057 item_compare __ARGS((const void *s1, const void *s2));
17058static int
17059#ifdef __BORLANDC__
17060 _RTLENTRYF
17061#endif
17062 item_compare2 __ARGS((const void *s1, const void *s2));
17063
17064static int item_compare_ic;
17065static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017066static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017068static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069#define ITEM_COMPARE_FAIL 999
17070
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017072 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 static int
17075#ifdef __BORLANDC__
17076_RTLENTRYF
17077#endif
17078item_compare(s1, s2)
17079 const void *s1;
17080 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 char_u *p1, *p2;
17083 char_u *tofree1, *tofree2;
17084 int res;
17085 char_u numbuf1[NUMBUFLEN];
17086 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017088 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17089 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017090 if (p1 == NULL)
17091 p1 = (char_u *)"";
17092 if (p2 == NULL)
17093 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 if (item_compare_ic)
17095 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 res = STRCMP(p1, p2);
17098 vim_free(tofree1);
17099 vim_free(tofree2);
17100 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101}
17102
17103 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104#ifdef __BORLANDC__
17105_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107item_compare2(s1, s2)
17108 const void *s1;
17109 const void *s2;
17110{
17111 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017113 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 /* shortcut after failure in previous call; compare all items equal */
17117 if (item_compare_func_err)
17118 return 0;
17119
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017120 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17121 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17123 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124
17125 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017126 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017127 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17128 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 clear_tv(&argv[0]);
17130 clear_tv(&argv[1]);
17131
17132 if (res == FAIL)
17133 res = ITEM_COMPARE_FAIL;
17134 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17136 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017137 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 clear_tv(&rettv);
17139 return res;
17140}
17141
17142/*
17143 * "sort({list})" function
17144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017146do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 typval_T *argvars;
17148 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017149 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150{
Bram Moolenaar33570922005-01-25 22:26:29 +000017151 list_T *l;
17152 listitem_T *li;
17153 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 long len;
17155 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017158 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 else
17160 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017162 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017163 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017164 return;
17165 rettv->vval.v_list = l;
17166 rettv->v_type = VAR_LIST;
17167 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169 len = list_len(l);
17170 if (len <= 1)
17171 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 item_compare_ic = FALSE;
17174 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017175 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 if (argvars[1].v_type != VAR_UNKNOWN)
17177 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017178 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 else
17182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017183 int error = FALSE;
17184
17185 i = get_tv_number_chk(&argvars[1], &error);
17186 if (error)
17187 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 if (i == 1)
17189 item_compare_ic = TRUE;
17190 else
17191 item_compare_func = get_tv_string(&argvars[1]);
17192 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017193
17194 if (argvars[2].v_type != VAR_UNKNOWN)
17195 {
17196 /* optional third argument: {dict} */
17197 if (argvars[2].v_type != VAR_DICT)
17198 {
17199 EMSG(_(e_dictreq));
17200 return;
17201 }
17202 item_compare_selfdict = argvars[2].vval.v_dict;
17203 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017207 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 if (ptrs == NULL)
17209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210
Bram Moolenaar327aa022014-03-25 18:24:23 +010017211 i = 0;
17212 if (sort)
17213 {
17214 /* sort(): ptrs will be the list to sort */
17215 for (li = l->lv_first; li != NULL; li = li->li_next)
17216 ptrs[i++] = li;
17217
17218 item_compare_func_err = FALSE;
17219 /* test the compare function */
17220 if (item_compare_func != NULL
17221 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017223 EMSG(_("E702: Sort compare function failed"));
17224 else
17225 {
17226 /* Sort the array with item pointers. */
17227 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17228 item_compare_func == NULL ? item_compare : item_compare2);
17229
17230 if (!item_compare_func_err)
17231 {
17232 /* Clear the List and append the items in sorted order. */
17233 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17234 l->lv_len = 0;
17235 for (i = 0; i < len; ++i)
17236 list_append(l, ptrs[i]);
17237 }
17238 }
17239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017242 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17243
17244 /* f_uniq(): ptrs will be a stack of items to remove */
17245 item_compare_func_err = FALSE;
17246 item_compare_func_ptr = item_compare_func
17247 ? item_compare2 : item_compare;
17248
17249 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17250 li = li->li_next)
17251 {
17252 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17253 == 0)
17254 ptrs[i++] = li;
17255 if (item_compare_func_err)
17256 {
17257 EMSG(_("E882: Uniq compare function failed"));
17258 break;
17259 }
17260 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017262 if (!item_compare_func_err)
17263 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017264 while (--i >= 0)
17265 {
17266 li = ptrs[i]->li_next;
17267 ptrs[i]->li_next = li->li_next;
17268 if (li->li_next != NULL)
17269 li->li_next->li_prev = ptrs[i];
17270 else
17271 l->lv_last = ptrs[i];
17272 list_fix_watch(l, li);
17273 listitem_free(li);
17274 l->lv_len--;
17275 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017277 }
17278
17279 vim_free(ptrs);
17280 }
17281}
17282
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017283/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017284 * "sort({list})" function
17285 */
17286 static void
17287f_sort(argvars, rettv)
17288 typval_T *argvars;
17289 typval_T *rettv;
17290{
17291 do_sort_uniq(argvars, rettv, TRUE);
17292}
17293
17294/*
17295 * "uniq({list})" function
17296 */
17297 static void
17298f_uniq(argvars, rettv)
17299 typval_T *argvars;
17300 typval_T *rettv;
17301{
17302 do_sort_uniq(argvars, rettv, FALSE);
17303}
17304
17305/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017306 * "soundfold({word})" function
17307 */
17308 static void
17309f_soundfold(argvars, rettv)
17310 typval_T *argvars;
17311 typval_T *rettv;
17312{
17313 char_u *s;
17314
17315 rettv->v_type = VAR_STRING;
17316 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017317#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017318 rettv->vval.v_string = eval_soundfold(s);
17319#else
17320 rettv->vval.v_string = vim_strsave(s);
17321#endif
17322}
17323
17324/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017325 * "spellbadword()" function
17326 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017327 static void
17328f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017329 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017330 typval_T *rettv;
17331{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017332 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017333 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017334 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017337 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017338
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017339#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017340 if (argvars[0].v_type == VAR_UNKNOWN)
17341 {
17342 /* Find the start and length of the badly spelled word. */
17343 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17344 if (len != 0)
17345 word = ml_get_cursor();
17346 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017347 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017348 {
17349 char_u *str = get_tv_string_chk(&argvars[0]);
17350 int capcol = -1;
17351
17352 if (str != NULL)
17353 {
17354 /* Check the argument for spelling. */
17355 while (*str != NUL)
17356 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017357 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017358 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017359 {
17360 word = str;
17361 break;
17362 }
17363 str += len;
17364 }
17365 }
17366 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017367#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017369 list_append_string(rettv->vval.v_list, word, len);
17370 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017371 attr == HLF_SPB ? "bad" :
17372 attr == HLF_SPR ? "rare" :
17373 attr == HLF_SPL ? "local" :
17374 attr == HLF_SPC ? "caps" :
17375 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017376}
17377
17378/*
17379 * "spellsuggest()" function
17380 */
17381 static void
17382f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017383 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017384 typval_T *rettv;
17385{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017386#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017387 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017388 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017389 int maxcount;
17390 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017391 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017392 listitem_T *li;
17393 int need_capital = FALSE;
17394#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017397 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017398
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017399#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017400 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017401 {
17402 str = get_tv_string(&argvars[0]);
17403 if (argvars[1].v_type != VAR_UNKNOWN)
17404 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017405 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017406 if (maxcount <= 0)
17407 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017408 if (argvars[2].v_type != VAR_UNKNOWN)
17409 {
17410 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17411 if (typeerr)
17412 return;
17413 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017414 }
17415 else
17416 maxcount = 25;
17417
Bram Moolenaar4770d092006-01-12 23:22:24 +000017418 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017419
17420 for (i = 0; i < ga.ga_len; ++i)
17421 {
17422 str = ((char_u **)ga.ga_data)[i];
17423
17424 li = listitem_alloc();
17425 if (li == NULL)
17426 vim_free(str);
17427 else
17428 {
17429 li->li_tv.v_type = VAR_STRING;
17430 li->li_tv.v_lock = 0;
17431 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017432 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017433 }
17434 }
17435 ga_clear(&ga);
17436 }
17437#endif
17438}
17439
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017441f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017442 typval_T *argvars;
17443 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017444{
17445 char_u *str;
17446 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017447 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017448 regmatch_T regmatch;
17449 char_u patbuf[NUMBUFLEN];
17450 char_u *save_cpo;
17451 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017452 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017453 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017455
17456 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17457 save_cpo = p_cpo;
17458 p_cpo = (char_u *)"";
17459
17460 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017461 if (argvars[1].v_type != VAR_UNKNOWN)
17462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017463 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17464 if (pat == NULL)
17465 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017467 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017468 }
17469 if (pat == NULL || *pat == NUL)
17470 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017474 if (typeerr)
17475 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476
Bram Moolenaar0d660222005-01-07 21:51:51 +000017477 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17478 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017480 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017481 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017482 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017483 if (*str == NUL)
17484 match = FALSE; /* empty item at the end */
17485 else
17486 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017487 if (match)
17488 end = regmatch.startp[0];
17489 else
17490 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017491 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17492 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017494 if (list_append_string(rettv->vval.v_list, str,
17495 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017496 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017497 }
17498 if (!match)
17499 break;
17500 /* Advance to just after the match. */
17501 if (regmatch.endp[0] > str)
17502 col = 0;
17503 else
17504 {
17505 /* Don't get stuck at the same match. */
17506#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017507 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508#else
17509 col = 1;
17510#endif
17511 }
17512 str = regmatch.endp[0];
17513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514
Bram Moolenaar473de612013-06-08 18:19:48 +020017515 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
Bram Moolenaar0d660222005-01-07 21:51:51 +000017518 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519}
17520
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017521#ifdef FEAT_FLOAT
17522/*
17523 * "sqrt()" function
17524 */
17525 static void
17526f_sqrt(argvars, rettv)
17527 typval_T *argvars;
17528 typval_T *rettv;
17529{
17530 float_T f;
17531
17532 rettv->v_type = VAR_FLOAT;
17533 if (get_float_arg(argvars, &f) == OK)
17534 rettv->vval.v_float = sqrt(f);
17535 else
17536 rettv->vval.v_float = 0.0;
17537}
17538
17539/*
17540 * "str2float()" function
17541 */
17542 static void
17543f_str2float(argvars, rettv)
17544 typval_T *argvars;
17545 typval_T *rettv;
17546{
17547 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17548
17549 if (*p == '+')
17550 p = skipwhite(p + 1);
17551 (void)string2float(p, &rettv->vval.v_float);
17552 rettv->v_type = VAR_FLOAT;
17553}
17554#endif
17555
Bram Moolenaar2c932302006-03-18 21:42:09 +000017556/*
17557 * "str2nr()" function
17558 */
17559 static void
17560f_str2nr(argvars, rettv)
17561 typval_T *argvars;
17562 typval_T *rettv;
17563{
17564 int base = 10;
17565 char_u *p;
17566 long n;
17567
17568 if (argvars[1].v_type != VAR_UNKNOWN)
17569 {
17570 base = get_tv_number(&argvars[1]);
17571 if (base != 8 && base != 10 && base != 16)
17572 {
17573 EMSG(_(e_invarg));
17574 return;
17575 }
17576 }
17577
17578 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017579 if (*p == '+')
17580 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017581 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17582 rettv->vval.v_number = n;
17583}
17584
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585#ifdef HAVE_STRFTIME
17586/*
17587 * "strftime({format}[, {time}])" function
17588 */
17589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017591 typval_T *argvars;
17592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593{
17594 char_u result_buf[256];
17595 struct tm *curtime;
17596 time_t seconds;
17597 char_u *p;
17598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017601 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017602 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 seconds = time(NULL);
17604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017605 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606 curtime = localtime(&seconds);
17607 /* MSVC returns NULL for an invalid value of seconds. */
17608 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 else
17611 {
17612# ifdef FEAT_MBYTE
17613 vimconv_T conv;
17614 char_u *enc;
17615
17616 conv.vc_type = CONV_NONE;
17617 enc = enc_locale();
17618 convert_setup(&conv, p_enc, enc);
17619 if (conv.vc_type != CONV_NONE)
17620 p = string_convert(&conv, p, NULL);
17621# endif
17622 if (p != NULL)
17623 (void)strftime((char *)result_buf, sizeof(result_buf),
17624 (char *)p, curtime);
17625 else
17626 result_buf[0] = NUL;
17627
17628# ifdef FEAT_MBYTE
17629 if (conv.vc_type != CONV_NONE)
17630 vim_free(p);
17631 convert_setup(&conv, enc, p_enc);
17632 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017633 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 else
17635# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017636 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637
17638# ifdef FEAT_MBYTE
17639 /* Release conversion descriptors */
17640 convert_setup(&conv, NULL, NULL);
17641 vim_free(enc);
17642# endif
17643 }
17644}
17645#endif
17646
17647/*
17648 * "stridx()" function
17649 */
17650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 typval_T *argvars;
17653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654{
17655 char_u buf[NUMBUFLEN];
17656 char_u *needle;
17657 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 needle = get_tv_string_chk(&argvars[1]);
17663 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017664 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 if (needle == NULL || haystack == NULL)
17666 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667
Bram Moolenaar33570922005-01-25 22:26:29 +000017668 if (argvars[2].v_type != VAR_UNKNOWN)
17669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 int error = FALSE;
17671
17672 start_idx = get_tv_number_chk(&argvars[2], &error);
17673 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017674 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017675 if (start_idx >= 0)
17676 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 }
17678
17679 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17680 if (pos != NULL)
17681 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
17684/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017685 * "string()" function
17686 */
17687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017689 typval_T *argvars;
17690 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017691{
17692 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017693 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017696 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017697 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017698 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017699 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700}
17701
17702/*
17703 * "strlen()" function
17704 */
17705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017706f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017707 typval_T *argvars;
17708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710 rettv->vval.v_number = (varnumber_T)(STRLEN(
17711 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712}
17713
17714/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017715 * "strchars()" function
17716 */
17717 static void
17718f_strchars(argvars, rettv)
17719 typval_T *argvars;
17720 typval_T *rettv;
17721{
17722 char_u *s = get_tv_string(&argvars[0]);
17723#ifdef FEAT_MBYTE
17724 varnumber_T len = 0;
17725
17726 while (*s != NUL)
17727 {
17728 mb_cptr2char_adv(&s);
17729 ++len;
17730 }
17731 rettv->vval.v_number = len;
17732#else
17733 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17734#endif
17735}
17736
17737/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017738 * "strdisplaywidth()" function
17739 */
17740 static void
17741f_strdisplaywidth(argvars, rettv)
17742 typval_T *argvars;
17743 typval_T *rettv;
17744{
17745 char_u *s = get_tv_string(&argvars[0]);
17746 int col = 0;
17747
17748 if (argvars[1].v_type != VAR_UNKNOWN)
17749 col = get_tv_number(&argvars[1]);
17750
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017751 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017752}
17753
17754/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017755 * "strwidth()" function
17756 */
17757 static void
17758f_strwidth(argvars, rettv)
17759 typval_T *argvars;
17760 typval_T *rettv;
17761{
17762 char_u *s = get_tv_string(&argvars[0]);
17763
17764 rettv->vval.v_number = (varnumber_T)(
17765#ifdef FEAT_MBYTE
17766 mb_string2cells(s, -1)
17767#else
17768 STRLEN(s)
17769#endif
17770 );
17771}
17772
17773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 * "strpart()" function
17775 */
17776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 typval_T *argvars;
17779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780{
17781 char_u *p;
17782 int n;
17783 int len;
17784 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 slen = (int)STRLEN(p);
17789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017790 n = get_tv_number_chk(&argvars[1], &error);
17791 if (error)
17792 len = 0;
17793 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 else
17796 len = slen - n; /* default len: all bytes that are available. */
17797
17798 /*
17799 * Only return the overlap between the specified part and the actual
17800 * string.
17801 */
17802 if (n < 0)
17803 {
17804 len += n;
17805 n = 0;
17806 }
17807 else if (n > slen)
17808 n = slen;
17809 if (len < 0)
17810 len = 0;
17811 else if (n + len > slen)
17812 len = slen - n;
17813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017814 rettv->v_type = VAR_STRING;
17815 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816}
17817
17818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017819 * "strridx()" function
17820 */
17821 static void
17822f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017823 typval_T *argvars;
17824 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825{
17826 char_u buf[NUMBUFLEN];
17827 char_u *needle;
17828 char_u *haystack;
17829 char_u *rest;
17830 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017831 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017833 needle = get_tv_string_chk(&argvars[1]);
17834 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017835
17836 rettv->vval.v_number = -1;
17837 if (needle == NULL || haystack == NULL)
17838 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017839
17840 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017841 if (argvars[2].v_type != VAR_UNKNOWN)
17842 {
17843 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017844 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017845 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017846 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017847 }
17848 else
17849 end_idx = haystack_len;
17850
Bram Moolenaar0d660222005-01-07 21:51:51 +000017851 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017852 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017853 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017854 lastmatch = haystack + end_idx;
17855 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017856 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017857 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017858 for (rest = haystack; *rest != '\0'; ++rest)
17859 {
17860 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017861 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017862 break;
17863 lastmatch = rest;
17864 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017865 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866
17867 if (lastmatch == NULL)
17868 rettv->vval.v_number = -1;
17869 else
17870 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17871}
17872
17873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 * "strtrans()" function
17875 */
17876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017877f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017878 typval_T *argvars;
17879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881 rettv->v_type = VAR_STRING;
17882 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883}
17884
17885/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017886 * "submatch()" function
17887 */
17888 static void
17889f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017890 typval_T *argvars;
17891 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017892{
Bram Moolenaar41571762014-04-02 19:00:58 +020017893 int error = FALSE;
17894 char_u **match;
17895 char_u **s;
17896 listitem_T *li;
17897 int no;
17898 int retList = 0;
17899
17900 no = (int)get_tv_number_chk(&argvars[0], &error);
17901 if (error)
17902 return;
17903 error = FALSE;
17904 if (argvars[1].v_type != VAR_UNKNOWN)
17905 retList = get_tv_number_chk(&argvars[1], &error);
17906 if (error)
17907 return;
17908
17909 if (retList == 0)
17910 {
17911 rettv->v_type = VAR_STRING;
17912 rettv->vval.v_string = reg_submatch(no);
17913 }
17914 else
17915 {
17916 rettv->v_type = VAR_LIST;
17917 rettv->vval.v_list = reg_submatch_list(no);
17918 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919}
17920
17921/*
17922 * "substitute()" function
17923 */
17924 static void
17925f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017926 typval_T *argvars;
17927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017928{
17929 char_u patbuf[NUMBUFLEN];
17930 char_u subbuf[NUMBUFLEN];
17931 char_u flagsbuf[NUMBUFLEN];
17932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933 char_u *str = get_tv_string_chk(&argvars[0]);
17934 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17935 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17936 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17937
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017939 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17940 rettv->vval.v_string = NULL;
17941 else
17942 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017943}
17944
17945/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017946 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952{
17953 int id = 0;
17954#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017955 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956 long col;
17957 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017958 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017960 lnum = get_tv_lnum(argvars); /* -1 on type error */
17961 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17962 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017964 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017965 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017966 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967#endif
17968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017969 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970}
17971
17972/*
17973 * "synIDattr(id, what [, mode])" function
17974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017977 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979{
17980 char_u *p = NULL;
17981#ifdef FEAT_SYN_HL
17982 int id;
17983 char_u *what;
17984 char_u *mode;
17985 char_u modebuf[NUMBUFLEN];
17986 int modec;
17987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017988 id = get_tv_number(&argvars[0]);
17989 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017990 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017994 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 modec = 0; /* replace invalid with current */
17996 }
17997 else
17998 {
17999#ifdef FEAT_GUI
18000 if (gui.in_use)
18001 modec = 'g';
18002 else
18003#endif
18004 if (t_colors > 1)
18005 modec = 'c';
18006 else
18007 modec = 't';
18008 }
18009
18010
18011 switch (TOLOWER_ASC(what[0]))
18012 {
18013 case 'b':
18014 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18015 p = highlight_color(id, what, modec);
18016 else /* bold */
18017 p = highlight_has_attr(id, HL_BOLD, modec);
18018 break;
18019
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018020 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 p = highlight_color(id, what, modec);
18022 break;
18023
18024 case 'i':
18025 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18026 p = highlight_has_attr(id, HL_INVERSE, modec);
18027 else /* italic */
18028 p = highlight_has_attr(id, HL_ITALIC, modec);
18029 break;
18030
18031 case 'n': /* name */
18032 p = get_highlight_name(NULL, id - 1);
18033 break;
18034
18035 case 'r': /* reverse */
18036 p = highlight_has_attr(id, HL_INVERSE, modec);
18037 break;
18038
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018039 case 's':
18040 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18041 p = highlight_color(id, what, modec);
18042 else /* standout */
18043 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 break;
18045
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018046 case 'u':
18047 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18048 /* underline */
18049 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18050 else
18051 /* undercurl */
18052 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 break;
18054 }
18055
18056 if (p != NULL)
18057 p = vim_strsave(p);
18058#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 rettv->v_type = VAR_STRING;
18060 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061}
18062
18063/*
18064 * "synIDtrans(id)" function
18065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018068 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070{
18071 int id;
18072
18073#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075
18076 if (id > 0)
18077 id = syn_get_final_id(id);
18078 else
18079#endif
18080 id = 0;
18081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018082 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083}
18084
18085/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018086 * "synconcealed(lnum, col)" function
18087 */
18088 static void
18089f_synconcealed(argvars, rettv)
18090 typval_T *argvars UNUSED;
18091 typval_T *rettv;
18092{
18093#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18094 long lnum;
18095 long col;
18096 int syntax_flags = 0;
18097 int cchar;
18098 int matchid = 0;
18099 char_u str[NUMBUFLEN];
18100#endif
18101
18102 rettv->v_type = VAR_LIST;
18103 rettv->vval.v_list = NULL;
18104
18105#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18106 lnum = get_tv_lnum(argvars); /* -1 on type error */
18107 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18108
18109 vim_memset(str, NUL, sizeof(str));
18110
18111 if (rettv_list_alloc(rettv) != FAIL)
18112 {
18113 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18114 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18115 && curwin->w_p_cole > 0)
18116 {
18117 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18118 syntax_flags = get_syntax_info(&matchid);
18119
18120 /* get the conceal character */
18121 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18122 {
18123 cchar = syn_get_sub_char();
18124 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18125 cchar = lcs_conceal;
18126 if (cchar != NUL)
18127 {
18128# ifdef FEAT_MBYTE
18129 if (has_mbyte)
18130 (*mb_char2bytes)(cchar, str);
18131 else
18132# endif
18133 str[0] = cchar;
18134 }
18135 }
18136 }
18137
18138 list_append_number(rettv->vval.v_list,
18139 (syntax_flags & HL_CONCEAL) != 0);
18140 /* -1 to auto-determine strlen */
18141 list_append_string(rettv->vval.v_list, str, -1);
18142 list_append_number(rettv->vval.v_list, matchid);
18143 }
18144#endif
18145}
18146
18147/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018148 * "synstack(lnum, col)" function
18149 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018150 static void
18151f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018152 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018153 typval_T *rettv;
18154{
18155#ifdef FEAT_SYN_HL
18156 long lnum;
18157 long col;
18158 int i;
18159 int id;
18160#endif
18161
18162 rettv->v_type = VAR_LIST;
18163 rettv->vval.v_list = NULL;
18164
18165#ifdef FEAT_SYN_HL
18166 lnum = get_tv_lnum(argvars); /* -1 on type error */
18167 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18168
18169 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018170 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018171 && rettv_list_alloc(rettv) != FAIL)
18172 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018173 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018174 for (i = 0; ; ++i)
18175 {
18176 id = syn_get_stack_item(i);
18177 if (id < 0)
18178 break;
18179 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18180 break;
18181 }
18182 }
18183#endif
18184}
18185
18186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 * "system()" function
18188 */
18189 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018190f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018191 typval_T *argvars;
18192 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018194 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018196 char_u *infile = NULL;
18197 char_u buf[NUMBUFLEN];
18198 int err = FALSE;
18199 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018201 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018202 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018203
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018205 {
18206 /*
18207 * Write the string to a temp file, to be used for input of the shell
18208 * command.
18209 */
18210 if ((infile = vim_tempname('i')) == NULL)
18211 {
18212 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018213 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018214 }
18215
18216 fd = mch_fopen((char *)infile, WRITEBIN);
18217 if (fd == NULL)
18218 {
18219 EMSG2(_(e_notopen), infile);
18220 goto done;
18221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018222 p = get_tv_string_buf_chk(&argvars[1], buf);
18223 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018224 {
18225 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018226 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018227 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018228 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18229 err = TRUE;
18230 if (fclose(fd) != 0)
18231 err = TRUE;
18232 if (err)
18233 {
18234 EMSG(_("E677: Error writing temp file"));
18235 goto done;
18236 }
18237 }
18238
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018239 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18240 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018241
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242#ifdef USE_CR
18243 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018244 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 {
18246 char_u *s;
18247
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018248 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 {
18250 if (*s == CAR)
18251 *s = NL;
18252 }
18253 }
18254#else
18255# ifdef USE_CRNL
18256 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018257 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 {
18259 char_u *s, *d;
18260
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018261 d = res;
18262 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 {
18264 if (s[0] == CAR && s[1] == NL)
18265 ++s;
18266 *d++ = *s;
18267 }
18268 *d = NUL;
18269 }
18270# endif
18271#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018272
18273done:
18274 if (infile != NULL)
18275 {
18276 mch_remove(infile);
18277 vim_free(infile);
18278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018279 rettv->v_type = VAR_STRING;
18280 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281}
18282
18283/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018284 * "tabpagebuflist()" function
18285 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018286 static void
18287f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018288 typval_T *argvars UNUSED;
18289 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018290{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018291#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018292 tabpage_T *tp;
18293 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018294
18295 if (argvars[0].v_type == VAR_UNKNOWN)
18296 wp = firstwin;
18297 else
18298 {
18299 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18300 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018301 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018302 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018303 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018304 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018305 for (; wp != NULL; wp = wp->w_next)
18306 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018307 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018308 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018309 }
18310#endif
18311}
18312
18313
18314/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018315 * "tabpagenr()" function
18316 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018317 static void
18318f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018319 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018320 typval_T *rettv;
18321{
18322 int nr = 1;
18323#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018324 char_u *arg;
18325
18326 if (argvars[0].v_type != VAR_UNKNOWN)
18327 {
18328 arg = get_tv_string_chk(&argvars[0]);
18329 nr = 0;
18330 if (arg != NULL)
18331 {
18332 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018333 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018334 else
18335 EMSG2(_(e_invexpr2), arg);
18336 }
18337 }
18338 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018339 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018340#endif
18341 rettv->vval.v_number = nr;
18342}
18343
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018344
18345#ifdef FEAT_WINDOWS
18346static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18347
18348/*
18349 * Common code for tabpagewinnr() and winnr().
18350 */
18351 static int
18352get_winnr(tp, argvar)
18353 tabpage_T *tp;
18354 typval_T *argvar;
18355{
18356 win_T *twin;
18357 int nr = 1;
18358 win_T *wp;
18359 char_u *arg;
18360
18361 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18362 if (argvar->v_type != VAR_UNKNOWN)
18363 {
18364 arg = get_tv_string_chk(argvar);
18365 if (arg == NULL)
18366 nr = 0; /* type error; errmsg already given */
18367 else if (STRCMP(arg, "$") == 0)
18368 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18369 else if (STRCMP(arg, "#") == 0)
18370 {
18371 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18372 if (twin == NULL)
18373 nr = 0;
18374 }
18375 else
18376 {
18377 EMSG2(_(e_invexpr2), arg);
18378 nr = 0;
18379 }
18380 }
18381
18382 if (nr > 0)
18383 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18384 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018385 {
18386 if (wp == NULL)
18387 {
18388 /* didn't find it in this tabpage */
18389 nr = 0;
18390 break;
18391 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018392 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018393 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018394 return nr;
18395}
18396#endif
18397
18398/*
18399 * "tabpagewinnr()" function
18400 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018401 static void
18402f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018403 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018404 typval_T *rettv;
18405{
18406 int nr = 1;
18407#ifdef FEAT_WINDOWS
18408 tabpage_T *tp;
18409
18410 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18411 if (tp == NULL)
18412 nr = 0;
18413 else
18414 nr = get_winnr(tp, &argvars[1]);
18415#endif
18416 rettv->vval.v_number = nr;
18417}
18418
18419
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018420/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018421 * "tagfiles()" function
18422 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018423 static void
18424f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018425 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018426 typval_T *rettv;
18427{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018428 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018429 tagname_T tn;
18430 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018431
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018432 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018433 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018434 fname = alloc(MAXPATHL);
18435 if (fname == NULL)
18436 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018437
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018438 for (first = TRUE; ; first = FALSE)
18439 if (get_tagfname(&tn, first, fname) == FAIL
18440 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018441 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018442 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018443 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018444}
18445
18446/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018447 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018448 */
18449 static void
18450f_taglist(argvars, rettv)
18451 typval_T *argvars;
18452 typval_T *rettv;
18453{
18454 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018455
18456 tag_pattern = get_tv_string(&argvars[0]);
18457
18458 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 if (*tag_pattern == NUL)
18460 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018461
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018462 if (rettv_list_alloc(rettv) == OK)
18463 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018464}
18465
18466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 * "tempname()" function
18468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018471 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473{
18474 static int x = 'A';
18475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476 rettv->v_type = VAR_STRING;
18477 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478
18479 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18480 * names. Skip 'I' and 'O', they are used for shell redirection. */
18481 do
18482 {
18483 if (x == 'Z')
18484 x = '0';
18485 else if (x == '9')
18486 x = 'A';
18487 else
18488 {
18489#ifdef EBCDIC
18490 if (x == 'I')
18491 x = 'J';
18492 else if (x == 'R')
18493 x = 'S';
18494 else
18495#endif
18496 ++x;
18497 }
18498 } while (x == 'I' || x == 'O');
18499}
18500
18501/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018502 * "test(list)" function: Just checking the walls...
18503 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018504 static void
18505f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018506 typval_T *argvars UNUSED;
18507 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018508{
18509 /* Used for unit testing. Change the code below to your liking. */
18510#if 0
18511 listitem_T *li;
18512 list_T *l;
18513 char_u *bad, *good;
18514
18515 if (argvars[0].v_type != VAR_LIST)
18516 return;
18517 l = argvars[0].vval.v_list;
18518 if (l == NULL)
18519 return;
18520 li = l->lv_first;
18521 if (li == NULL)
18522 return;
18523 bad = get_tv_string(&li->li_tv);
18524 li = li->li_next;
18525 if (li == NULL)
18526 return;
18527 good = get_tv_string(&li->li_tv);
18528 rettv->vval.v_number = test_edit_score(bad, good);
18529#endif
18530}
18531
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018532#ifdef FEAT_FLOAT
18533/*
18534 * "tan()" function
18535 */
18536 static void
18537f_tan(argvars, rettv)
18538 typval_T *argvars;
18539 typval_T *rettv;
18540{
18541 float_T f;
18542
18543 rettv->v_type = VAR_FLOAT;
18544 if (get_float_arg(argvars, &f) == OK)
18545 rettv->vval.v_float = tan(f);
18546 else
18547 rettv->vval.v_float = 0.0;
18548}
18549
18550/*
18551 * "tanh()" function
18552 */
18553 static void
18554f_tanh(argvars, rettv)
18555 typval_T *argvars;
18556 typval_T *rettv;
18557{
18558 float_T f;
18559
18560 rettv->v_type = VAR_FLOAT;
18561 if (get_float_arg(argvars, &f) == OK)
18562 rettv->vval.v_float = tanh(f);
18563 else
18564 rettv->vval.v_float = 0.0;
18565}
18566#endif
18567
Bram Moolenaard52d9742005-08-21 22:20:28 +000018568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 * "tolower(string)" function
18570 */
18571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018573 typval_T *argvars;
18574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575{
18576 char_u *p;
18577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578 p = vim_strsave(get_tv_string(&argvars[0]));
18579 rettv->v_type = VAR_STRING;
18580 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581
18582 if (p != NULL)
18583 while (*p != NUL)
18584 {
18585#ifdef FEAT_MBYTE
18586 int l;
18587
18588 if (enc_utf8)
18589 {
18590 int c, lc;
18591
18592 c = utf_ptr2char(p);
18593 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018594 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 /* TODO: reallocate string when byte count changes. */
18596 if (utf_char2len(lc) == l)
18597 utf_char2bytes(lc, p);
18598 p += l;
18599 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018600 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601 p += l; /* skip multi-byte character */
18602 else
18603#endif
18604 {
18605 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18606 ++p;
18607 }
18608 }
18609}
18610
18611/*
18612 * "toupper(string)" function
18613 */
18614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018616 typval_T *argvars;
18617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621}
18622
18623/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018624 * "tr(string, fromstr, tostr)" function
18625 */
18626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018628 typval_T *argvars;
18629 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018630{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018631 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018632 char_u *fromstr;
18633 char_u *tostr;
18634 char_u *p;
18635#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018636 int inlen;
18637 int fromlen;
18638 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018639 int idx;
18640 char_u *cpstr;
18641 int cplen;
18642 int first = TRUE;
18643#endif
18644 char_u buf[NUMBUFLEN];
18645 char_u buf2[NUMBUFLEN];
18646 garray_T ga;
18647
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018648 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18650 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018651
18652 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653 rettv->v_type = VAR_STRING;
18654 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018655 if (fromstr == NULL || tostr == NULL)
18656 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018657 ga_init2(&ga, (int)sizeof(char), 80);
18658
18659#ifdef FEAT_MBYTE
18660 if (!has_mbyte)
18661#endif
18662 /* not multi-byte: fromstr and tostr must be the same length */
18663 if (STRLEN(fromstr) != STRLEN(tostr))
18664 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018665#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018666error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018667#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018668 EMSG2(_(e_invarg2), fromstr);
18669 ga_clear(&ga);
18670 return;
18671 }
18672
18673 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018674 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018675 {
18676#ifdef FEAT_MBYTE
18677 if (has_mbyte)
18678 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018679 inlen = (*mb_ptr2len)(in_str);
18680 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018681 cplen = inlen;
18682 idx = 0;
18683 for (p = fromstr; *p != NUL; p += fromlen)
18684 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018685 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018686 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018687 {
18688 for (p = tostr; *p != NUL; p += tolen)
18689 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018690 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018691 if (idx-- == 0)
18692 {
18693 cplen = tolen;
18694 cpstr = p;
18695 break;
18696 }
18697 }
18698 if (*p == NUL) /* tostr is shorter than fromstr */
18699 goto error;
18700 break;
18701 }
18702 ++idx;
18703 }
18704
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018705 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018706 {
18707 /* Check that fromstr and tostr have the same number of
18708 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018709 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018710 first = FALSE;
18711 for (p = tostr; *p != NUL; p += tolen)
18712 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018713 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018714 --idx;
18715 }
18716 if (idx != 0)
18717 goto error;
18718 }
18719
18720 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018721 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018722 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018723
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018724 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018725 }
18726 else
18727#endif
18728 {
18729 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018730 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018731 if (p != NULL)
18732 ga_append(&ga, tostr[p - fromstr]);
18733 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018734 ga_append(&ga, *in_str);
18735 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018736 }
18737 }
18738
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018739 /* add a terminating NUL */
18740 ga_grow(&ga, 1);
18741 ga_append(&ga, NUL);
18742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018744}
18745
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018746#ifdef FEAT_FLOAT
18747/*
18748 * "trunc({float})" function
18749 */
18750 static void
18751f_trunc(argvars, rettv)
18752 typval_T *argvars;
18753 typval_T *rettv;
18754{
18755 float_T f;
18756
18757 rettv->v_type = VAR_FLOAT;
18758 if (get_float_arg(argvars, &f) == OK)
18759 /* trunc() is not in C90, use floor() or ceil() instead. */
18760 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18761 else
18762 rettv->vval.v_float = 0.0;
18763}
18764#endif
18765
Bram Moolenaar8299df92004-07-10 09:47:34 +000018766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 * "type(expr)" function
18768 */
18769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018770f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *argvars;
18772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018774 int n;
18775
18776 switch (argvars[0].v_type)
18777 {
18778 case VAR_NUMBER: n = 0; break;
18779 case VAR_STRING: n = 1; break;
18780 case VAR_FUNC: n = 2; break;
18781 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018782 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018783#ifdef FEAT_FLOAT
18784 case VAR_FLOAT: n = 5; break;
18785#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018786 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18787 }
18788 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018792 * "undofile(name)" function
18793 */
18794 static void
18795f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018796 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018797 typval_T *rettv;
18798{
18799 rettv->v_type = VAR_STRING;
18800#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018801 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018802 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018803
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018804 if (*fname == NUL)
18805 {
18806 /* If there is no file name there will be no undo file. */
18807 rettv->vval.v_string = NULL;
18808 }
18809 else
18810 {
18811 char_u *ffname = FullName_save(fname, FALSE);
18812
18813 if (ffname != NULL)
18814 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18815 vim_free(ffname);
18816 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018817 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018818#else
18819 rettv->vval.v_string = NULL;
18820#endif
18821}
18822
18823/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018824 * "undotree()" function
18825 */
18826 static void
18827f_undotree(argvars, rettv)
18828 typval_T *argvars UNUSED;
18829 typval_T *rettv;
18830{
18831 if (rettv_dict_alloc(rettv) == OK)
18832 {
18833 dict_T *dict = rettv->vval.v_dict;
18834 list_T *list;
18835
Bram Moolenaar730cde92010-06-27 05:18:54 +020018836 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018837 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018838 dict_add_nr_str(dict, "save_last",
18839 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018840 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18841 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018842 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018843
18844 list = list_alloc();
18845 if (list != NULL)
18846 {
18847 u_eval_tree(curbuf->b_u_oldhead, list);
18848 dict_add_list(dict, "entries", list);
18849 }
18850 }
18851}
18852
18853/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018854 * "values(dict)" function
18855 */
18856 static void
18857f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018858 typval_T *argvars;
18859 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018860{
18861 dict_list(argvars, rettv, 1);
18862}
18863
18864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 * "virtcol(string)" function
18866 */
18867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018868f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018869 typval_T *argvars;
18870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871{
18872 colnr_T vcol = 0;
18873 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018874 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018876 fp = var2fpos(&argvars[0], FALSE, &fnum);
18877 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18878 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 {
18880 getvvcol(curwin, fp, NULL, NULL, &vcol);
18881 ++vcol;
18882 }
18883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885}
18886
18887/*
18888 * "visualmode()" function
18889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018892 typval_T *argvars UNUSED;
18893 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 char_u str[2];
18896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 str[0] = curbuf->b_visual_mode_eval;
18899 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018900 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901
18902 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018903 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905}
18906
18907/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018908 * "wildmenumode()" function
18909 */
18910 static void
18911f_wildmenumode(argvars, rettv)
18912 typval_T *argvars UNUSED;
18913 typval_T *rettv UNUSED;
18914{
18915#ifdef FEAT_WILDMENU
18916 if (wild_menu_showing)
18917 rettv->vval.v_number = 1;
18918#endif
18919}
18920
18921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 * "winbufnr(nr)" function
18923 */
18924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 typval_T *argvars;
18927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
18929 win_T *wp;
18930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018931 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018935 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936}
18937
18938/*
18939 * "wincol()" function
18940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018942f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018943 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945{
18946 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018947 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948}
18949
18950/*
18951 * "winheight(nr)" function
18952 */
18953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018955 typval_T *argvars;
18956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957{
18958 win_T *wp;
18959
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018960 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018964 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965}
18966
18967/*
18968 * "winline()" function
18969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018971f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018972 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974{
18975 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977}
18978
18979/*
18980 * "winnr()" function
18981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986{
18987 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018988
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018990 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
18995/*
18996 * "winrestcmd()" function
18997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018999f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002{
19003#ifdef FEAT_WINDOWS
19004 win_T *wp;
19005 int winnr = 1;
19006 garray_T ga;
19007 char_u buf[50];
19008
19009 ga_init2(&ga, (int)sizeof(char), 70);
19010 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19011 {
19012 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19013 ga_concat(&ga, buf);
19014# ifdef FEAT_VERTSPLIT
19015 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19016 ga_concat(&ga, buf);
19017# endif
19018 ++winnr;
19019 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019020 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019022 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019026 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027}
19028
19029/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019030 * "winrestview()" function
19031 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019032 static void
19033f_winrestview(argvars, rettv)
19034 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019035 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019036{
19037 dict_T *dict;
19038
19039 if (argvars[0].v_type != VAR_DICT
19040 || (dict = argvars[0].vval.v_dict) == NULL)
19041 EMSG(_(e_invarg));
19042 else
19043 {
19044 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19045 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19046#ifdef FEAT_VIRTUALEDIT
19047 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19048#endif
19049 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019050 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019051
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019052 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019053#ifdef FEAT_DIFF
19054 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19055#endif
19056 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19057 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19058
19059 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019060 win_new_height(curwin, curwin->w_height);
19061# ifdef FEAT_VERTSPLIT
19062 win_new_width(curwin, W_WIDTH(curwin));
19063# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019064 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019065
19066 if (curwin->w_topline == 0)
19067 curwin->w_topline = 1;
19068 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19069 curwin->w_topline = curbuf->b_ml.ml_line_count;
19070#ifdef FEAT_DIFF
19071 check_topfill(curwin, TRUE);
19072#endif
19073 }
19074}
19075
19076/*
19077 * "winsaveview()" function
19078 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019079 static void
19080f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019081 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019082 typval_T *rettv;
19083{
19084 dict_T *dict;
19085
Bram Moolenaara800b422010-06-27 01:15:55 +020019086 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019087 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019088 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019089
19090 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19091 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19092#ifdef FEAT_VIRTUALEDIT
19093 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19094#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019095 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019096 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19097
19098 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19099#ifdef FEAT_DIFF
19100 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19101#endif
19102 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19103 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19104}
19105
19106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 * "winwidth(nr)" function
19108 */
19109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019110f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019111 typval_T *argvars;
19112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113{
19114 win_T *wp;
19115
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019116 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 else
19120#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124#endif
19125}
19126
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019128 * "writefile()" function
19129 */
19130 static void
19131f_writefile(argvars, rettv)
19132 typval_T *argvars;
19133 typval_T *rettv;
19134{
19135 int binary = FALSE;
19136 char_u *fname;
19137 FILE *fd;
19138 listitem_T *li;
19139 char_u *s;
19140 int ret = 0;
19141 int c;
19142
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019143 if (check_restricted() || check_secure())
19144 return;
19145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019146 if (argvars[0].v_type != VAR_LIST)
19147 {
19148 EMSG2(_(e_listarg), "writefile()");
19149 return;
19150 }
19151 if (argvars[0].vval.v_list == NULL)
19152 return;
19153
19154 if (argvars[2].v_type != VAR_UNKNOWN
19155 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19156 binary = TRUE;
19157
19158 /* Always open the file in binary mode, library functions have a mind of
19159 * their own about CR-LF conversion. */
19160 fname = get_tv_string(&argvars[1]);
19161 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19162 {
19163 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19164 ret = -1;
19165 }
19166 else
19167 {
19168 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19169 li = li->li_next)
19170 {
19171 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19172 {
19173 if (*s == '\n')
19174 c = putc(NUL, fd);
19175 else
19176 c = putc(*s, fd);
19177 if (c == EOF)
19178 {
19179 ret = -1;
19180 break;
19181 }
19182 }
19183 if (!binary || li->li_next != NULL)
19184 if (putc('\n', fd) == EOF)
19185 {
19186 ret = -1;
19187 break;
19188 }
19189 if (ret < 0)
19190 {
19191 EMSG(_(e_write));
19192 break;
19193 }
19194 }
19195 fclose(fd);
19196 }
19197
19198 rettv->vval.v_number = ret;
19199}
19200
19201/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019202 * "xor(expr, expr)" function
19203 */
19204 static void
19205f_xor(argvars, rettv)
19206 typval_T *argvars;
19207 typval_T *rettv;
19208{
19209 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19210 ^ get_tv_number_chk(&argvars[1], NULL);
19211}
19212
19213
19214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019216 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 */
19218 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019219var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019220 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019221 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019222 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019224 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019226 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227
Bram Moolenaara5525202006-03-02 22:52:09 +000019228 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019229 if (varp->v_type == VAR_LIST)
19230 {
19231 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019232 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019233 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019234 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019235
19236 l = varp->vval.v_list;
19237 if (l == NULL)
19238 return NULL;
19239
19240 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019241 pos.lnum = list_find_nr(l, 0L, &error);
19242 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019243 return NULL; /* invalid line number */
19244
19245 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019246 pos.col = list_find_nr(l, 1L, &error);
19247 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019248 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019249 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019250
19251 /* We accept "$" for the column number: last column. */
19252 li = list_find(l, 1L);
19253 if (li != NULL && li->li_tv.v_type == VAR_STRING
19254 && li->li_tv.vval.v_string != NULL
19255 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19256 pos.col = len + 1;
19257
Bram Moolenaara5525202006-03-02 22:52:09 +000019258 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019259 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019260 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019261 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019262
Bram Moolenaara5525202006-03-02 22:52:09 +000019263#ifdef FEAT_VIRTUALEDIT
19264 /* Get the virtual offset. Defaults to zero. */
19265 pos.coladd = list_find_nr(l, 2L, &error);
19266 if (error)
19267 pos.coladd = 0;
19268#endif
19269
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019270 return &pos;
19271 }
19272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 name = get_tv_string_chk(varp);
19274 if (name == NULL)
19275 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019276 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019278 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19279 {
19280 if (VIsual_active)
19281 return &VIsual;
19282 return &curwin->w_cursor;
19283 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019284 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019286 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19288 return NULL;
19289 return pp;
19290 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019291
19292#ifdef FEAT_VIRTUALEDIT
19293 pos.coladd = 0;
19294#endif
19295
Bram Moolenaar477933c2007-07-17 14:32:23 +000019296 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019297 {
19298 pos.col = 0;
19299 if (name[1] == '0') /* "w0": first visible line */
19300 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019301 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019302 pos.lnum = curwin->w_topline;
19303 return &pos;
19304 }
19305 else if (name[1] == '$') /* "w$": last visible line */
19306 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019307 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019308 pos.lnum = curwin->w_botline - 1;
19309 return &pos;
19310 }
19311 }
19312 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019314 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315 {
19316 pos.lnum = curbuf->b_ml.ml_line_count;
19317 pos.col = 0;
19318 }
19319 else
19320 {
19321 pos.lnum = curwin->w_cursor.lnum;
19322 pos.col = (colnr_T)STRLEN(ml_get_curline());
19323 }
19324 return &pos;
19325 }
19326 return NULL;
19327}
19328
19329/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019330 * Convert list in "arg" into a position and optional file number.
19331 * When "fnump" is NULL there is no file number, only 3 items.
19332 * Note that the column is passed on as-is, the caller may want to decrement
19333 * it to use 1 for the first column.
19334 * Return FAIL when conversion is not possible, doesn't check the position for
19335 * validity.
19336 */
19337 static int
19338list2fpos(arg, posp, fnump)
19339 typval_T *arg;
19340 pos_T *posp;
19341 int *fnump;
19342{
19343 list_T *l = arg->vval.v_list;
19344 long i = 0;
19345 long n;
19346
Bram Moolenaarbde35262006-07-23 20:12:24 +000019347 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19348 * when "fnump" isn't NULL and "coladd" is optional. */
19349 if (arg->v_type != VAR_LIST
19350 || l == NULL
19351 || l->lv_len < (fnump == NULL ? 2 : 3)
19352 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019353 return FAIL;
19354
19355 if (fnump != NULL)
19356 {
19357 n = list_find_nr(l, i++, NULL); /* fnum */
19358 if (n < 0)
19359 return FAIL;
19360 if (n == 0)
19361 n = curbuf->b_fnum; /* current buffer */
19362 *fnump = n;
19363 }
19364
19365 n = list_find_nr(l, i++, NULL); /* lnum */
19366 if (n < 0)
19367 return FAIL;
19368 posp->lnum = n;
19369
19370 n = list_find_nr(l, i++, NULL); /* col */
19371 if (n < 0)
19372 return FAIL;
19373 posp->col = n;
19374
19375#ifdef FEAT_VIRTUALEDIT
19376 n = list_find_nr(l, i, NULL);
19377 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019378 posp->coladd = 0;
19379 else
19380 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019381#endif
19382
19383 return OK;
19384}
19385
19386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 * Get the length of an environment variable name.
19388 * Advance "arg" to the first character after the name.
19389 * Return 0 for error.
19390 */
19391 static int
19392get_env_len(arg)
19393 char_u **arg;
19394{
19395 char_u *p;
19396 int len;
19397
19398 for (p = *arg; vim_isIDc(*p); ++p)
19399 ;
19400 if (p == *arg) /* no name found */
19401 return 0;
19402
19403 len = (int)(p - *arg);
19404 *arg = p;
19405 return len;
19406}
19407
19408/*
19409 * Get the length of the name of a function or internal variable.
19410 * "arg" is advanced to the first non-white character after the name.
19411 * Return 0 if something is wrong.
19412 */
19413 static int
19414get_id_len(arg)
19415 char_u **arg;
19416{
19417 char_u *p;
19418 int len;
19419
19420 /* Find the end of the name. */
19421 for (p = *arg; eval_isnamec(*p); ++p)
19422 ;
19423 if (p == *arg) /* no name found */
19424 return 0;
19425
19426 len = (int)(p - *arg);
19427 *arg = skipwhite(p);
19428
19429 return len;
19430}
19431
19432/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019433 * Get the length of the name of a variable or function.
19434 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019436 * Return -1 if curly braces expansion failed.
19437 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 * If the name contains 'magic' {}'s, expand them and return the
19439 * expanded name in an allocated string via 'alias' - caller must free.
19440 */
19441 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019442get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 char_u **arg;
19444 char_u **alias;
19445 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019446 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447{
19448 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 char_u *p;
19450 char_u *expr_start;
19451 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452
19453 *alias = NULL; /* default to no alias */
19454
19455 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19456 && (*arg)[2] == (int)KE_SNR)
19457 {
19458 /* hard coded <SNR>, already translated */
19459 *arg += 3;
19460 return get_id_len(arg) + 3;
19461 }
19462 len = eval_fname_script(*arg);
19463 if (len > 0)
19464 {
19465 /* literal "<SID>", "s:" or "<SNR>" */
19466 *arg += len;
19467 }
19468
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019470 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019472 p = find_name_end(*arg, &expr_start, &expr_end,
19473 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 if (expr_start != NULL)
19475 {
19476 char_u *temp_string;
19477
19478 if (!evaluate)
19479 {
19480 len += (int)(p - *arg);
19481 *arg = skipwhite(p);
19482 return len;
19483 }
19484
19485 /*
19486 * Include any <SID> etc in the expanded string:
19487 * Thus the -len here.
19488 */
19489 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19490 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019491 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 *alias = temp_string;
19493 *arg = skipwhite(p);
19494 return (int)STRLEN(temp_string);
19495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496
19497 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019498 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 EMSG2(_(e_invexpr2), *arg);
19500
19501 return len;
19502}
19503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504/*
19505 * Find the end of a variable or function name, taking care of magic braces.
19506 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19507 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019508 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 * Return a pointer to just after the name. Equal to "arg" if there is no
19510 * valid name.
19511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019513find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 char_u *arg;
19515 char_u **expr_start;
19516 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019517 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019519 int mb_nest = 0;
19520 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 char_u *p;
19522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019525 *expr_start = NULL;
19526 *expr_end = NULL;
19527 }
19528
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019529 /* Quick check for valid starting character. */
19530 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19531 return arg;
19532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019533 for (p = arg; *p != NUL
19534 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019535 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019536 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019538 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019540 if (*p == '\'')
19541 {
19542 /* skip over 'string' to avoid counting [ and ] inside it. */
19543 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19544 ;
19545 if (*p == NUL)
19546 break;
19547 }
19548 else if (*p == '"')
19549 {
19550 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19551 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19552 if (*p == '\\' && p[1] != NUL)
19553 ++p;
19554 if (*p == NUL)
19555 break;
19556 }
19557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 if (*p == '[')
19561 ++br_nest;
19562 else if (*p == ']')
19563 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019566 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019568 if (*p == '{')
19569 {
19570 mb_nest++;
19571 if (expr_start != NULL && *expr_start == NULL)
19572 *expr_start = p;
19573 }
19574 else if (*p == '}')
19575 {
19576 mb_nest--;
19577 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19578 *expr_end = p;
19579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 }
19582
19583 return p;
19584}
19585
19586/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019587 * Expands out the 'magic' {}'s in a variable/function name.
19588 * Note that this can call itself recursively, to deal with
19589 * constructs like foo{bar}{baz}{bam}
19590 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19591 * "in_start" ^
19592 * "expr_start" ^
19593 * "expr_end" ^
19594 * "in_end" ^
19595 *
19596 * Returns a new allocated string, which the caller must free.
19597 * Returns NULL for failure.
19598 */
19599 static char_u *
19600make_expanded_name(in_start, expr_start, expr_end, in_end)
19601 char_u *in_start;
19602 char_u *expr_start;
19603 char_u *expr_end;
19604 char_u *in_end;
19605{
19606 char_u c1;
19607 char_u *retval = NULL;
19608 char_u *temp_result;
19609 char_u *nextcmd = NULL;
19610
19611 if (expr_end == NULL || in_end == NULL)
19612 return NULL;
19613 *expr_start = NUL;
19614 *expr_end = NUL;
19615 c1 = *in_end;
19616 *in_end = NUL;
19617
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019618 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019619 if (temp_result != NULL && nextcmd == NULL)
19620 {
19621 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19622 + (in_end - expr_end) + 1));
19623 if (retval != NULL)
19624 {
19625 STRCPY(retval, in_start);
19626 STRCAT(retval, temp_result);
19627 STRCAT(retval, expr_end + 1);
19628 }
19629 }
19630 vim_free(temp_result);
19631
19632 *in_end = c1; /* put char back for error messages */
19633 *expr_start = '{';
19634 *expr_end = '}';
19635
19636 if (retval != NULL)
19637 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019638 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019639 if (expr_start != NULL)
19640 {
19641 /* Further expansion! */
19642 temp_result = make_expanded_name(retval, expr_start,
19643 expr_end, temp_result);
19644 vim_free(retval);
19645 retval = temp_result;
19646 }
19647 }
19648
19649 return retval;
19650}
19651
19652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019654 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 */
19656 static int
19657eval_isnamec(c)
19658 int c;
19659{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019660 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19661}
19662
19663/*
19664 * Return TRUE if character "c" can be used as the first character in a
19665 * variable or function name (excluding '{' and '}').
19666 */
19667 static int
19668eval_isnamec1(c)
19669 int c;
19670{
19671 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672}
19673
19674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 * Set number v: variable to "val".
19676 */
19677 void
19678set_vim_var_nr(idx, val)
19679 int idx;
19680 long val;
19681{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019682 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683}
19684
19685/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019686 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 */
19688 long
19689get_vim_var_nr(idx)
19690 int idx;
19691{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019692 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693}
19694
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019695/*
19696 * Get string v: variable value. Uses a static buffer, can only be used once.
19697 */
19698 char_u *
19699get_vim_var_str(idx)
19700 int idx;
19701{
19702 return get_tv_string(&vimvars[idx].vv_tv);
19703}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019704
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019706 * Get List v: variable value. Caller must take care of reference count when
19707 * needed.
19708 */
19709 list_T *
19710get_vim_var_list(idx)
19711 int idx;
19712{
19713 return vimvars[idx].vv_list;
19714}
19715
19716/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019717 * Set v:char to character "c".
19718 */
19719 void
19720set_vim_var_char(c)
19721 int c;
19722{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019723 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019724
19725#ifdef FEAT_MBYTE
19726 if (has_mbyte)
19727 buf[(*mb_char2bytes)(c, buf)] = NUL;
19728 else
19729#endif
19730 {
19731 buf[0] = c;
19732 buf[1] = NUL;
19733 }
19734 set_vim_var_string(VV_CHAR, buf, -1);
19735}
19736
19737/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019738 * Set v:count to "count" and v:count1 to "count1".
19739 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 */
19741 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019742set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 long count;
19744 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019745 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019747 if (set_prevcount)
19748 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019749 vimvars[VV_COUNT].vv_nr = count;
19750 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751}
19752
19753/*
19754 * Set string v: variable to a copy of "val".
19755 */
19756 void
19757set_vim_var_string(idx, val, len)
19758 int idx;
19759 char_u *val;
19760 int len; /* length of "val" to use or -1 (whole string) */
19761{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019762 /* Need to do this (at least) once, since we can't initialize a union.
19763 * Will always be invoked when "v:progname" is set. */
19764 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19765
Bram Moolenaare9a41262005-01-15 22:18:47 +000019766 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019768 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019770 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019772 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773}
19774
19775/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019776 * Set List v: variable to "val".
19777 */
19778 void
19779set_vim_var_list(idx, val)
19780 int idx;
19781 list_T *val;
19782{
19783 list_unref(vimvars[idx].vv_list);
19784 vimvars[idx].vv_list = val;
19785 if (val != NULL)
19786 ++val->lv_refcount;
19787}
19788
19789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 * Set v:register if needed.
19791 */
19792 void
19793set_reg_var(c)
19794 int c;
19795{
19796 char_u regname;
19797
19798 if (c == 0 || c == ' ')
19799 regname = '"';
19800 else
19801 regname = c;
19802 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019803 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 set_vim_var_string(VV_REG, &regname, 1);
19805}
19806
19807/*
19808 * Get or set v:exception. If "oldval" == NULL, return the current value.
19809 * Otherwise, restore the value to "oldval" and return NULL.
19810 * Must always be called in pairs to save and restore v:exception! Does not
19811 * take care of memory allocations.
19812 */
19813 char_u *
19814v_exception(oldval)
19815 char_u *oldval;
19816{
19817 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019818 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819
Bram Moolenaare9a41262005-01-15 22:18:47 +000019820 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 return NULL;
19822}
19823
19824/*
19825 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19826 * Otherwise, restore the value to "oldval" and return NULL.
19827 * Must always be called in pairs to save and restore v:throwpoint! Does not
19828 * take care of memory allocations.
19829 */
19830 char_u *
19831v_throwpoint(oldval)
19832 char_u *oldval;
19833{
19834 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019835 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836
Bram Moolenaare9a41262005-01-15 22:18:47 +000019837 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 return NULL;
19839}
19840
19841#if defined(FEAT_AUTOCMD) || defined(PROTO)
19842/*
19843 * Set v:cmdarg.
19844 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19845 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19846 * Must always be called in pairs!
19847 */
19848 char_u *
19849set_cmdarg(eap, oldarg)
19850 exarg_T *eap;
19851 char_u *oldarg;
19852{
19853 char_u *oldval;
19854 char_u *newval;
19855 unsigned len;
19856
Bram Moolenaare9a41262005-01-15 22:18:47 +000019857 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019858 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019860 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019861 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019862 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 }
19864
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019865 if (eap->force_bin == FORCE_BIN)
19866 len = 6;
19867 else if (eap->force_bin == FORCE_NOBIN)
19868 len = 8;
19869 else
19870 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019871
19872 if (eap->read_edit)
19873 len += 7;
19874
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019875 if (eap->force_ff != 0)
19876 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19877# ifdef FEAT_MBYTE
19878 if (eap->force_enc != 0)
19879 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019880 if (eap->bad_char != 0)
19881 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019882# endif
19883
19884 newval = alloc(len + 1);
19885 if (newval == NULL)
19886 return NULL;
19887
19888 if (eap->force_bin == FORCE_BIN)
19889 sprintf((char *)newval, " ++bin");
19890 else if (eap->force_bin == FORCE_NOBIN)
19891 sprintf((char *)newval, " ++nobin");
19892 else
19893 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019894
19895 if (eap->read_edit)
19896 STRCAT(newval, " ++edit");
19897
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019898 if (eap->force_ff != 0)
19899 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19900 eap->cmd + eap->force_ff);
19901# ifdef FEAT_MBYTE
19902 if (eap->force_enc != 0)
19903 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19904 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019905 if (eap->bad_char == BAD_KEEP)
19906 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19907 else if (eap->bad_char == BAD_DROP)
19908 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19909 else if (eap->bad_char != 0)
19910 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019911# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019912 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019913 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914}
19915#endif
19916
19917/*
19918 * Get the value of internal variable "name".
19919 * Return OK or FAIL.
19920 */
19921 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019922get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 char_u *name;
19924 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019926 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019927 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928{
19929 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 typval_T *tv = NULL;
19931 typval_T atv;
19932 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934
19935 /* truncate the name, so that we can use strcmp() */
19936 cc = name[len];
19937 name[len] = NUL;
19938
19939 /*
19940 * Check for "b:changedtick".
19941 */
19942 if (STRCMP(name, "b:changedtick") == 0)
19943 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019944 atv.v_type = VAR_NUMBER;
19945 atv.vval.v_number = curbuf->b_changedtick;
19946 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 }
19948
19949 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 * Check for user-defined variables.
19951 */
19952 else
19953 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019954 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019956 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 }
19958
Bram Moolenaare9a41262005-01-15 22:18:47 +000019959 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019961 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 ret = FAIL;
19964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019966 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967
19968 name[len] = cc;
19969
19970 return ret;
19971}
19972
19973/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019974 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19975 * Also handle function call with Funcref variable: func(expr)
19976 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19977 */
19978 static int
19979handle_subscript(arg, rettv, evaluate, verbose)
19980 char_u **arg;
19981 typval_T *rettv;
19982 int evaluate; /* do more than finding the end */
19983 int verbose; /* give error messages */
19984{
19985 int ret = OK;
19986 dict_T *selfdict = NULL;
19987 char_u *s;
19988 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019989 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019990
19991 while (ret == OK
19992 && (**arg == '['
19993 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019994 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019995 && !vim_iswhite(*(*arg - 1)))
19996 {
19997 if (**arg == '(')
19998 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019999 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020000 if (evaluate)
20001 {
20002 functv = *rettv;
20003 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020004
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020005 /* Invoke the function. Recursive! */
20006 s = functv.vval.v_string;
20007 }
20008 else
20009 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020010 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020011 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20012 &len, evaluate, selfdict);
20013
20014 /* Clear the funcref afterwards, so that deleting it while
20015 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020016 if (evaluate)
20017 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020018
20019 /* Stop the expression evaluation when immediately aborting on
20020 * error, or when an interrupt occurred or an exception was thrown
20021 * but not caught. */
20022 if (aborting())
20023 {
20024 if (ret == OK)
20025 clear_tv(rettv);
20026 ret = FAIL;
20027 }
20028 dict_unref(selfdict);
20029 selfdict = NULL;
20030 }
20031 else /* **arg == '[' || **arg == '.' */
20032 {
20033 dict_unref(selfdict);
20034 if (rettv->v_type == VAR_DICT)
20035 {
20036 selfdict = rettv->vval.v_dict;
20037 if (selfdict != NULL)
20038 ++selfdict->dv_refcount;
20039 }
20040 else
20041 selfdict = NULL;
20042 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20043 {
20044 clear_tv(rettv);
20045 ret = FAIL;
20046 }
20047 }
20048 }
20049 dict_unref(selfdict);
20050 return ret;
20051}
20052
20053/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020054 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020055 * value).
20056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020059{
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020061}
20062
20063/*
20064 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 * The string "s" must have been allocated, it is consumed.
20066 * Return NULL for out of memory, the variable otherwise.
20067 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020068 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020069alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 char_u *s;
20071{
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020074 rettv = alloc_tv();
20075 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 rettv->v_type = VAR_STRING;
20078 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 }
20080 else
20081 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083}
20084
20085/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020086 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020088 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020090 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091{
20092 if (varp != NULL)
20093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 switch (varp->v_type)
20095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020096 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020097 func_unref(varp->vval.v_string);
20098 /*FALLTHROUGH*/
20099 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020100 vim_free(varp->vval.v_string);
20101 break;
20102 case VAR_LIST:
20103 list_unref(varp->vval.v_list);
20104 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020105 case VAR_DICT:
20106 dict_unref(varp->vval.v_dict);
20107 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020108 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020109#ifdef FEAT_FLOAT
20110 case VAR_FLOAT:
20111#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020112 case VAR_UNKNOWN:
20113 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020114 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020115 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020116 break;
20117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 vim_free(varp);
20119 }
20120}
20121
20122/*
20123 * Free the memory for a variable value and set the value to NULL or 0.
20124 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020125 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020126clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128{
20129 if (varp != NULL)
20130 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020131 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020133 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020134 func_unref(varp->vval.v_string);
20135 /*FALLTHROUGH*/
20136 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020137 vim_free(varp->vval.v_string);
20138 varp->vval.v_string = NULL;
20139 break;
20140 case VAR_LIST:
20141 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020142 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020143 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020144 case VAR_DICT:
20145 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020146 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020147 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020149 varp->vval.v_number = 0;
20150 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020151#ifdef FEAT_FLOAT
20152 case VAR_FLOAT:
20153 varp->vval.v_float = 0.0;
20154 break;
20155#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020156 case VAR_UNKNOWN:
20157 break;
20158 default:
20159 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020161 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 }
20163}
20164
20165/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020166 * Set the value of a variable to NULL without freeing items.
20167 */
20168 static void
20169init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020171{
20172 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174}
20175
20176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 * Get the number value of a variable.
20178 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020179 * For incompatible types, return 0.
20180 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20181 * caller of incompatible types: it sets *denote to TRUE if "denote"
20182 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 */
20184 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020185get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020188 int error = FALSE;
20189
20190 return get_tv_number_chk(varp, &error); /* return 0L on error */
20191}
20192
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020193 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020194get_tv_number_chk(varp, denote)
20195 typval_T *varp;
20196 int *denote;
20197{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020198 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020200 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020202 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020203 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020204#ifdef FEAT_FLOAT
20205 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020206 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020207 break;
20208#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020209 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020210 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020211 break;
20212 case VAR_STRING:
20213 if (varp->vval.v_string != NULL)
20214 vim_str2nr(varp->vval.v_string, NULL, NULL,
20215 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020216 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020217 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020218 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020219 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020220 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020221 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020222 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020223 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020224 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020227 if (denote == NULL) /* useful for values that must be unsigned */
20228 n = -1;
20229 else
20230 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020231 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232}
20233
20234/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020235 * Get the lnum from the first argument.
20236 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020237 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 */
20239 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020240get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242{
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244 linenr_T lnum;
20245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020246 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 if (lnum == 0) /* no valid number, try using line() */
20248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 rettv.v_type = VAR_NUMBER;
20250 f_line(argvars, &rettv);
20251 lnum = rettv.vval.v_number;
20252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 }
20254 return lnum;
20255}
20256
20257/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020258 * Get the lnum from the first argument.
20259 * Also accepts "$", then "buf" is used.
20260 * Returns 0 on error.
20261 */
20262 static linenr_T
20263get_tv_lnum_buf(argvars, buf)
20264 typval_T *argvars;
20265 buf_T *buf;
20266{
20267 if (argvars[0].v_type == VAR_STRING
20268 && argvars[0].vval.v_string != NULL
20269 && argvars[0].vval.v_string[0] == '$'
20270 && buf != NULL)
20271 return buf->b_ml.ml_line_count;
20272 return get_tv_number_chk(&argvars[0], NULL);
20273}
20274
20275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 * Get the string value of a variable.
20277 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020278 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20279 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 * If the String variable has never been set, return an empty string.
20281 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020282 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20283 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 */
20285 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
20289 static char_u mybuf[NUMBUFLEN];
20290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020291 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
20294 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020295get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020297 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020299 char_u *res = get_tv_string_buf_chk(varp, buf);
20300
20301 return res != NULL ? res : (char_u *)"";
20302}
20303
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020304 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020305get_tv_string_chk(varp)
20306 typval_T *varp;
20307{
20308 static char_u mybuf[NUMBUFLEN];
20309
20310 return get_tv_string_buf_chk(varp, mybuf);
20311}
20312
20313 static char_u *
20314get_tv_string_buf_chk(varp, buf)
20315 typval_T *varp;
20316 char_u *buf;
20317{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020318 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020320 case VAR_NUMBER:
20321 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20322 return buf;
20323 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020324 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020325 break;
20326 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020327 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020328 break;
20329 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020330 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020331 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020332#ifdef FEAT_FLOAT
20333 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020334 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020335 break;
20336#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020337 case VAR_STRING:
20338 if (varp->vval.v_string != NULL)
20339 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020340 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020341 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020342 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020345 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346}
20347
20348/*
20349 * Find variable "name" in the list of variables.
20350 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020351 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020352 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020353 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020356find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020359 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
Bram Moolenaara7043832005-01-21 11:56:39 +000020364 ht = find_var_ht(name, &varname);
20365 if (htp != NULL)
20366 *htp = ht;
20367 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020369 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370}
20371
20372/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020373 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020374 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020377find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020379 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020380 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020381 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020382{
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 hashitem_T *hi;
20384
20385 if (*varname == NUL)
20386 {
20387 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020388 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020390 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 case 'g': return &globvars_var;
20392 case 'v': return &vimvars_var;
20393 case 'b': return &curbuf->b_bufvar;
20394 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020395#ifdef FEAT_WINDOWS
20396 case 't': return &curtab->tp_winvar;
20397#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020398 case 'l': return current_funccal == NULL
20399 ? NULL : &current_funccal->l_vars_var;
20400 case 'a': return current_funccal == NULL
20401 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 }
20403 return NULL;
20404 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020405
20406 hi = hash_find(ht, varname);
20407 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020408 {
20409 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020410 * worked find the variable again. Don't auto-load a script if it was
20411 * loaded already, otherwise it would be loaded every time when
20412 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020413 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020414 {
20415 /* Note: script_autoload() may make "hi" invalid. It must either
20416 * be obtained again or not used. */
20417 if (!script_autoload(varname, FALSE) || aborting())
20418 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020419 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020420 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020421 if (HASHITEM_EMPTY(hi))
20422 return NULL;
20423 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020425}
20426
20427/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020429 * Set "varname" to the start of name without ':'.
20430 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020432find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 char_u *name;
20434 char_u **varname;
20435{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020436 hashitem_T *hi;
20437
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 if (name[1] != ':')
20439 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020440 /* The name must not start with a colon or #. */
20441 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 return NULL;
20443 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020444
20445 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020446 hi = hash_find(&compat_hashtab, name);
20447 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020448 return &compat_hashtab;
20449
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020451 return &globvarht; /* global variable */
20452 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 }
20454 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020455 if (*name == 'g') /* global variable */
20456 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020457 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20458 */
20459 if (vim_strchr(name + 2, ':') != NULL
20460 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020461 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020463 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020465 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020466#ifdef FEAT_WINDOWS
20467 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020468 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 if (*name == 'v') /* v: variable */
20471 return &vimvarht;
20472 if (*name == 'a' && current_funccal != NULL) /* function argument */
20473 return &current_funccal->l_avars.dv_hashtab;
20474 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20475 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 if (*name == 's' /* script variable */
20477 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20478 return &SCRIPT_VARS(current_SID);
20479 return NULL;
20480}
20481
20482/*
20483 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020484 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 * Returns NULL when it doesn't exist.
20486 */
20487 char_u *
20488get_var_value(name)
20489 char_u *name;
20490{
Bram Moolenaar33570922005-01-25 22:26:29 +000020491 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020493 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 if (v == NULL)
20495 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497}
20498
20499/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 * sourcing this script and when executing functions defined in the script.
20502 */
20503 void
20504new_script_vars(id)
20505 scid_T id;
20506{
Bram Moolenaara7043832005-01-21 11:56:39 +000020507 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020508 hashtab_T *ht;
20509 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020510
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20512 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020513 /* Re-allocating ga_data means that an ht_array pointing to
20514 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020516 for (i = 1; i <= ga_scripts.ga_len; ++i)
20517 {
20518 ht = &SCRIPT_VARS(i);
20519 if (ht->ht_mask == HT_INIT_SIZE - 1)
20520 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020521 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020522 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020523 }
20524
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 while (ga_scripts.ga_len < id)
20526 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020527 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020528 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020529 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 }
20532 }
20533}
20534
20535/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020536 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20537 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020538 */
20539 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020540init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 dict_T *dict;
20542 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020543 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544{
Bram Moolenaar33570922005-01-25 22:26:29 +000020545 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020546 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020547 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020548 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020549 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 dict_var->di_tv.vval.v_dict = dict;
20551 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020552 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20554 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555}
20556
20557/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020558 * Unreference a dictionary initialized by init_var_dict().
20559 */
20560 void
20561unref_var_dict(dict)
20562 dict_T *dict;
20563{
20564 /* Now the dict needs to be freed if no one else is using it, go back to
20565 * normal reference counting. */
20566 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20567 dict_unref(dict);
20568}
20569
20570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020572 * Frees all allocated variables and the value they contain.
20573 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574 */
20575 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020576vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020577 hashtab_T *ht;
20578{
20579 vars_clear_ext(ht, TRUE);
20580}
20581
20582/*
20583 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20584 */
20585 static void
20586vars_clear_ext(ht, free_val)
20587 hashtab_T *ht;
20588 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589{
Bram Moolenaara7043832005-01-21 11:56:39 +000020590 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 hashitem_T *hi;
20592 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593
Bram Moolenaar33570922005-01-25 22:26:29 +000020594 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020595 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020596 for (hi = ht->ht_array; todo > 0; ++hi)
20597 {
20598 if (!HASHITEM_EMPTY(hi))
20599 {
20600 --todo;
20601
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020603 * ht_array might change then. hash_clear() takes care of it
20604 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020605 v = HI2DI(hi);
20606 if (free_val)
20607 clear_tv(&v->di_tv);
20608 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20609 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020610 }
20611 }
20612 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020613 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614}
20615
Bram Moolenaara7043832005-01-21 11:56:39 +000020616/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 * Delete a variable from hashtab "ht" at item "hi".
20618 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020621delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020622 hashtab_T *ht;
20623 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624{
Bram Moolenaar33570922005-01-25 22:26:29 +000020625 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020626
20627 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020628 clear_tv(&di->di_tv);
20629 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630}
20631
20632/*
20633 * List the value of one internal variable.
20634 */
20635 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020636list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020637 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020639 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020641 char_u *tofree;
20642 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020643 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020644
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020645 current_copyID += COPYID_INC;
20646 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020647 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020648 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020649 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650}
20651
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020653list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 char_u *prefix;
20655 char_u *name;
20656 int type;
20657 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020658 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659{
Bram Moolenaar31859182007-08-14 20:41:13 +000020660 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20661 msg_start();
20662 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 if (name != NULL) /* "a:" vars don't have a name stored */
20664 msg_puts(name);
20665 msg_putchar(' ');
20666 msg_advance(22);
20667 if (type == VAR_NUMBER)
20668 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020669 else if (type == VAR_FUNC)
20670 msg_putchar('*');
20671 else if (type == VAR_LIST)
20672 {
20673 msg_putchar('[');
20674 if (*string == '[')
20675 ++string;
20676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020677 else if (type == VAR_DICT)
20678 {
20679 msg_putchar('{');
20680 if (*string == '{')
20681 ++string;
20682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 else
20684 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020687
20688 if (type == VAR_FUNC)
20689 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020690 if (*first)
20691 {
20692 msg_clr_eos();
20693 *first = FALSE;
20694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695}
20696
20697/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 * If the variable already exists, the value is updated.
20700 * Otherwise the variable is created.
20701 */
20702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020703set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020705 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020706 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707{
Bram Moolenaar33570922005-01-25 22:26:29 +000020708 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020710 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020712 ht = find_var_ht(name, &varname);
20713 if (ht == NULL || *varname == NUL)
20714 {
20715 EMSG2(_(e_illvar), name);
20716 return;
20717 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020718 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020719
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020720 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20721 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020722
Bram Moolenaar33570922005-01-25 22:26:29 +000020723 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020725 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020726 if (var_check_ro(v->di_flags, name)
20727 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020728 return;
20729 if (v->di_tv.v_type != tv->v_type
20730 && !((v->di_tv.v_type == VAR_STRING
20731 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020732 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020733 || tv->v_type == VAR_NUMBER))
20734#ifdef FEAT_FLOAT
20735 && !((v->di_tv.v_type == VAR_NUMBER
20736 || v->di_tv.v_type == VAR_FLOAT)
20737 && (tv->v_type == VAR_NUMBER
20738 || tv->v_type == VAR_FLOAT))
20739#endif
20740 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020741 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020742 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020743 return;
20744 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020745
20746 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020747 * Handle setting internal v: variables separately: we don't change
20748 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020749 */
20750 if (ht == &vimvarht)
20751 {
20752 if (v->di_tv.v_type == VAR_STRING)
20753 {
20754 vim_free(v->di_tv.vval.v_string);
20755 if (copy || tv->v_type != VAR_STRING)
20756 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20757 else
20758 {
20759 /* Take over the string to avoid an extra alloc/free. */
20760 v->di_tv.vval.v_string = tv->vval.v_string;
20761 tv->vval.v_string = NULL;
20762 }
20763 }
20764 else if (v->di_tv.v_type != VAR_NUMBER)
20765 EMSG2(_(e_intern2), "set_var()");
20766 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020767 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020768 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020769 if (STRCMP(varname, "searchforward") == 0)
20770 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020771#ifdef FEAT_SEARCH_EXTRA
20772 else if (STRCMP(varname, "hlsearch") == 0)
20773 {
20774 no_hlsearch = !v->di_tv.vval.v_number;
20775 redraw_all_later(SOME_VALID);
20776 }
20777#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020778 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020779 return;
20780 }
20781
20782 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 }
20784 else /* add a new variable */
20785 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020786 /* Can't add "v:" variable. */
20787 if (ht == &vimvarht)
20788 {
20789 EMSG2(_(e_illvar), name);
20790 return;
20791 }
20792
Bram Moolenaar92124a32005-06-17 22:03:40 +000020793 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020794 if (!valid_varname(varname))
20795 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020796
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020797 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20798 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020799 if (v == NULL)
20800 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020802 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020804 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 return;
20806 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020807 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020809
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020810 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020811 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020812 else
20813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020814 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020815 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818}
20819
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020820/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020821 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 * Also give an error message.
20823 */
20824 static int
20825var_check_ro(flags, name)
20826 int flags;
20827 char_u *name;
20828{
20829 if (flags & DI_FLAGS_RO)
20830 {
20831 EMSG2(_(e_readonlyvar), name);
20832 return TRUE;
20833 }
20834 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20835 {
20836 EMSG2(_(e_readonlysbx), name);
20837 return TRUE;
20838 }
20839 return FALSE;
20840}
20841
20842/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020843 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20844 * Also give an error message.
20845 */
20846 static int
20847var_check_fixed(flags, name)
20848 int flags;
20849 char_u *name;
20850{
20851 if (flags & DI_FLAGS_FIX)
20852 {
20853 EMSG2(_("E795: Cannot delete variable %s"), name);
20854 return TRUE;
20855 }
20856 return FALSE;
20857}
20858
20859/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020860 * Check if a funcref is assigned to a valid variable name.
20861 * Return TRUE and give an error if not.
20862 */
20863 static int
20864var_check_func_name(name, new_var)
20865 char_u *name; /* points to start of variable name */
20866 int new_var; /* TRUE when creating the variable */
20867{
20868 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20869 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20870 ? name[2] : name[0]))
20871 {
20872 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20873 name);
20874 return TRUE;
20875 }
20876 /* Don't allow hiding a function. When "v" is not NULL we might be
20877 * assigning another function to the same var, the type is checked
20878 * below. */
20879 if (new_var && function_exists(name))
20880 {
20881 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20882 name);
20883 return TRUE;
20884 }
20885 return FALSE;
20886}
20887
20888/*
20889 * Check if a variable name is valid.
20890 * Return FALSE and give an error if not.
20891 */
20892 static int
20893valid_varname(varname)
20894 char_u *varname;
20895{
20896 char_u *p;
20897
20898 for (p = varname; *p != NUL; ++p)
20899 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20900 && *p != AUTOLOAD_CHAR)
20901 {
20902 EMSG2(_(e_illvar), varname);
20903 return FALSE;
20904 }
20905 return TRUE;
20906}
20907
20908/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020909 * Return TRUE if typeval "tv" is set to be locked (immutable).
20910 * Also give an error message, using "name".
20911 */
20912 static int
20913tv_check_lock(lock, name)
20914 int lock;
20915 char_u *name;
20916{
20917 if (lock & VAR_LOCKED)
20918 {
20919 EMSG2(_("E741: Value is locked: %s"),
20920 name == NULL ? (char_u *)_("Unknown") : name);
20921 return TRUE;
20922 }
20923 if (lock & VAR_FIXED)
20924 {
20925 EMSG2(_("E742: Cannot change value of %s"),
20926 name == NULL ? (char_u *)_("Unknown") : name);
20927 return TRUE;
20928 }
20929 return FALSE;
20930}
20931
20932/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020933 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020934 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020935 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020936 * It is OK for "from" and "to" to point to the same item. This is used to
20937 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020938 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020939 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020940copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020941 typval_T *from;
20942 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020945 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020946 switch (from->v_type)
20947 {
20948 case VAR_NUMBER:
20949 to->vval.v_number = from->vval.v_number;
20950 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020951#ifdef FEAT_FLOAT
20952 case VAR_FLOAT:
20953 to->vval.v_float = from->vval.v_float;
20954 break;
20955#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020956 case VAR_STRING:
20957 case VAR_FUNC:
20958 if (from->vval.v_string == NULL)
20959 to->vval.v_string = NULL;
20960 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020962 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020963 if (from->v_type == VAR_FUNC)
20964 func_ref(to->vval.v_string);
20965 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020966 break;
20967 case VAR_LIST:
20968 if (from->vval.v_list == NULL)
20969 to->vval.v_list = NULL;
20970 else
20971 {
20972 to->vval.v_list = from->vval.v_list;
20973 ++to->vval.v_list->lv_refcount;
20974 }
20975 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020976 case VAR_DICT:
20977 if (from->vval.v_dict == NULL)
20978 to->vval.v_dict = NULL;
20979 else
20980 {
20981 to->vval.v_dict = from->vval.v_dict;
20982 ++to->vval.v_dict->dv_refcount;
20983 }
20984 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020985 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020987 break;
20988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989}
20990
20991/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020992 * Make a copy of an item.
20993 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020994 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20995 * reference to an already copied list/dict can be used.
20996 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020997 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020998 static int
20999item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021000 typval_T *from;
21001 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021002 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021003 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021004{
21005 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021006 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021007
Bram Moolenaar33570922005-01-25 22:26:29 +000021008 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021009 {
21010 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021011 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021012 }
21013 ++recurse;
21014
21015 switch (from->v_type)
21016 {
21017 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021018#ifdef FEAT_FLOAT
21019 case VAR_FLOAT:
21020#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021021 case VAR_STRING:
21022 case VAR_FUNC:
21023 copy_tv(from, to);
21024 break;
21025 case VAR_LIST:
21026 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021027 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021028 if (from->vval.v_list == NULL)
21029 to->vval.v_list = NULL;
21030 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21031 {
21032 /* use the copy made earlier */
21033 to->vval.v_list = from->vval.v_list->lv_copylist;
21034 ++to->vval.v_list->lv_refcount;
21035 }
21036 else
21037 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21038 if (to->vval.v_list == NULL)
21039 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021040 break;
21041 case VAR_DICT:
21042 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021043 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021044 if (from->vval.v_dict == NULL)
21045 to->vval.v_dict = NULL;
21046 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21047 {
21048 /* use the copy made earlier */
21049 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21050 ++to->vval.v_dict->dv_refcount;
21051 }
21052 else
21053 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21054 if (to->vval.v_dict == NULL)
21055 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021056 break;
21057 default:
21058 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021059 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021060 }
21061 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021062 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021063}
21064
21065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 * ":echo expr1 ..." print each argument separated with a space, add a
21067 * newline at the end.
21068 * ":echon expr1 ..." print each argument plain.
21069 */
21070 void
21071ex_echo(eap)
21072 exarg_T *eap;
21073{
21074 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021075 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021076 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 char_u *p;
21078 int needclr = TRUE;
21079 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021080 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081
21082 if (eap->skip)
21083 ++emsg_skip;
21084 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21085 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021086 /* If eval1() causes an error message the text from the command may
21087 * still need to be cleared. E.g., "echo 22,44". */
21088 need_clr_eos = needclr;
21089
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 {
21093 /*
21094 * Report the invalid expression unless the expression evaluation
21095 * has been cancelled due to an aborting error, an interrupt, or an
21096 * exception.
21097 */
21098 if (!aborting())
21099 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021100 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 break;
21102 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021103 need_clr_eos = FALSE;
21104
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 if (!eap->skip)
21106 {
21107 if (atstart)
21108 {
21109 atstart = FALSE;
21110 /* Call msg_start() after eval1(), evaluating the expression
21111 * may cause a message to appear. */
21112 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021113 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021114 /* Mark the saved text as finishing the line, so that what
21115 * follows is displayed on a new line when scrolling back
21116 * at the more prompt. */
21117 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 }
21121 else if (eap->cmdidx == CMD_echo)
21122 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021123 current_copyID += COPYID_INC;
21124 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021125 if (p != NULL)
21126 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021128 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021130 if (*p != TAB && needclr)
21131 {
21132 /* remove any text still there from the command */
21133 msg_clr_eos();
21134 needclr = FALSE;
21135 }
21136 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 }
21138 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021139 {
21140#ifdef FEAT_MBYTE
21141 if (has_mbyte)
21142 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021143 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021144
21145 (void)msg_outtrans_len_attr(p, i, echo_attr);
21146 p += i - 1;
21147 }
21148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021150 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021153 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 arg = skipwhite(arg);
21157 }
21158 eap->nextcmd = check_nextcmd(arg);
21159
21160 if (eap->skip)
21161 --emsg_skip;
21162 else
21163 {
21164 /* remove text that may still be there from the command */
21165 if (needclr)
21166 msg_clr_eos();
21167 if (eap->cmdidx == CMD_echo)
21168 msg_end();
21169 }
21170}
21171
21172/*
21173 * ":echohl {name}".
21174 */
21175 void
21176ex_echohl(eap)
21177 exarg_T *eap;
21178{
21179 int id;
21180
21181 id = syn_name2id(eap->arg);
21182 if (id == 0)
21183 echo_attr = 0;
21184 else
21185 echo_attr = syn_id2attr(id);
21186}
21187
21188/*
21189 * ":execute expr1 ..." execute the result of an expression.
21190 * ":echomsg expr1 ..." Print a message
21191 * ":echoerr expr1 ..." Print an error
21192 * Each gets spaces around each argument and a newline at the end for
21193 * echo commands
21194 */
21195 void
21196ex_execute(eap)
21197 exarg_T *eap;
21198{
21199 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021200 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 int ret = OK;
21202 char_u *p;
21203 garray_T ga;
21204 int len;
21205 int save_did_emsg;
21206
21207 ga_init2(&ga, 1, 80);
21208
21209 if (eap->skip)
21210 ++emsg_skip;
21211 while (*arg != NUL && *arg != '|' && *arg != '\n')
21212 {
21213 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 {
21216 /*
21217 * Report the invalid expression unless the expression evaluation
21218 * has been cancelled due to an aborting error, an interrupt, or an
21219 * exception.
21220 */
21221 if (!aborting())
21222 EMSG2(_(e_invexpr2), p);
21223 ret = FAIL;
21224 break;
21225 }
21226
21227 if (!eap->skip)
21228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021229 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 len = (int)STRLEN(p);
21231 if (ga_grow(&ga, len + 2) == FAIL)
21232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 ret = FAIL;
21235 break;
21236 }
21237 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 ga.ga_len += len;
21241 }
21242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 arg = skipwhite(arg);
21245 }
21246
21247 if (ret != FAIL && ga.ga_data != NULL)
21248 {
21249 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021252 out_flush();
21253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 else if (eap->cmdidx == CMD_echoerr)
21255 {
21256 /* We don't want to abort following commands, restore did_emsg. */
21257 save_did_emsg = did_emsg;
21258 EMSG((char_u *)ga.ga_data);
21259 if (!force_abort)
21260 did_emsg = save_did_emsg;
21261 }
21262 else if (eap->cmdidx == CMD_execute)
21263 do_cmdline((char_u *)ga.ga_data,
21264 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21265 }
21266
21267 ga_clear(&ga);
21268
21269 if (eap->skip)
21270 --emsg_skip;
21271
21272 eap->nextcmd = check_nextcmd(arg);
21273}
21274
21275/*
21276 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21277 * "arg" points to the "&" or '+' when called, to "option" when returning.
21278 * Returns NULL when no option name found. Otherwise pointer to the char
21279 * after the option name.
21280 */
21281 static char_u *
21282find_option_end(arg, opt_flags)
21283 char_u **arg;
21284 int *opt_flags;
21285{
21286 char_u *p = *arg;
21287
21288 ++p;
21289 if (*p == 'g' && p[1] == ':')
21290 {
21291 *opt_flags = OPT_GLOBAL;
21292 p += 2;
21293 }
21294 else if (*p == 'l' && p[1] == ':')
21295 {
21296 *opt_flags = OPT_LOCAL;
21297 p += 2;
21298 }
21299 else
21300 *opt_flags = 0;
21301
21302 if (!ASCII_ISALPHA(*p))
21303 return NULL;
21304 *arg = p;
21305
21306 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21307 p += 4; /* termcap option */
21308 else
21309 while (ASCII_ISALPHA(*p))
21310 ++p;
21311 return p;
21312}
21313
21314/*
21315 * ":function"
21316 */
21317 void
21318ex_function(eap)
21319 exarg_T *eap;
21320{
21321 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021322 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 int j;
21324 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021326 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 char_u *name = NULL;
21328 char_u *p;
21329 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021330 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 garray_T newargs;
21332 garray_T newlines;
21333 int varargs = FALSE;
21334 int mustend = FALSE;
21335 int flags = 0;
21336 ufunc_T *fp;
21337 int indent;
21338 int nesting;
21339 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021340 dictitem_T *v;
21341 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021342 static int func_nr = 0; /* number for nameless function */
21343 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021344 hashtab_T *ht;
21345 int todo;
21346 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021347 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348
21349 /*
21350 * ":function" without argument: list functions.
21351 */
21352 if (ends_excmd(*eap->arg))
21353 {
21354 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021356 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021357 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021358 {
21359 if (!HASHITEM_EMPTY(hi))
21360 {
21361 --todo;
21362 fp = HI2UF(hi);
21363 if (!isdigit(*fp->uf_name))
21364 list_func_head(fp, FALSE);
21365 }
21366 }
21367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 eap->nextcmd = check_nextcmd(eap->arg);
21369 return;
21370 }
21371
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021372 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021373 * ":function /pat": list functions matching pattern.
21374 */
21375 if (*eap->arg == '/')
21376 {
21377 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21378 if (!eap->skip)
21379 {
21380 regmatch_T regmatch;
21381
21382 c = *p;
21383 *p = NUL;
21384 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21385 *p = c;
21386 if (regmatch.regprog != NULL)
21387 {
21388 regmatch.rm_ic = p_ic;
21389
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021390 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021391 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21392 {
21393 if (!HASHITEM_EMPTY(hi))
21394 {
21395 --todo;
21396 fp = HI2UF(hi);
21397 if (!isdigit(*fp->uf_name)
21398 && vim_regexec(&regmatch, fp->uf_name, 0))
21399 list_func_head(fp, FALSE);
21400 }
21401 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021402 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021403 }
21404 }
21405 if (*p == '/')
21406 ++p;
21407 eap->nextcmd = check_nextcmd(p);
21408 return;
21409 }
21410
21411 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021412 * Get the function name. There are these situations:
21413 * func normal function name
21414 * "name" == func, "fudi.fd_dict" == NULL
21415 * dict.func new dictionary entry
21416 * "name" == NULL, "fudi.fd_dict" set,
21417 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21418 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021419 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021420 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21421 * dict.func existing dict entry that's not a Funcref
21422 * "name" == NULL, "fudi.fd_dict" set,
21423 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021426 name = trans_function_name(&p, eap->skip, 0, &fudi);
21427 paren = (vim_strchr(p, '(') != NULL);
21428 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 {
21430 /*
21431 * Return on an invalid expression in braces, unless the expression
21432 * evaluation has been cancelled due to an aborting error, an
21433 * interrupt, or an exception.
21434 */
21435 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021437 if (!eap->skip && fudi.fd_newkey != NULL)
21438 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021439 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 else
21443 eap->skip = TRUE;
21444 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021445
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 /* An error in a function call during evaluation of an expression in magic
21447 * braces should not cause the function not to be defined. */
21448 saved_did_emsg = did_emsg;
21449 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450
21451 /*
21452 * ":function func" with only function name: list function.
21453 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021454 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 {
21456 if (!ends_excmd(*skipwhite(p)))
21457 {
21458 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021459 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 }
21461 eap->nextcmd = check_nextcmd(p);
21462 if (eap->nextcmd != NULL)
21463 *p = NUL;
21464 if (!eap->skip && !got_int)
21465 {
21466 fp = find_func(name);
21467 if (fp != NULL)
21468 {
21469 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021470 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021472 if (FUNCLINE(fp, j) == NULL)
21473 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 msg_putchar('\n');
21475 msg_outnum((long)(j + 1));
21476 if (j < 9)
21477 msg_putchar(' ');
21478 if (j < 99)
21479 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021480 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 out_flush(); /* show a line at a time */
21482 ui_breakcheck();
21483 }
21484 if (!got_int)
21485 {
21486 msg_putchar('\n');
21487 msg_puts((char_u *)" endfunction");
21488 }
21489 }
21490 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021491 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021493 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 }
21495
21496 /*
21497 * ":function name(arg1, arg2)" Define function.
21498 */
21499 p = skipwhite(p);
21500 if (*p != '(')
21501 {
21502 if (!eap->skip)
21503 {
21504 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 }
21507 /* attempt to continue by skipping some text */
21508 if (vim_strchr(p, '(') != NULL)
21509 p = vim_strchr(p, '(');
21510 }
21511 p = skipwhite(p + 1);
21512
21513 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21514 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21515
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021516 if (!eap->skip)
21517 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021518 /* Check the name of the function. Unless it's a dictionary function
21519 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021520 if (name != NULL)
21521 arg = name;
21522 else
21523 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021524 if (arg != NULL && (fudi.fd_di == NULL
21525 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021526 {
21527 if (*arg == K_SPECIAL)
21528 j = 3;
21529 else
21530 j = 0;
21531 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21532 : eval_isnamec(arg[j])))
21533 ++j;
21534 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021535 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021536 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021537 /* Disallow using the g: dict. */
21538 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21539 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021540 }
21541
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 /*
21543 * Isolate the arguments: "arg1, arg2, ...)"
21544 */
21545 while (*p != ')')
21546 {
21547 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21548 {
21549 varargs = TRUE;
21550 p += 3;
21551 mustend = TRUE;
21552 }
21553 else
21554 {
21555 arg = p;
21556 while (ASCII_ISALNUM(*p) || *p == '_')
21557 ++p;
21558 if (arg == p || isdigit(*arg)
21559 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21560 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21561 {
21562 if (!eap->skip)
21563 EMSG2(_("E125: Illegal argument: %s"), arg);
21564 break;
21565 }
21566 if (ga_grow(&newargs, 1) == FAIL)
21567 goto erret;
21568 c = *p;
21569 *p = NUL;
21570 arg = vim_strsave(arg);
21571 if (arg == NULL)
21572 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021573
21574 /* Check for duplicate argument name. */
21575 for (i = 0; i < newargs.ga_len; ++i)
21576 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21577 {
21578 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021579 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021580 goto erret;
21581 }
21582
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21584 *p = c;
21585 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 if (*p == ',')
21587 ++p;
21588 else
21589 mustend = TRUE;
21590 }
21591 p = skipwhite(p);
21592 if (mustend && *p != ')')
21593 {
21594 if (!eap->skip)
21595 EMSG2(_(e_invarg2), eap->arg);
21596 break;
21597 }
21598 }
21599 ++p; /* skip the ')' */
21600
Bram Moolenaare9a41262005-01-15 22:18:47 +000021601 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 for (;;)
21603 {
21604 p = skipwhite(p);
21605 if (STRNCMP(p, "range", 5) == 0)
21606 {
21607 flags |= FC_RANGE;
21608 p += 5;
21609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021610 else if (STRNCMP(p, "dict", 4) == 0)
21611 {
21612 flags |= FC_DICT;
21613 p += 4;
21614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 else if (STRNCMP(p, "abort", 5) == 0)
21616 {
21617 flags |= FC_ABORT;
21618 p += 5;
21619 }
21620 else
21621 break;
21622 }
21623
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021624 /* When there is a line break use what follows for the function body.
21625 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21626 if (*p == '\n')
21627 line_arg = p + 1;
21628 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 EMSG(_(e_trailing));
21630
21631 /*
21632 * Read the body of the function, until ":endfunction" is found.
21633 */
21634 if (KeyTyped)
21635 {
21636 /* Check if the function already exists, don't let the user type the
21637 * whole function before telling him it doesn't work! For a script we
21638 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 if (!eap->skip && !eap->forceit)
21640 {
21641 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21642 EMSG(_(e_funcdict));
21643 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021644 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021647 if (!eap->skip && did_emsg)
21648 goto erret;
21649
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 msg_putchar('\n'); /* don't overwrite the function name */
21651 cmdline_row = msg_row;
21652 }
21653
21654 indent = 2;
21655 nesting = 0;
21656 for (;;)
21657 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021658 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021659 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021660 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021661 saved_wait_return = FALSE;
21662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021664 sourcing_lnum_off = sourcing_lnum;
21665
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021666 if (line_arg != NULL)
21667 {
21668 /* Use eap->arg, split up in parts by line breaks. */
21669 theline = line_arg;
21670 p = vim_strchr(theline, '\n');
21671 if (p == NULL)
21672 line_arg += STRLEN(line_arg);
21673 else
21674 {
21675 *p = NUL;
21676 line_arg = p + 1;
21677 }
21678 }
21679 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 theline = getcmdline(':', 0L, indent);
21681 else
21682 theline = eap->getline(':', eap->cookie, indent);
21683 if (KeyTyped)
21684 lines_left = Rows - 1;
21685 if (theline == NULL)
21686 {
21687 EMSG(_("E126: Missing :endfunction"));
21688 goto erret;
21689 }
21690
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021691 /* Detect line continuation: sourcing_lnum increased more than one. */
21692 if (sourcing_lnum > sourcing_lnum_off + 1)
21693 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21694 else
21695 sourcing_lnum_off = 0;
21696
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 if (skip_until != NULL)
21698 {
21699 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21700 * don't check for ":endfunc". */
21701 if (STRCMP(theline, skip_until) == 0)
21702 {
21703 vim_free(skip_until);
21704 skip_until = NULL;
21705 }
21706 }
21707 else
21708 {
21709 /* skip ':' and blanks*/
21710 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21711 ;
21712
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021713 /* Check for "endfunction". */
21714 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021716 if (line_arg == NULL)
21717 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 break;
21719 }
21720
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021721 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 * at "end". */
21723 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21724 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021725 else if (STRNCMP(p, "if", 2) == 0
21726 || STRNCMP(p, "wh", 2) == 0
21727 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 || STRNCMP(p, "try", 3) == 0)
21729 indent += 2;
21730
21731 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021732 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021734 if (*p == '!')
21735 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 p += eval_fname_script(p);
21737 if (ASCII_ISALPHA(*p))
21738 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 if (*skipwhite(p) == '(')
21741 {
21742 ++nesting;
21743 indent += 2;
21744 }
21745 }
21746 }
21747
21748 /* Check for ":append" or ":insert". */
21749 p = skip_range(p, NULL);
21750 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21751 || (p[0] == 'i'
21752 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21753 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21754 skip_until = vim_strsave((char_u *)".");
21755
21756 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21757 arg = skipwhite(skiptowhite(p));
21758 if (arg[0] == '<' && arg[1] =='<'
21759 && ((p[0] == 'p' && p[1] == 'y'
21760 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21761 || (p[0] == 'p' && p[1] == 'e'
21762 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21763 || (p[0] == 't' && p[1] == 'c'
21764 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021765 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21766 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21768 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021769 || (p[0] == 'm' && p[1] == 'z'
21770 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 ))
21772 {
21773 /* ":python <<" continues until a dot, like ":append" */
21774 p = skipwhite(arg + 2);
21775 if (*p == NUL)
21776 skip_until = vim_strsave((char_u *)".");
21777 else
21778 skip_until = vim_strsave(p);
21779 }
21780 }
21781
21782 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021783 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021784 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021785 if (line_arg == NULL)
21786 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021788 }
21789
21790 /* Copy the line to newly allocated memory. get_one_sourceline()
21791 * allocates 250 bytes per line, this saves 80% on average. The cost
21792 * is an extra alloc/free. */
21793 p = vim_strsave(theline);
21794 if (p != NULL)
21795 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021796 if (line_arg == NULL)
21797 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021798 theline = p;
21799 }
21800
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021801 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21802
21803 /* Add NULL lines for continuation lines, so that the line count is
21804 * equal to the index in the growarray. */
21805 while (sourcing_lnum_off-- > 0)
21806 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021807
21808 /* Check for end of eap->arg. */
21809 if (line_arg != NULL && *line_arg == NUL)
21810 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 }
21812
21813 /* Don't define the function when skipping commands or when an error was
21814 * detected. */
21815 if (eap->skip || did_emsg)
21816 goto erret;
21817
21818 /*
21819 * If there are no errors, add the function
21820 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021821 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021822 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021823 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021826 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021827 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021828 goto erret;
21829 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021830
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021831 fp = find_func(name);
21832 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021834 if (!eap->forceit)
21835 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021836 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 goto erret;
21838 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021839 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021840 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021841 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021842 name);
21843 goto erret;
21844 }
21845 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 ga_clear_strings(&(fp->uf_args));
21847 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 vim_free(name);
21849 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 }
21852 else
21853 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021854 char numbuf[20];
21855
21856 fp = NULL;
21857 if (fudi.fd_newkey == NULL && !eap->forceit)
21858 {
21859 EMSG(_(e_funcdict));
21860 goto erret;
21861 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021862 if (fudi.fd_di == NULL)
21863 {
21864 /* Can't add a function to a locked dictionary */
21865 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21866 goto erret;
21867 }
21868 /* Can't change an existing function if it is locked */
21869 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21870 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021871
21872 /* Give the function a sequential number. Can only be used with a
21873 * Funcref! */
21874 vim_free(name);
21875 sprintf(numbuf, "%d", ++func_nr);
21876 name = vim_strsave((char_u *)numbuf);
21877 if (name == NULL)
21878 goto erret;
21879 }
21880
21881 if (fp == NULL)
21882 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021883 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021884 {
21885 int slen, plen;
21886 char_u *scriptname;
21887
21888 /* Check that the autoload name matches the script name. */
21889 j = FAIL;
21890 if (sourcing_name != NULL)
21891 {
21892 scriptname = autoload_name(name);
21893 if (scriptname != NULL)
21894 {
21895 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021896 plen = (int)STRLEN(p);
21897 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021898 if (slen > plen && fnamecmp(p,
21899 sourcing_name + slen - plen) == 0)
21900 j = OK;
21901 vim_free(scriptname);
21902 }
21903 }
21904 if (j == FAIL)
21905 {
21906 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21907 goto erret;
21908 }
21909 }
21910
21911 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 if (fp == NULL)
21913 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021914
21915 if (fudi.fd_dict != NULL)
21916 {
21917 if (fudi.fd_di == NULL)
21918 {
21919 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021920 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 if (fudi.fd_di == NULL)
21922 {
21923 vim_free(fp);
21924 goto erret;
21925 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021926 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21927 {
21928 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021929 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021930 goto erret;
21931 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021932 }
21933 else
21934 /* overwrite existing dict entry */
21935 clear_tv(&fudi.fd_di->di_tv);
21936 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021937 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021938 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021939 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021940
21941 /* behave like "dict" was used */
21942 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021943 }
21944
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021946 STRCPY(fp->uf_name, name);
21947 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021949 fp->uf_args = newargs;
21950 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021951#ifdef FEAT_PROFILE
21952 fp->uf_tml_count = NULL;
21953 fp->uf_tml_total = NULL;
21954 fp->uf_tml_self = NULL;
21955 fp->uf_profiling = FALSE;
21956 if (prof_def_func())
21957 func_do_profile(fp);
21958#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021959 fp->uf_varargs = varargs;
21960 fp->uf_flags = flags;
21961 fp->uf_calls = 0;
21962 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021963 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964
21965erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 ga_clear_strings(&newargs);
21967 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021968ret_free:
21969 vim_free(skip_until);
21970 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021973 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974}
21975
21976/*
21977 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021978 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021980 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021981 * TFN_INT: internal function name OK
21982 * TFN_QUIET: be quiet
21983 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * Advances "pp" to just after the function name (if no error).
21985 */
21986 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021987trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 char_u **pp;
21989 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021990 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021991 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021993 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 char_u *start;
21995 char_u *end;
21996 int lead;
21997 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022000
22001 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022002 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022004
22005 /* Check for hard coded <SNR>: already translated function ID (from a user
22006 * command). */
22007 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22008 && (*pp)[2] == (int)KE_SNR)
22009 {
22010 *pp += 3;
22011 len = get_id_len(pp) + 3;
22012 return vim_strnsave(start, len);
22013 }
22014
22015 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22016 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022018 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022020
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022021 /* Note that TFN_ flags use the same values as GLV_ flags. */
22022 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022023 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022024 if (end == start)
22025 {
22026 if (!skip)
22027 EMSG(_("E129: Function name required"));
22028 goto theend;
22029 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022030 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022031 {
22032 /*
22033 * Report an invalid expression in braces, unless the expression
22034 * evaluation has been cancelled due to an aborting error, an
22035 * interrupt, or an exception.
22036 */
22037 if (!aborting())
22038 {
22039 if (end != NULL)
22040 EMSG2(_(e_invarg2), start);
22041 }
22042 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022043 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022044 goto theend;
22045 }
22046
22047 if (lv.ll_tv != NULL)
22048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022049 if (fdp != NULL)
22050 {
22051 fdp->fd_dict = lv.ll_dict;
22052 fdp->fd_newkey = lv.ll_newkey;
22053 lv.ll_newkey = NULL;
22054 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022056 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22057 {
22058 name = vim_strsave(lv.ll_tv->vval.v_string);
22059 *pp = end;
22060 }
22061 else
22062 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022063 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22064 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 EMSG(_(e_funcref));
22066 else
22067 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022068 name = NULL;
22069 }
22070 goto theend;
22071 }
22072
22073 if (lv.ll_name == NULL)
22074 {
22075 /* Error found, but continue after the function name. */
22076 *pp = end;
22077 goto theend;
22078 }
22079
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022080 /* Check if the name is a Funcref. If so, use the value. */
22081 if (lv.ll_exp_name != NULL)
22082 {
22083 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022084 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022085 if (name == lv.ll_exp_name)
22086 name = NULL;
22087 }
22088 else
22089 {
22090 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022091 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022092 if (name == *pp)
22093 name = NULL;
22094 }
22095 if (name != NULL)
22096 {
22097 name = vim_strsave(name);
22098 *pp = end;
22099 goto theend;
22100 }
22101
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022102 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022103 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022104 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022105 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22106 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22107 {
22108 /* When there was "s:" already or the name expanded to get a
22109 * leading "s:" then remove it. */
22110 lv.ll_name += 2;
22111 len -= 2;
22112 lead = 2;
22113 }
22114 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022115 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022116 {
22117 if (lead == 2) /* skip over "s:" */
22118 lv.ll_name += 2;
22119 len = (int)(end - lv.ll_name);
22120 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022121
22122 /*
22123 * Copy the function name to allocated memory.
22124 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22125 * Accept <SNR>123_name() outside a script.
22126 */
22127 if (skip)
22128 lead = 0; /* do nothing */
22129 else if (lead > 0)
22130 {
22131 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022132 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22133 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022134 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022135 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022136 if (current_SID <= 0)
22137 {
22138 EMSG(_(e_usingsid));
22139 goto theend;
22140 }
22141 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22142 lead += (int)STRLEN(sid_buf);
22143 }
22144 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022145 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022146 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022147 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022148 goto theend;
22149 }
22150 name = alloc((unsigned)(len + lead + 1));
22151 if (name != NULL)
22152 {
22153 if (lead > 0)
22154 {
22155 name[0] = K_SPECIAL;
22156 name[1] = KS_EXTRA;
22157 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022158 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022159 STRCPY(name + 3, sid_buf);
22160 }
22161 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22162 name[len + lead] = NUL;
22163 }
22164 *pp = end;
22165
22166theend:
22167 clear_lval(&lv);
22168 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169}
22170
22171/*
22172 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22173 * Return 2 if "p" starts with "s:".
22174 * Return 0 otherwise.
22175 */
22176 static int
22177eval_fname_script(p)
22178 char_u *p;
22179{
22180 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22181 || STRNICMP(p + 1, "SNR>", 4) == 0))
22182 return 5;
22183 if (p[0] == 's' && p[1] == ':')
22184 return 2;
22185 return 0;
22186}
22187
22188/*
22189 * Return TRUE if "p" starts with "<SID>" or "s:".
22190 * Only works if eval_fname_script() returned non-zero for "p"!
22191 */
22192 static int
22193eval_fname_sid(p)
22194 char_u *p;
22195{
22196 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22197}
22198
22199/*
22200 * List the head of the function: "name(arg1, arg2)".
22201 */
22202 static void
22203list_func_head(fp, indent)
22204 ufunc_T *fp;
22205 int indent;
22206{
22207 int j;
22208
22209 msg_start();
22210 if (indent)
22211 MSG_PUTS(" ");
22212 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022213 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 {
22215 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022216 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 }
22218 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022219 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022221 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 {
22223 if (j)
22224 MSG_PUTS(", ");
22225 msg_puts(FUNCARG(fp, j));
22226 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022227 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 {
22229 if (j)
22230 MSG_PUTS(", ");
22231 MSG_PUTS("...");
22232 }
22233 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022234 if (fp->uf_flags & FC_ABORT)
22235 MSG_PUTS(" abort");
22236 if (fp->uf_flags & FC_RANGE)
22237 MSG_PUTS(" range");
22238 if (fp->uf_flags & FC_DICT)
22239 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022240 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022241 if (p_verbose > 0)
22242 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243}
22244
22245/*
22246 * Find a function by name, return pointer to it in ufuncs.
22247 * Return NULL for unknown function.
22248 */
22249 static ufunc_T *
22250find_func(name)
22251 char_u *name;
22252{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022253 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022255 hi = hash_find(&func_hashtab, name);
22256 if (!HASHITEM_EMPTY(hi))
22257 return HI2UF(hi);
22258 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259}
22260
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022261#if defined(EXITFREE) || defined(PROTO)
22262 void
22263free_all_functions()
22264{
22265 hashitem_T *hi;
22266
22267 /* Need to start all over every time, because func_free() may change the
22268 * hash table. */
22269 while (func_hashtab.ht_used > 0)
22270 for (hi = func_hashtab.ht_array; ; ++hi)
22271 if (!HASHITEM_EMPTY(hi))
22272 {
22273 func_free(HI2UF(hi));
22274 break;
22275 }
22276}
22277#endif
22278
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022279 int
22280translated_function_exists(name)
22281 char_u *name;
22282{
22283 if (builtin_function(name))
22284 return find_internal_func(name) >= 0;
22285 return find_func(name) != NULL;
22286}
22287
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288/*
22289 * Return TRUE if a function "name" exists.
22290 */
22291 static int
22292function_exists(name)
22293 char_u *name;
22294{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022295 char_u *nm = name;
22296 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022297 int n = FALSE;
22298
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022299 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22300 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022301 nm = skipwhite(nm);
22302
22303 /* Only accept "funcname", "funcname ", "funcname (..." and
22304 * "funcname(...", not "funcname!...". */
22305 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022306 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022307 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022308 return n;
22309}
22310
Bram Moolenaara1544c02013-05-30 12:35:52 +020022311 char_u *
22312get_expanded_name(name, check)
22313 char_u *name;
22314 int check;
22315{
22316 char_u *nm = name;
22317 char_u *p;
22318
22319 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22320
22321 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022322 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022323 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022324
Bram Moolenaara1544c02013-05-30 12:35:52 +020022325 vim_free(p);
22326 return NULL;
22327}
22328
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022329/*
22330 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022331 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022332 */
22333 static int
22334builtin_function(name)
22335 char_u *name;
22336{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022337 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22338 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022339}
22340
Bram Moolenaar05159a02005-02-26 23:04:13 +000022341#if defined(FEAT_PROFILE) || defined(PROTO)
22342/*
22343 * Start profiling function "fp".
22344 */
22345 static void
22346func_do_profile(fp)
22347 ufunc_T *fp;
22348{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022349 int len = fp->uf_lines.ga_len;
22350
22351 if (len == 0)
22352 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022353 fp->uf_tm_count = 0;
22354 profile_zero(&fp->uf_tm_self);
22355 profile_zero(&fp->uf_tm_total);
22356 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022357 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022358 if (fp->uf_tml_total == NULL)
22359 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022360 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022361 if (fp->uf_tml_self == NULL)
22362 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022363 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022364 fp->uf_tml_idx = -1;
22365 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22366 || fp->uf_tml_self == NULL)
22367 return; /* out of memory */
22368
22369 fp->uf_profiling = TRUE;
22370}
22371
22372/*
22373 * Dump the profiling results for all functions in file "fd".
22374 */
22375 void
22376func_dump_profile(fd)
22377 FILE *fd;
22378{
22379 hashitem_T *hi;
22380 int todo;
22381 ufunc_T *fp;
22382 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022383 ufunc_T **sorttab;
22384 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022386 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022387 if (todo == 0)
22388 return; /* nothing to dump */
22389
Bram Moolenaar73830342005-02-28 22:48:19 +000022390 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22391
Bram Moolenaar05159a02005-02-26 23:04:13 +000022392 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22393 {
22394 if (!HASHITEM_EMPTY(hi))
22395 {
22396 --todo;
22397 fp = HI2UF(hi);
22398 if (fp->uf_profiling)
22399 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022400 if (sorttab != NULL)
22401 sorttab[st_len++] = fp;
22402
Bram Moolenaar05159a02005-02-26 23:04:13 +000022403 if (fp->uf_name[0] == K_SPECIAL)
22404 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22405 else
22406 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22407 if (fp->uf_tm_count == 1)
22408 fprintf(fd, "Called 1 time\n");
22409 else
22410 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22411 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22412 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22413 fprintf(fd, "\n");
22414 fprintf(fd, "count total (s) self (s)\n");
22415
22416 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22417 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022418 if (FUNCLINE(fp, i) == NULL)
22419 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022420 prof_func_line(fd, fp->uf_tml_count[i],
22421 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022422 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22423 }
22424 fprintf(fd, "\n");
22425 }
22426 }
22427 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022428
22429 if (sorttab != NULL && st_len > 0)
22430 {
22431 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22432 prof_total_cmp);
22433 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22434 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22435 prof_self_cmp);
22436 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22437 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022438
22439 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022440}
Bram Moolenaar73830342005-02-28 22:48:19 +000022441
22442 static void
22443prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22444 FILE *fd;
22445 ufunc_T **sorttab;
22446 int st_len;
22447 char *title;
22448 int prefer_self; /* when equal print only self time */
22449{
22450 int i;
22451 ufunc_T *fp;
22452
22453 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22454 fprintf(fd, "count total (s) self (s) function\n");
22455 for (i = 0; i < 20 && i < st_len; ++i)
22456 {
22457 fp = sorttab[i];
22458 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22459 prefer_self);
22460 if (fp->uf_name[0] == K_SPECIAL)
22461 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22462 else
22463 fprintf(fd, " %s()\n", fp->uf_name);
22464 }
22465 fprintf(fd, "\n");
22466}
22467
22468/*
22469 * Print the count and times for one function or function line.
22470 */
22471 static void
22472prof_func_line(fd, count, total, self, prefer_self)
22473 FILE *fd;
22474 int count;
22475 proftime_T *total;
22476 proftime_T *self;
22477 int prefer_self; /* when equal print only self time */
22478{
22479 if (count > 0)
22480 {
22481 fprintf(fd, "%5d ", count);
22482 if (prefer_self && profile_equal(total, self))
22483 fprintf(fd, " ");
22484 else
22485 fprintf(fd, "%s ", profile_msg(total));
22486 if (!prefer_self && profile_equal(total, self))
22487 fprintf(fd, " ");
22488 else
22489 fprintf(fd, "%s ", profile_msg(self));
22490 }
22491 else
22492 fprintf(fd, " ");
22493}
22494
22495/*
22496 * Compare function for total time sorting.
22497 */
22498 static int
22499#ifdef __BORLANDC__
22500_RTLENTRYF
22501#endif
22502prof_total_cmp(s1, s2)
22503 const void *s1;
22504 const void *s2;
22505{
22506 ufunc_T *p1, *p2;
22507
22508 p1 = *(ufunc_T **)s1;
22509 p2 = *(ufunc_T **)s2;
22510 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22511}
22512
22513/*
22514 * Compare function for self time sorting.
22515 */
22516 static int
22517#ifdef __BORLANDC__
22518_RTLENTRYF
22519#endif
22520prof_self_cmp(s1, s2)
22521 const void *s1;
22522 const void *s2;
22523{
22524 ufunc_T *p1, *p2;
22525
22526 p1 = *(ufunc_T **)s1;
22527 p2 = *(ufunc_T **)s2;
22528 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22529}
22530
Bram Moolenaar05159a02005-02-26 23:04:13 +000022531#endif
22532
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022533/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022534 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022535 * Return TRUE if a package was loaded.
22536 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022537 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022538script_autoload(name, reload)
22539 char_u *name;
22540 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022541{
22542 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022543 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022544 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022545 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022546
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022547 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022548 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022549 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022550 return FALSE;
22551
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022552 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022553
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022554 /* Find the name in the list of previously loaded package names. Skip
22555 * "autoload/", it's always the same. */
22556 for (i = 0; i < ga_loaded.ga_len; ++i)
22557 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22558 break;
22559 if (!reload && i < ga_loaded.ga_len)
22560 ret = FALSE; /* was loaded already */
22561 else
22562 {
22563 /* Remember the name if it wasn't loaded already. */
22564 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22565 {
22566 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22567 tofree = NULL;
22568 }
22569
22570 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022571 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022572 ret = TRUE;
22573 }
22574
22575 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022576 return ret;
22577}
22578
22579/*
22580 * Return the autoload script name for a function or variable name.
22581 * Returns NULL when out of memory.
22582 */
22583 static char_u *
22584autoload_name(name)
22585 char_u *name;
22586{
22587 char_u *p;
22588 char_u *scriptname;
22589
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022590 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022591 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22592 if (scriptname == NULL)
22593 return FALSE;
22594 STRCPY(scriptname, "autoload/");
22595 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022596 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022597 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022598 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022599 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022601}
22602
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22604
22605/*
22606 * Function given to ExpandGeneric() to obtain the list of user defined
22607 * function names.
22608 */
22609 char_u *
22610get_user_func_name(xp, idx)
22611 expand_T *xp;
22612 int idx;
22613{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022614 static long_u done;
22615 static hashitem_T *hi;
22616 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617
22618 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022620 done = 0;
22621 hi = func_hashtab.ht_array;
22622 }
22623 if (done < func_hashtab.ht_used)
22624 {
22625 if (done++ > 0)
22626 ++hi;
22627 while (HASHITEM_EMPTY(hi))
22628 ++hi;
22629 fp = HI2UF(hi);
22630
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022631 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022632 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022633
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022634 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22635 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636
22637 cat_func_name(IObuff, fp);
22638 if (xp->xp_context != EXPAND_USER_FUNC)
22639 {
22640 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022641 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 STRCAT(IObuff, ")");
22643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 return IObuff;
22645 }
22646 return NULL;
22647}
22648
22649#endif /* FEAT_CMDL_COMPL */
22650
22651/*
22652 * Copy the function name of "fp" to buffer "buf".
22653 * "buf" must be able to hold the function name plus three bytes.
22654 * Takes care of script-local function names.
22655 */
22656 static void
22657cat_func_name(buf, fp)
22658 char_u *buf;
22659 ufunc_T *fp;
22660{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022661 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 {
22663 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022664 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022667 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668}
22669
22670/*
22671 * ":delfunction {name}"
22672 */
22673 void
22674ex_delfunction(eap)
22675 exarg_T *eap;
22676{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022677 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 char_u *p;
22679 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681
22682 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022683 name = trans_function_name(&p, eap->skip, 0, &fudi);
22684 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022686 {
22687 if (fudi.fd_dict != NULL && !eap->skip)
22688 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 if (!ends_excmd(*skipwhite(p)))
22692 {
22693 vim_free(name);
22694 EMSG(_(e_trailing));
22695 return;
22696 }
22697 eap->nextcmd = check_nextcmd(p);
22698 if (eap->nextcmd != NULL)
22699 *p = NUL;
22700
22701 if (!eap->skip)
22702 fp = find_func(name);
22703 vim_free(name);
22704
22705 if (!eap->skip)
22706 {
22707 if (fp == NULL)
22708 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022709 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 return;
22711 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022712 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 {
22714 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22715 return;
22716 }
22717
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022718 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022720 /* Delete the dict item that refers to the function, it will
22721 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022722 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022724 else
22725 func_free(fp);
22726 }
22727}
22728
22729/*
22730 * Free a function and remove it from the list of functions.
22731 */
22732 static void
22733func_free(fp)
22734 ufunc_T *fp;
22735{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022736 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022737
22738 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022739 ga_clear_strings(&(fp->uf_args));
22740 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022741#ifdef FEAT_PROFILE
22742 vim_free(fp->uf_tml_count);
22743 vim_free(fp->uf_tml_total);
22744 vim_free(fp->uf_tml_self);
22745#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022746
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022747 /* remove the function from the function hashtable */
22748 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22749 if (HASHITEM_EMPTY(hi))
22750 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022751 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022752 hash_remove(&func_hashtab, hi);
22753
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 vim_free(fp);
22755}
22756
22757/*
22758 * Unreference a Function: decrement the reference count and free it when it
22759 * becomes zero. Only for numbered functions.
22760 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022761 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762func_unref(name)
22763 char_u *name;
22764{
22765 ufunc_T *fp;
22766
22767 if (name != NULL && isdigit(*name))
22768 {
22769 fp = find_func(name);
22770 if (fp == NULL)
22771 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022772 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 {
22774 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022775 * when "uf_calls" becomes zero. */
22776 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022777 func_free(fp);
22778 }
22779 }
22780}
22781
22782/*
22783 * Count a reference to a Function.
22784 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022785 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786func_ref(name)
22787 char_u *name;
22788{
22789 ufunc_T *fp;
22790
22791 if (name != NULL && isdigit(*name))
22792 {
22793 fp = find_func(name);
22794 if (fp == NULL)
22795 EMSG2(_(e_intern2), "func_ref()");
22796 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022797 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 }
22799}
22800
22801/*
22802 * Call a user function.
22803 */
22804 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022805call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 ufunc_T *fp; /* pointer to function */
22807 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022808 typval_T *argvars; /* arguments */
22809 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 linenr_T firstline; /* first line of range */
22811 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813{
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 char_u *save_sourcing_name;
22815 linenr_T save_sourcing_lnum;
22816 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022817 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022818 int save_did_emsg;
22819 static int depth = 0;
22820 dictitem_T *v;
22821 int fixvar_idx = 0; /* index in fixvar[] */
22822 int i;
22823 int ai;
22824 char_u numbuf[NUMBUFLEN];
22825 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022826#ifdef FEAT_PROFILE
22827 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022828 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830
22831 /* If depth of calling is getting too high, don't execute the function */
22832 if (depth >= p_mfd)
22833 {
22834 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022835 rettv->v_type = VAR_NUMBER;
22836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 return;
22838 }
22839 ++depth;
22840
22841 line_breakcheck(); /* check for CTRL-C hit */
22842
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022843 fc = (funccall_T *)alloc(sizeof(funccall_T));
22844 fc->caller = current_funccal;
22845 current_funccal = fc;
22846 fc->func = fp;
22847 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022848 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022849 fc->linenr = 0;
22850 fc->returned = FALSE;
22851 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022853 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22854 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855
Bram Moolenaar33570922005-01-25 22:26:29 +000022856 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022857 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22859 * each argument variable and saves a lot of time.
22860 */
22861 /*
22862 * Init l: variables.
22863 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022864 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022865 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022866 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022867 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22868 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022869 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022870 name = v->di_key;
22871 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022873 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022875 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022876 v->di_tv.vval.v_dict = selfdict;
22877 ++selfdict->dv_refcount;
22878 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022879
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 /*
22881 * Init a: variables.
22882 * Set a:0 to "argcount".
22883 * Set a:000 to a list with room for the "..." arguments.
22884 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022885 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022886 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022887 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022888 /* Use "name" to avoid a warning from some compiler that checks the
22889 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022890 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022891 name = v->di_key;
22892 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022894 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022896 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022897 v->di_tv.vval.v_list = &fc->l_varlist;
22898 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22899 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22900 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022901
22902 /*
22903 * Set a:firstline to "firstline" and a:lastline to "lastline".
22904 * Set a:name to named arguments.
22905 * Set a:N to the "..." arguments.
22906 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022907 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022909 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 (varnumber_T)lastline);
22911 for (i = 0; i < argcount; ++i)
22912 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022913 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022914 if (ai < 0)
22915 /* named argument a:name */
22916 name = FUNCARG(fp, i);
22917 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022918 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022919 /* "..." argument a:1, a:2, etc. */
22920 sprintf((char *)numbuf, "%d", ai + 1);
22921 name = numbuf;
22922 }
22923 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22924 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022926 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22927 }
22928 else
22929 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022930 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22931 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 if (v == NULL)
22933 break;
22934 v->di_flags = DI_FLAGS_RO;
22935 }
22936 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022937 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022938
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022939 /* Note: the values are copied directly to avoid alloc/free.
22940 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022941 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022942 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022943
22944 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22945 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022946 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22947 fc->l_listitems[ai].li_tv = argvars[i];
22948 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022949 }
22950 }
22951
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 /* Don't redraw while executing the function. */
22953 ++RedrawingDisabled;
22954 save_sourcing_name = sourcing_name;
22955 save_sourcing_lnum = sourcing_lnum;
22956 sourcing_lnum = 1;
22957 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022958 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 if (sourcing_name != NULL)
22960 {
22961 if (save_sourcing_name != NULL
22962 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22963 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22964 else
22965 STRCPY(sourcing_name, "function ");
22966 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22967
22968 if (p_verbose >= 12)
22969 {
22970 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022971 verbose_enter_scroll();
22972
Bram Moolenaar555b2802005-05-19 21:08:39 +000022973 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 if (p_verbose >= 14)
22975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022977 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022978 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022979 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980
22981 msg_puts((char_u *)"(");
22982 for (i = 0; i < argcount; ++i)
22983 {
22984 if (i > 0)
22985 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022986 if (argvars[i].v_type == VAR_NUMBER)
22987 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 else
22989 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022990 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22991 if (s != NULL)
22992 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022993 if (vim_strsize(s) > MSG_BUF_CLEN)
22994 {
22995 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22996 s = buf;
22997 }
22998 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022999 vim_free(tofree);
23000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 }
23002 }
23003 msg_puts((char_u *)")");
23004 }
23005 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023006
23007 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 --no_wait_return;
23009 }
23010 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023011#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023012 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023013 {
23014 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23015 func_do_profile(fp);
23016 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023017 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023018 {
23019 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023020 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023021 profile_zero(&fp->uf_tm_children);
23022 }
23023 script_prof_save(&wait_start);
23024 }
23025#endif
23026
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023028 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 save_did_emsg = did_emsg;
23030 did_emsg = FALSE;
23031
23032 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023033 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23035
23036 --RedrawingDisabled;
23037
23038 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023039 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023041 clear_tv(rettv);
23042 rettv->v_type = VAR_NUMBER;
23043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 }
23045
Bram Moolenaar05159a02005-02-26 23:04:13 +000023046#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023047 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023048 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023050 profile_end(&call_start);
23051 profile_sub_wait(&wait_start, &call_start);
23052 profile_add(&fp->uf_tm_total, &call_start);
23053 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023054 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023055 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023056 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23057 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023058 }
23059 }
23060#endif
23061
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 /* when being verbose, mention the return value */
23063 if (p_verbose >= 12)
23064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023066 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023069 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023070 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023071 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023072 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023075 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023076 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023077 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023078 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023079
Bram Moolenaar555b2802005-05-19 21:08:39 +000023080 /* The value may be very long. Skip the middle part, so that we
23081 * have some idea how it starts and ends. smsg() would always
23082 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023083 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023084 if (s != NULL)
23085 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023086 if (vim_strsize(s) > MSG_BUF_CLEN)
23087 {
23088 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23089 s = buf;
23090 }
23091 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023092 vim_free(tofree);
23093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 }
23095 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023096
23097 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 --no_wait_return;
23099 }
23100
23101 vim_free(sourcing_name);
23102 sourcing_name = save_sourcing_name;
23103 sourcing_lnum = save_sourcing_lnum;
23104 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023105#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023106 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023107 script_prof_restore(&wait_start);
23108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109
23110 if (p_verbose >= 12 && sourcing_name != NULL)
23111 {
23112 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023113 verbose_enter_scroll();
23114
Bram Moolenaar555b2802005-05-19 21:08:39 +000023115 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023117
23118 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 --no_wait_return;
23120 }
23121
23122 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023123 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023125
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023126 /* If the a:000 list and the l: and a: dicts are not referenced we can
23127 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023128 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23129 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23130 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23131 {
23132 free_funccal(fc, FALSE);
23133 }
23134 else
23135 {
23136 hashitem_T *hi;
23137 listitem_T *li;
23138 int todo;
23139
23140 /* "fc" is still in use. This can happen when returning "a:000" or
23141 * assigning "l:" to a global variable.
23142 * Link "fc" in the list for garbage collection later. */
23143 fc->caller = previous_funccal;
23144 previous_funccal = fc;
23145
23146 /* Make a copy of the a: variables, since we didn't do that above. */
23147 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23148 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23149 {
23150 if (!HASHITEM_EMPTY(hi))
23151 {
23152 --todo;
23153 v = HI2DI(hi);
23154 copy_tv(&v->di_tv, &v->di_tv);
23155 }
23156 }
23157
23158 /* Make a copy of the a:000 items, since we didn't do that above. */
23159 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23160 copy_tv(&li->li_tv, &li->li_tv);
23161 }
23162}
23163
23164/*
23165 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023166 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023167 */
23168 static int
23169can_free_funccal(fc, copyID)
23170 funccall_T *fc;
23171 int copyID;
23172{
23173 return (fc->l_varlist.lv_copyID != copyID
23174 && fc->l_vars.dv_copyID != copyID
23175 && fc->l_avars.dv_copyID != copyID);
23176}
23177
23178/*
23179 * Free "fc" and what it contains.
23180 */
23181 static void
23182free_funccal(fc, free_val)
23183 funccall_T *fc;
23184 int free_val; /* a: vars were allocated */
23185{
23186 listitem_T *li;
23187
23188 /* The a: variables typevals may not have been allocated, only free the
23189 * allocated variables. */
23190 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23191
23192 /* free all l: variables */
23193 vars_clear(&fc->l_vars.dv_hashtab);
23194
23195 /* Free the a:000 variables if they were allocated. */
23196 if (free_val)
23197 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23198 clear_tv(&li->li_tv);
23199
23200 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201}
23202
23203/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 * Add a number variable "name" to dict "dp" with value "nr".
23205 */
23206 static void
23207add_nr_var(dp, v, name, nr)
23208 dict_T *dp;
23209 dictitem_T *v;
23210 char *name;
23211 varnumber_T nr;
23212{
23213 STRCPY(v->di_key, name);
23214 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23215 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23216 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023217 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023218 v->di_tv.vval.v_number = nr;
23219}
23220
23221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 * ":return [expr]"
23223 */
23224 void
23225ex_return(eap)
23226 exarg_T *eap;
23227{
23228 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 int returning = FALSE;
23231
23232 if (current_funccal == NULL)
23233 {
23234 EMSG(_("E133: :return not inside a function"));
23235 return;
23236 }
23237
23238 if (eap->skip)
23239 ++emsg_skip;
23240
23241 eap->nextcmd = NULL;
23242 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023243 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 {
23245 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023246 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023248 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 }
23250 /* It's safer to return also on error. */
23251 else if (!eap->skip)
23252 {
23253 /*
23254 * Return unless the expression evaluation has been cancelled due to an
23255 * aborting error, an interrupt, or an exception.
23256 */
23257 if (!aborting())
23258 returning = do_return(eap, FALSE, TRUE, NULL);
23259 }
23260
23261 /* When skipping or the return gets pending, advance to the next command
23262 * in this line (!returning). Otherwise, ignore the rest of the line.
23263 * Following lines will be ignored by get_func_line(). */
23264 if (returning)
23265 eap->nextcmd = NULL;
23266 else if (eap->nextcmd == NULL) /* no argument */
23267 eap->nextcmd = check_nextcmd(arg);
23268
23269 if (eap->skip)
23270 --emsg_skip;
23271}
23272
23273/*
23274 * Return from a function. Possibly makes the return pending. Also called
23275 * for a pending return at the ":endtry" or after returning from an extra
23276 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023277 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023278 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 * FALSE when the return gets pending.
23280 */
23281 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023282do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 exarg_T *eap;
23284 int reanimate;
23285 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023286 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287{
23288 int idx;
23289 struct condstack *cstack = eap->cstack;
23290
23291 if (reanimate)
23292 /* Undo the return. */
23293 current_funccal->returned = FALSE;
23294
23295 /*
23296 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23297 * not in its finally clause (which then is to be executed next) is found.
23298 * In this case, make the ":return" pending for execution at the ":endtry".
23299 * Otherwise, return normally.
23300 */
23301 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23302 if (idx >= 0)
23303 {
23304 cstack->cs_pending[idx] = CSTP_RETURN;
23305
23306 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023307 /* A pending return again gets pending. "rettv" points to an
23308 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023310 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 else
23312 {
23313 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023314 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023316 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023318 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
23320 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023321 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023322 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 else
23324 EMSG(_(e_outofmem));
23325 }
23326 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023327 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328
23329 if (reanimate)
23330 {
23331 /* The pending return value could be overwritten by a ":return"
23332 * without argument in a finally clause; reset the default
23333 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023334 current_funccal->rettv->v_type = VAR_NUMBER;
23335 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 }
23337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023338 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 }
23340 else
23341 {
23342 current_funccal->returned = TRUE;
23343
23344 /* If the return is carried out now, store the return value. For
23345 * a return immediately after reanimation, the value is already
23346 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023347 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023349 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023350 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023352 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 }
23354 }
23355
23356 return idx < 0;
23357}
23358
23359/*
23360 * Free the variable with a pending return value.
23361 */
23362 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023363discard_pending_return(rettv)
23364 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365{
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367}
23368
23369/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023370 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 * is an allocated string. Used by report_pending() for verbose messages.
23372 */
23373 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023374get_return_cmd(rettv)
23375 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023377 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023378 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023379 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023381 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023382 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023383 if (s == NULL)
23384 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023385
23386 STRCPY(IObuff, ":return ");
23387 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23388 if (STRLEN(s) + 8 >= IOSIZE)
23389 STRCPY(IObuff + IOSIZE - 4, "...");
23390 vim_free(tofree);
23391 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392}
23393
23394/*
23395 * Get next function line.
23396 * Called by do_cmdline() to get the next line.
23397 * Returns allocated string, or NULL for end of function.
23398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 char_u *
23400get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023401 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023403 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404{
Bram Moolenaar33570922005-01-25 22:26:29 +000023405 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023406 ufunc_T *fp = fcp->func;
23407 char_u *retval;
23408 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409
23410 /* If breakpoints have been added/deleted need to check for it. */
23411 if (fcp->dbg_tick != debug_tick)
23412 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023413 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 sourcing_lnum);
23415 fcp->dbg_tick = debug_tick;
23416 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023417#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023418 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023419 func_line_end(cookie);
23420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421
Bram Moolenaar05159a02005-02-26 23:04:13 +000023422 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023423 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23424 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 retval = NULL;
23426 else
23427 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023428 /* Skip NULL lines (continuation lines). */
23429 while (fcp->linenr < gap->ga_len
23430 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23431 ++fcp->linenr;
23432 if (fcp->linenr >= gap->ga_len)
23433 retval = NULL;
23434 else
23435 {
23436 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23437 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023438#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023439 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023440 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023441#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 }
23444
23445 /* Did we encounter a breakpoint? */
23446 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23447 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023448 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023450 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 sourcing_lnum);
23452 fcp->dbg_tick = debug_tick;
23453 }
23454
23455 return retval;
23456}
23457
Bram Moolenaar05159a02005-02-26 23:04:13 +000023458#if defined(FEAT_PROFILE) || defined(PROTO)
23459/*
23460 * Called when starting to read a function line.
23461 * "sourcing_lnum" must be correct!
23462 * When skipping lines it may not actually be executed, but we won't find out
23463 * until later and we need to store the time now.
23464 */
23465 void
23466func_line_start(cookie)
23467 void *cookie;
23468{
23469 funccall_T *fcp = (funccall_T *)cookie;
23470 ufunc_T *fp = fcp->func;
23471
23472 if (fp->uf_profiling && sourcing_lnum >= 1
23473 && sourcing_lnum <= fp->uf_lines.ga_len)
23474 {
23475 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023476 /* Skip continuation lines. */
23477 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23478 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023479 fp->uf_tml_execed = FALSE;
23480 profile_start(&fp->uf_tml_start);
23481 profile_zero(&fp->uf_tml_children);
23482 profile_get_wait(&fp->uf_tml_wait);
23483 }
23484}
23485
23486/*
23487 * Called when actually executing a function line.
23488 */
23489 void
23490func_line_exec(cookie)
23491 void *cookie;
23492{
23493 funccall_T *fcp = (funccall_T *)cookie;
23494 ufunc_T *fp = fcp->func;
23495
23496 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23497 fp->uf_tml_execed = TRUE;
23498}
23499
23500/*
23501 * Called when done with a function line.
23502 */
23503 void
23504func_line_end(cookie)
23505 void *cookie;
23506{
23507 funccall_T *fcp = (funccall_T *)cookie;
23508 ufunc_T *fp = fcp->func;
23509
23510 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23511 {
23512 if (fp->uf_tml_execed)
23513 {
23514 ++fp->uf_tml_count[fp->uf_tml_idx];
23515 profile_end(&fp->uf_tml_start);
23516 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023517 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023518 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23519 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023520 }
23521 fp->uf_tml_idx = -1;
23522 }
23523}
23524#endif
23525
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526/*
23527 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023528 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 */
23530 int
23531func_has_ended(cookie)
23532 void *cookie;
23533{
Bram Moolenaar33570922005-01-25 22:26:29 +000023534 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
23536 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23537 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023538 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 || fcp->returned);
23540}
23541
23542/*
23543 * return TRUE if cookie indicates a function which "abort"s on errors.
23544 */
23545 int
23546func_has_abort(cookie)
23547 void *cookie;
23548{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023549 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550}
23551
23552#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23553typedef enum
23554{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023555 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23556 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23557 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558} var_flavour_T;
23559
23560static var_flavour_T var_flavour __ARGS((char_u *varname));
23561
23562 static var_flavour_T
23563var_flavour(varname)
23564 char_u *varname;
23565{
23566 char_u *p = varname;
23567
23568 if (ASCII_ISUPPER(*p))
23569 {
23570 while (*(++p))
23571 if (ASCII_ISLOWER(*p))
23572 return VAR_FLAVOUR_SESSION;
23573 return VAR_FLAVOUR_VIMINFO;
23574 }
23575 else
23576 return VAR_FLAVOUR_DEFAULT;
23577}
23578#endif
23579
23580#if defined(FEAT_VIMINFO) || defined(PROTO)
23581/*
23582 * Restore global vars that start with a capital from the viminfo file
23583 */
23584 int
23585read_viminfo_varlist(virp, writing)
23586 vir_T *virp;
23587 int writing;
23588{
23589 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023590 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023591 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592
23593 if (!writing && (find_viminfo_parameter('!') != NULL))
23594 {
23595 tab = vim_strchr(virp->vir_line + 1, '\t');
23596 if (tab != NULL)
23597 {
23598 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023599 switch (*tab)
23600 {
23601 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023602#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023603 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023604#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023605 case 'D': type = VAR_DICT; break;
23606 case 'L': type = VAR_LIST; break;
23607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608
23609 tab = vim_strchr(tab, '\t');
23610 if (tab != NULL)
23611 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023612 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023613 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023614 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023616#ifdef FEAT_FLOAT
23617 else if (type == VAR_FLOAT)
23618 (void)string2float(tab + 1, &tv.vval.v_float);
23619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023621 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023622 if (type == VAR_DICT || type == VAR_LIST)
23623 {
23624 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23625
23626 if (etv == NULL)
23627 /* Failed to parse back the dict or list, use it as a
23628 * string. */
23629 tv.v_type = VAR_STRING;
23630 else
23631 {
23632 vim_free(tv.vval.v_string);
23633 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023634 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023635 }
23636 }
23637
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023638 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023639
23640 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023641 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023642 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23643 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 }
23645 }
23646 }
23647
23648 return viminfo_readline(virp);
23649}
23650
23651/*
23652 * Write global vars that start with a capital to the viminfo file
23653 */
23654 void
23655write_viminfo_varlist(fp)
23656 FILE *fp;
23657{
Bram Moolenaar33570922005-01-25 22:26:29 +000023658 hashitem_T *hi;
23659 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023660 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023661 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023662 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023663 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023664 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665
23666 if (find_viminfo_parameter('!') == NULL)
23667 return;
23668
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023669 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023671 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023672 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023676 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023677 this_var = HI2DI(hi);
23678 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023679 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023680 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023681 {
23682 case VAR_STRING: s = "STR"; break;
23683 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023684#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023685 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023686#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023687 case VAR_DICT: s = "DIC"; break;
23688 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023689 default: continue;
23690 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023691 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023692 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023693 if (p != NULL)
23694 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023695 vim_free(tofree);
23696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697 }
23698 }
23699}
23700#endif
23701
23702#if defined(FEAT_SESSION) || defined(PROTO)
23703 int
23704store_session_globals(fd)
23705 FILE *fd;
23706{
Bram Moolenaar33570922005-01-25 22:26:29 +000023707 hashitem_T *hi;
23708 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023709 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710 char_u *p, *t;
23711
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023712 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023713 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023715 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023717 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023718 this_var = HI2DI(hi);
23719 if ((this_var->di_tv.v_type == VAR_NUMBER
23720 || this_var->di_tv.v_type == VAR_STRING)
23721 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023722 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023723 /* Escape special characters with a backslash. Turn a LF and
23724 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023726 (char_u *)"\\\"\n\r");
23727 if (p == NULL) /* out of memory */
23728 break;
23729 for (t = p; *t != NUL; ++t)
23730 if (*t == '\n')
23731 *t = 'n';
23732 else if (*t == '\r')
23733 *t = 'r';
23734 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023735 this_var->di_key,
23736 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23737 : ' ',
23738 p,
23739 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23740 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023741 || put_eol(fd) == FAIL)
23742 {
23743 vim_free(p);
23744 return FAIL;
23745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 vim_free(p);
23747 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023748#ifdef FEAT_FLOAT
23749 else if (this_var->di_tv.v_type == VAR_FLOAT
23750 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23751 {
23752 float_T f = this_var->di_tv.vval.v_float;
23753 int sign = ' ';
23754
23755 if (f < 0)
23756 {
23757 f = -f;
23758 sign = '-';
23759 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023760 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023761 this_var->di_key, sign, f) < 0)
23762 || put_eol(fd) == FAIL)
23763 return FAIL;
23764 }
23765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 }
23767 }
23768 return OK;
23769}
23770#endif
23771
Bram Moolenaar661b1822005-07-28 22:36:45 +000023772/*
23773 * Display script name where an item was last set.
23774 * Should only be invoked when 'verbose' is non-zero.
23775 */
23776 void
23777last_set_msg(scriptID)
23778 scid_T scriptID;
23779{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023780 char_u *p;
23781
Bram Moolenaar661b1822005-07-28 22:36:45 +000023782 if (scriptID != 0)
23783 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023784 p = home_replace_save(NULL, get_scriptname(scriptID));
23785 if (p != NULL)
23786 {
23787 verbose_enter();
23788 MSG_PUTS(_("\n\tLast set from "));
23789 MSG_PUTS(p);
23790 vim_free(p);
23791 verbose_leave();
23792 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023793 }
23794}
23795
Bram Moolenaard812df62008-11-09 12:46:09 +000023796/*
23797 * List v:oldfiles in a nice way.
23798 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023799 void
23800ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023801 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023802{
23803 list_T *l = vimvars[VV_OLDFILES].vv_list;
23804 listitem_T *li;
23805 int nr = 0;
23806
23807 if (l == NULL)
23808 msg((char_u *)_("No old files"));
23809 else
23810 {
23811 msg_start();
23812 msg_scroll = TRUE;
23813 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23814 {
23815 msg_outnum((long)++nr);
23816 MSG_PUTS(": ");
23817 msg_outtrans(get_tv_string(&li->li_tv));
23818 msg_putchar('\n');
23819 out_flush(); /* output one line at a time */
23820 ui_breakcheck();
23821 }
23822 /* Assume "got_int" was set to truncate the listing. */
23823 got_int = FALSE;
23824
23825#ifdef FEAT_BROWSE_CMD
23826 if (cmdmod.browse)
23827 {
23828 quit_more = FALSE;
23829 nr = prompt_for_number(FALSE);
23830 msg_starthere();
23831 if (nr > 0)
23832 {
23833 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23834 (long)nr);
23835
23836 if (p != NULL)
23837 {
23838 p = expand_env_save(p);
23839 eap->arg = p;
23840 eap->cmdidx = CMD_edit;
23841 cmdmod.browse = FALSE;
23842 do_exedit(eap, NULL);
23843 vim_free(p);
23844 }
23845 }
23846 }
23847#endif
23848 }
23849}
23850
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851#endif /* FEAT_EVAL */
23852
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023854#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855
23856#ifdef WIN3264
23857/*
23858 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23859 */
23860static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23861static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23862static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23863
23864/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023865 * Get the short path (8.3) for the filename in "fnamep".
23866 * Only works for a valid file name.
23867 * When the path gets longer "fnamep" is changed and the allocated buffer
23868 * is put in "bufp".
23869 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23870 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 */
23872 static int
23873get_short_pathname(fnamep, bufp, fnamelen)
23874 char_u **fnamep;
23875 char_u **bufp;
23876 int *fnamelen;
23877{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023878 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 char_u *newbuf;
23880
23881 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 l = GetShortPathName(*fnamep, *fnamep, len);
23883 if (l > len - 1)
23884 {
23885 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023886 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 newbuf = vim_strnsave(*fnamep, l);
23888 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890
23891 vim_free(*bufp);
23892 *fnamep = *bufp = newbuf;
23893
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023894 /* Really should always succeed, as the buffer is big enough. */
23895 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 }
23897
23898 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023899 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900}
23901
23902/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023903 * Get the short path (8.3) for the filename in "fname". The converted
23904 * path is returned in "bufp".
23905 *
23906 * Some of the directories specified in "fname" may not exist. This function
23907 * will shorten the existing directories at the beginning of the path and then
23908 * append the remaining non-existing path.
23909 *
23910 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023911 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023912 * bufp - Pointer to an allocated buffer for the filename.
23913 * fnamelen - Length of the filename pointed to by fname
23914 *
23915 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 */
23917 static int
23918shortpath_for_invalid_fname(fname, bufp, fnamelen)
23919 char_u **fname;
23920 char_u **bufp;
23921 int *fnamelen;
23922{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023923 char_u *short_fname, *save_fname, *pbuf_unused;
23924 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023926 int old_len, len;
23927 int new_len, sfx_len;
23928 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929
23930 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023931 old_len = *fnamelen;
23932 save_fname = vim_strnsave(*fname, old_len);
23933 pbuf_unused = NULL;
23934 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023936 endp = save_fname + old_len - 1; /* Find the end of the copy */
23937 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023939 /*
23940 * Try shortening the supplied path till it succeeds by removing one
23941 * directory at a time from the tail of the path.
23942 */
23943 len = 0;
23944 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023946 /* go back one path-separator */
23947 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23948 --endp;
23949 if (endp <= save_fname)
23950 break; /* processed the complete path */
23951
23952 /*
23953 * Replace the path separator with a NUL and try to shorten the
23954 * resulting path.
23955 */
23956 ch = *endp;
23957 *endp = 0;
23958 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023959 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023960 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23961 {
23962 retval = FAIL;
23963 goto theend;
23964 }
23965 *endp = ch; /* preserve the string */
23966
23967 if (len > 0)
23968 break; /* successfully shortened the path */
23969
23970 /* failed to shorten the path. Skip the path separator */
23971 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 }
23973
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023974 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023976 /*
23977 * Succeeded in shortening the path. Now concatenate the shortened
23978 * path with the remaining path at the tail.
23979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023981 /* Compute the length of the new path. */
23982 sfx_len = (int)(save_endp - endp) + 1;
23983 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023985 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023987 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023989 /* There is not enough space in the currently allocated string,
23990 * copy it to a buffer big enough. */
23991 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023993 {
23994 retval = FAIL;
23995 goto theend;
23996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 }
23998 else
23999 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024000 /* Transfer short_fname to the main buffer (it's big enough),
24001 * unless get_short_pathname() did its work in-place. */
24002 *fname = *bufp = save_fname;
24003 if (short_fname != save_fname)
24004 vim_strncpy(save_fname, short_fname, len);
24005 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024007
24008 /* concat the not-shortened part of the path */
24009 vim_strncpy(*fname + len, endp, sfx_len);
24010 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024012
24013theend:
24014 vim_free(pbuf_unused);
24015 vim_free(save_fname);
24016
24017 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018}
24019
24020/*
24021 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024022 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 */
24024 static int
24025shortpath_for_partial(fnamep, bufp, fnamelen)
24026 char_u **fnamep;
24027 char_u **bufp;
24028 int *fnamelen;
24029{
24030 int sepcount, len, tflen;
24031 char_u *p;
24032 char_u *pbuf, *tfname;
24033 int hasTilde;
24034
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024035 /* Count up the path separators from the RHS.. so we know which part
24036 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024038 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 if (vim_ispathsep(*p))
24040 ++sepcount;
24041
24042 /* Need full path first (use expand_env() to remove a "~/") */
24043 hasTilde = (**fnamep == '~');
24044 if (hasTilde)
24045 pbuf = tfname = expand_env_save(*fnamep);
24046 else
24047 pbuf = tfname = FullName_save(*fnamep, FALSE);
24048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024049 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024051 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053
24054 if (len == 0)
24055 {
24056 /* Don't have a valid filename, so shorten the rest of the
24057 * path if we can. This CAN give us invalid 8.3 filenames, but
24058 * there's not a lot of point in guessing what it might be.
24059 */
24060 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024061 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 }
24064
24065 /* Count the paths backward to find the beginning of the desired string. */
24066 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024067 {
24068#ifdef FEAT_MBYTE
24069 if (has_mbyte)
24070 p -= mb_head_off(tfname, p);
24071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 if (vim_ispathsep(*p))
24073 {
24074 if (sepcount == 0 || (hasTilde && sepcount == 1))
24075 break;
24076 else
24077 sepcount --;
24078 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 if (hasTilde)
24081 {
24082 --p;
24083 if (p >= tfname)
24084 *p = '~';
24085 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 }
24088 else
24089 ++p;
24090
24091 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24092 vim_free(*bufp);
24093 *fnamelen = (int)STRLEN(p);
24094 *bufp = pbuf;
24095 *fnamep = p;
24096
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024097 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098}
24099#endif /* WIN3264 */
24100
24101/*
24102 * Adjust a filename, according to a string of modifiers.
24103 * *fnamep must be NUL terminated when called. When returning, the length is
24104 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024105 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106 * When there is an error, *fnamep is set to NULL.
24107 */
24108 int
24109modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24110 char_u *src; /* string with modifiers */
24111 int *usedlen; /* characters after src that are used */
24112 char_u **fnamep; /* file name so far */
24113 char_u **bufp; /* buffer for allocated file name or NULL */
24114 int *fnamelen; /* length of fnamep */
24115{
24116 int valid = 0;
24117 char_u *tail;
24118 char_u *s, *p, *pbuf;
24119 char_u dirname[MAXPATHL];
24120 int c;
24121 int has_fullname = 0;
24122#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024123 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 int has_shortname = 0;
24125#endif
24126
24127repeat:
24128 /* ":p" - full path/file_name */
24129 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24130 {
24131 has_fullname = 1;
24132
24133 valid |= VALID_PATH;
24134 *usedlen += 2;
24135
24136 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24137 if ((*fnamep)[0] == '~'
24138#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24139 && ((*fnamep)[1] == '/'
24140# ifdef BACKSLASH_IN_FILENAME
24141 || (*fnamep)[1] == '\\'
24142# endif
24143 || (*fnamep)[1] == NUL)
24144
24145#endif
24146 )
24147 {
24148 *fnamep = expand_env_save(*fnamep);
24149 vim_free(*bufp); /* free any allocated file name */
24150 *bufp = *fnamep;
24151 if (*fnamep == NULL)
24152 return -1;
24153 }
24154
24155 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024156 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 {
24158 if (vim_ispathsep(*p)
24159 && p[1] == '.'
24160 && (p[2] == NUL
24161 || vim_ispathsep(p[2])
24162 || (p[2] == '.'
24163 && (p[3] == NUL || vim_ispathsep(p[3])))))
24164 break;
24165 }
24166
24167 /* FullName_save() is slow, don't use it when not needed. */
24168 if (*p != NUL || !vim_isAbsName(*fnamep))
24169 {
24170 *fnamep = FullName_save(*fnamep, *p != NUL);
24171 vim_free(*bufp); /* free any allocated file name */
24172 *bufp = *fnamep;
24173 if (*fnamep == NULL)
24174 return -1;
24175 }
24176
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024177#ifdef WIN3264
24178# if _WIN32_WINNT >= 0x0500
24179 if (vim_strchr(*fnamep, '~') != NULL)
24180 {
24181 /* Expand 8.3 filename to full path. Needed to make sure the same
24182 * file does not have two different names.
24183 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24184 p = alloc(_MAX_PATH + 1);
24185 if (p != NULL)
24186 {
24187 if (GetLongPathName(*fnamep, p, MAXPATHL))
24188 {
24189 vim_free(*bufp);
24190 *bufp = *fnamep = p;
24191 }
24192 else
24193 vim_free(p);
24194 }
24195 }
24196# endif
24197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 /* Append a path separator to a directory. */
24199 if (mch_isdir(*fnamep))
24200 {
24201 /* Make room for one or two extra characters. */
24202 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24203 vim_free(*bufp); /* free any allocated file name */
24204 *bufp = *fnamep;
24205 if (*fnamep == NULL)
24206 return -1;
24207 add_pathsep(*fnamep);
24208 }
24209 }
24210
24211 /* ":." - path relative to the current directory */
24212 /* ":~" - path relative to the home directory */
24213 /* ":8" - shortname path - postponed till after */
24214 while (src[*usedlen] == ':'
24215 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24216 {
24217 *usedlen += 2;
24218 if (c == '8')
24219 {
24220#ifdef WIN3264
24221 has_shortname = 1; /* Postpone this. */
24222#endif
24223 continue;
24224 }
24225 pbuf = NULL;
24226 /* Need full path first (use expand_env() to remove a "~/") */
24227 if (!has_fullname)
24228 {
24229 if (c == '.' && **fnamep == '~')
24230 p = pbuf = expand_env_save(*fnamep);
24231 else
24232 p = pbuf = FullName_save(*fnamep, FALSE);
24233 }
24234 else
24235 p = *fnamep;
24236
24237 has_fullname = 0;
24238
24239 if (p != NULL)
24240 {
24241 if (c == '.')
24242 {
24243 mch_dirname(dirname, MAXPATHL);
24244 s = shorten_fname(p, dirname);
24245 if (s != NULL)
24246 {
24247 *fnamep = s;
24248 if (pbuf != NULL)
24249 {
24250 vim_free(*bufp); /* free any allocated file name */
24251 *bufp = pbuf;
24252 pbuf = NULL;
24253 }
24254 }
24255 }
24256 else
24257 {
24258 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24259 /* Only replace it when it starts with '~' */
24260 if (*dirname == '~')
24261 {
24262 s = vim_strsave(dirname);
24263 if (s != NULL)
24264 {
24265 *fnamep = s;
24266 vim_free(*bufp);
24267 *bufp = s;
24268 }
24269 }
24270 }
24271 vim_free(pbuf);
24272 }
24273 }
24274
24275 tail = gettail(*fnamep);
24276 *fnamelen = (int)STRLEN(*fnamep);
24277
24278 /* ":h" - head, remove "/file_name", can be repeated */
24279 /* Don't remove the first "/" or "c:\" */
24280 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24281 {
24282 valid |= VALID_HEAD;
24283 *usedlen += 2;
24284 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024285 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024286 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 *fnamelen = (int)(tail - *fnamep);
24288#ifdef VMS
24289 if (*fnamelen > 0)
24290 *fnamelen += 1; /* the path separator is part of the path */
24291#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024292 if (*fnamelen == 0)
24293 {
24294 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24295 p = vim_strsave((char_u *)".");
24296 if (p == NULL)
24297 return -1;
24298 vim_free(*bufp);
24299 *bufp = *fnamep = tail = p;
24300 *fnamelen = 1;
24301 }
24302 else
24303 {
24304 while (tail > s && !after_pathsep(s, tail))
24305 mb_ptr_back(*fnamep, tail);
24306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 }
24308
24309 /* ":8" - shortname */
24310 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24311 {
24312 *usedlen += 2;
24313#ifdef WIN3264
24314 has_shortname = 1;
24315#endif
24316 }
24317
24318#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024319 /*
24320 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 */
24322 if (has_shortname)
24323 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024324 /* Copy the string if it is shortened by :h and when it wasn't copied
24325 * yet, because we are going to change it in place. Avoids changing
24326 * the buffer name for "%:8". */
24327 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 {
24329 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024330 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 return -1;
24332 vim_free(*bufp);
24333 *bufp = *fnamep = p;
24334 }
24335
24336 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024337 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 if (!has_fullname && !vim_isAbsName(*fnamep))
24339 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024340 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341 return -1;
24342 }
24343 else
24344 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024345 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346
Bram Moolenaardc935552011-08-17 15:23:23 +020024347 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024349 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 return -1;
24351
24352 if (l == 0)
24353 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024354 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024356 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 return -1;
24358 }
24359 *fnamelen = l;
24360 }
24361 }
24362#endif /* WIN3264 */
24363
24364 /* ":t" - tail, just the basename */
24365 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24366 {
24367 *usedlen += 2;
24368 *fnamelen -= (int)(tail - *fnamep);
24369 *fnamep = tail;
24370 }
24371
24372 /* ":e" - extension, can be repeated */
24373 /* ":r" - root, without extension, can be repeated */
24374 while (src[*usedlen] == ':'
24375 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24376 {
24377 /* find a '.' in the tail:
24378 * - for second :e: before the current fname
24379 * - otherwise: The last '.'
24380 */
24381 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24382 s = *fnamep - 2;
24383 else
24384 s = *fnamep + *fnamelen - 1;
24385 for ( ; s > tail; --s)
24386 if (s[0] == '.')
24387 break;
24388 if (src[*usedlen + 1] == 'e') /* :e */
24389 {
24390 if (s > tail)
24391 {
24392 *fnamelen += (int)(*fnamep - (s + 1));
24393 *fnamep = s + 1;
24394#ifdef VMS
24395 /* cut version from the extension */
24396 s = *fnamep + *fnamelen - 1;
24397 for ( ; s > *fnamep; --s)
24398 if (s[0] == ';')
24399 break;
24400 if (s > *fnamep)
24401 *fnamelen = s - *fnamep;
24402#endif
24403 }
24404 else if (*fnamep <= tail)
24405 *fnamelen = 0;
24406 }
24407 else /* :r */
24408 {
24409 if (s > tail) /* remove one extension */
24410 *fnamelen = (int)(s - *fnamep);
24411 }
24412 *usedlen += 2;
24413 }
24414
24415 /* ":s?pat?foo?" - substitute */
24416 /* ":gs?pat?foo?" - global substitute */
24417 if (src[*usedlen] == ':'
24418 && (src[*usedlen + 1] == 's'
24419 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24420 {
24421 char_u *str;
24422 char_u *pat;
24423 char_u *sub;
24424 int sep;
24425 char_u *flags;
24426 int didit = FALSE;
24427
24428 flags = (char_u *)"";
24429 s = src + *usedlen + 2;
24430 if (src[*usedlen + 1] == 'g')
24431 {
24432 flags = (char_u *)"g";
24433 ++s;
24434 }
24435
24436 sep = *s++;
24437 if (sep)
24438 {
24439 /* find end of pattern */
24440 p = vim_strchr(s, sep);
24441 if (p != NULL)
24442 {
24443 pat = vim_strnsave(s, (int)(p - s));
24444 if (pat != NULL)
24445 {
24446 s = p + 1;
24447 /* find end of substitution */
24448 p = vim_strchr(s, sep);
24449 if (p != NULL)
24450 {
24451 sub = vim_strnsave(s, (int)(p - s));
24452 str = vim_strnsave(*fnamep, *fnamelen);
24453 if (sub != NULL && str != NULL)
24454 {
24455 *usedlen = (int)(p + 1 - src);
24456 s = do_string_sub(str, pat, sub, flags);
24457 if (s != NULL)
24458 {
24459 *fnamep = s;
24460 *fnamelen = (int)STRLEN(s);
24461 vim_free(*bufp);
24462 *bufp = s;
24463 didit = TRUE;
24464 }
24465 }
24466 vim_free(sub);
24467 vim_free(str);
24468 }
24469 vim_free(pat);
24470 }
24471 }
24472 /* after using ":s", repeat all the modifiers */
24473 if (didit)
24474 goto repeat;
24475 }
24476 }
24477
Bram Moolenaar26df0922014-02-23 23:39:13 +010024478 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24479 {
24480 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24481 if (p == NULL)
24482 return -1;
24483 vim_free(*bufp);
24484 *bufp = *fnamep = p;
24485 *fnamelen = (int)STRLEN(p);
24486 *usedlen += 2;
24487 }
24488
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 return valid;
24490}
24491
24492/*
24493 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24494 * "flags" can be "g" to do a global substitute.
24495 * Returns an allocated string, NULL for error.
24496 */
24497 char_u *
24498do_string_sub(str, pat, sub, flags)
24499 char_u *str;
24500 char_u *pat;
24501 char_u *sub;
24502 char_u *flags;
24503{
24504 int sublen;
24505 regmatch_T regmatch;
24506 int i;
24507 int do_all;
24508 char_u *tail;
24509 garray_T ga;
24510 char_u *ret;
24511 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024512 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513
24514 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24515 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024516 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517
24518 ga_init2(&ga, 1, 200);
24519
24520 do_all = (flags[0] == 'g');
24521
24522 regmatch.rm_ic = p_ic;
24523 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24524 if (regmatch.regprog != NULL)
24525 {
24526 tail = str;
24527 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24528 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024529 /* Skip empty match except for first match. */
24530 if (regmatch.startp[0] == regmatch.endp[0])
24531 {
24532 if (zero_width == regmatch.startp[0])
24533 {
24534 /* avoid getting stuck on a match with an empty string */
24535 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24536 ++ga.ga_len;
24537 continue;
24538 }
24539 zero_width = regmatch.startp[0];
24540 }
24541
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 /*
24543 * Get some space for a temporary buffer to do the substitution
24544 * into. It will contain:
24545 * - The text up to where the match is.
24546 * - The substituted text.
24547 * - The text after the match.
24548 */
24549 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24550 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24551 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24552 {
24553 ga_clear(&ga);
24554 break;
24555 }
24556
24557 /* copy the text up to where the match is */
24558 i = (int)(regmatch.startp[0] - tail);
24559 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24560 /* add the substituted text */
24561 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24562 + ga.ga_len + i, TRUE, TRUE, FALSE);
24563 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024564 tail = regmatch.endp[0];
24565 if (*tail == NUL)
24566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 if (!do_all)
24568 break;
24569 }
24570
24571 if (ga.ga_data != NULL)
24572 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24573
Bram Moolenaar473de612013-06-08 18:19:48 +020024574 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 }
24576
24577 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24578 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024579 if (p_cpo == empty_option)
24580 p_cpo = save_cpo;
24581 else
24582 /* Darn, evaluating {sub} expression changed the value. */
24583 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584
24585 return ret;
24586}
24587
24588#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */