blob: df8c09475de0db491279d5914bf6ed00a11521ce [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 Moolenaar57ebe6e2014-04-05 18:55:46 +0200839static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar33570922005-01-25 22:26:29 +0000840
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200841
842#ifdef EBCDIC
843static int compare_func_name __ARGS((const void *s1, const void *s2));
844static void sortFunctions __ARGS(());
845#endif
846
Bram Moolenaar33570922005-01-25 22:26:29 +0000847/*
848 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000849 */
850 void
851eval_init()
852{
Bram Moolenaar33570922005-01-25 22:26:29 +0000853 int i;
854 struct vimvar *p;
855
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200856 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
857 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200858 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000859 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000860 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
862 for (i = 0; i < VV_LEN; ++i)
863 {
864 p = &vimvars[i];
865 STRCPY(p->vv_di.di_key, p->vv_name);
866 if (p->vv_flags & VV_RO)
867 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
868 else if (p->vv_flags & VV_RO_SBX)
869 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
870 else
871 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000872
873 /* add to v: scope dict, unless the value is not always available */
874 if (p->vv_type != VAR_UNKNOWN)
875 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000877 /* add to compat scope dict */
878 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000880 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100881 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200882 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
885 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100886 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887 */
888 sortFunctions();
889#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000890}
891
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000892#if defined(EXITFREE) || defined(PROTO)
893 void
894eval_clear()
895{
896 int i;
897 struct vimvar *p;
898
899 for (i = 0; i < VV_LEN; ++i)
900 {
901 p = &vimvars[i];
902 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000904 vim_free(p->vv_str);
905 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000906 }
907 else if (p->vv_di.di_tv.v_type == VAR_LIST)
908 {
909 list_unref(p->vv_list);
910 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000912 }
913 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000914 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 hash_clear(&compat_hashtab);
916
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100918# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200919 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921
922 /* global variables */
923 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000924
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000925 /* autoloaded script names */
926 ga_clear_strings(&ga_loaded);
927
Bram Moolenaarcca74132013-09-25 21:00:28 +0200928 /* Script-local variables. First clear all the variables and in a second
929 * loop free the scriptvar_T, because a variable in one script might hold
930 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200932 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200935 ga_clear(&ga_scripts);
936
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000937 /* unreferenced lists and dicts */
938 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000939
940 /* functions */
941 free_all_functions();
942 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000943}
944#endif
945
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 * Return the name of the executed function.
948 */
949 char_u *
950func_name(cookie)
951 void *cookie;
952{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000953 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956/*
957 * Return the address holding the next breakpoint line for a funccall cookie.
958 */
959 linenr_T *
960func_breakpoint(cookie)
961 void *cookie;
962{
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
967 * Return the address holding the debug tick for a funccall cookie.
968 */
969 int *
970func_dbg_tick(cookie)
971 void *cookie;
972{
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/*
977 * Return the nesting level for a funccall cookie.
978 */
979 int
980func_level(cookie)
981 void *cookie;
982{
Bram Moolenaar33570922005-01-25 22:26:29 +0000983 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984}
985
986/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000987funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000989/* pointer to list of previously used funccal, still around because some
990 * item in it is still being used. */
991funccall_T *previous_funccal = NULL;
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
994 * Return TRUE when a function was ended by a ":return" command.
995 */
996 int
997current_func_returned()
998{
999 return current_funccal->returned;
1000}
1001
1002
1003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * Set an internal variable to a string value. Creates the variable if it does
1005 * not already exist.
1006 */
1007 void
1008set_internal_string_var(name, value)
1009 char_u *name;
1010 char_u *value;
1011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001013 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 val = vim_strsave(value);
1016 if (val != NULL)
1017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001019 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001022 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 }
1025}
1026
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001028static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static char_u *redir_endp = NULL;
1030static char_u *redir_varname = NULL;
1031
1032/*
1033 * Start recording command output to a variable
1034 * Returns OK if successfully completed the setup. FAIL otherwise.
1035 */
1036 int
1037var_redir_start(name, append)
1038 char_u *name;
1039 int append; /* append to an existing variable */
1040{
1041 int save_emsg;
1042 int err;
1043 typval_T tv;
1044
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001045 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001046 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 {
1048 EMSG(_(e_invarg));
1049 return FAIL;
1050 }
1051
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001052 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 redir_varname = vim_strsave(name);
1054 if (redir_varname == NULL)
1055 return FAIL;
1056
1057 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1058 if (redir_lval == NULL)
1059 {
1060 var_redir_stop();
1061 return FAIL;
1062 }
1063
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001064 /* The output is stored in growarray "redir_ga" until redirection ends. */
1065 ga_init2(&redir_ga, (int)sizeof(char), 500);
1066
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001068 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001069 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1071 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001072 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (redir_endp != NULL && *redir_endp != NUL)
1074 /* Trailing characters are present after the variable name */
1075 EMSG(_(e_trailing));
1076 else
1077 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001078 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 var_redir_stop();
1080 return FAIL;
1081 }
1082
1083 /* check if we can write to the variable: set it to or append an empty
1084 * string */
1085 save_emsg = did_emsg;
1086 did_emsg = FALSE;
1087 tv.v_type = VAR_STRING;
1088 tv.vval.v_string = (char_u *)"";
1089 if (append)
1090 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1091 else
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001093 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001095 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 if (err)
1097 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001098 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 var_redir_stop();
1100 return FAIL;
1101 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102
1103 return OK;
1104}
1105
1106/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 * Append "value[value_len]" to the variable set by var_redir_start().
1108 * The actual appending is postponed until redirection ends, because the value
1109 * appended may in fact be the string we write to, changing it may cause freed
1110 * memory to be used:
1111 * :redir => foo
1112 * :let foo
1113 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 */
1115 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
1122 if (redir_lval == NULL)
1123 return;
1124
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 {
1132 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001133 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 }
1135 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137}
1138
1139/*
1140 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 */
1143 void
1144var_redir_stop()
1145{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 typval_T tv;
1147
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 if (redir_lval != NULL)
1149 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 /* If there was no error: assign the text to the variable. */
1151 if (redir_endp != NULL)
1152 {
1153 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 /* Call get_lval() again, if it's inside a Dict or List it may
1157 * have changed. */
1158 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001159 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1161 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1162 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* free the collected output */
1166 vim_free(redir_ga.ga_data);
1167 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 vim_free(redir_lval);
1170 redir_lval = NULL;
1171 }
1172 vim_free(redir_varname);
1173 redir_varname = NULL;
1174}
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176# if defined(FEAT_MBYTE) || defined(PROTO)
1177 int
1178eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1179 char_u *enc_from;
1180 char_u *enc_to;
1181 char_u *fname_from;
1182 char_u *fname_to;
1183{
1184 int err = FALSE;
1185
1186 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1187 set_vim_var_string(VV_CC_TO, enc_to, -1);
1188 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1189 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1190 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1191 err = TRUE;
1192 set_vim_var_string(VV_CC_FROM, NULL, -1);
1193 set_vim_var_string(VV_CC_TO, NULL, -1);
1194 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1195 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1196
1197 if (err)
1198 return FAIL;
1199 return OK;
1200}
1201# endif
1202
1203# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1204 int
1205eval_printexpr(fname, args)
1206 char_u *fname;
1207 char_u *args;
1208{
1209 int err = FALSE;
1210
1211 set_vim_var_string(VV_FNAME_IN, fname, -1);
1212 set_vim_var_string(VV_CMDARG, args, -1);
1213 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1214 err = TRUE;
1215 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1216 set_vim_var_string(VV_CMDARG, NULL, -1);
1217
1218 if (err)
1219 {
1220 mch_remove(fname);
1221 return FAIL;
1222 }
1223 return OK;
1224}
1225# endif
1226
1227# if defined(FEAT_DIFF) || defined(PROTO)
1228 void
1229eval_diff(origfile, newfile, outfile)
1230 char_u *origfile;
1231 char_u *newfile;
1232 char_u *outfile;
1233{
1234 int err = FALSE;
1235
1236 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1237 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1238 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1239 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1240 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1241 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1242 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1243}
1244
1245 void
1246eval_patch(origfile, difffile, outfile)
1247 char_u *origfile;
1248 char_u *difffile;
1249 char_u *outfile;
1250{
1251 int err;
1252
1253 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1255 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1256 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1259 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1260}
1261# endif
1262
1263/*
1264 * Top level evaluation function, returning a boolean.
1265 * Sets "error" to TRUE if there was an error.
1266 * Return TRUE or FALSE.
1267 */
1268 int
1269eval_to_bool(arg, error, nextcmd, skip)
1270 char_u *arg;
1271 int *error;
1272 char_u **nextcmd;
1273 int skip; /* only parse, don't execute */
1274{
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 int retval = FALSE;
1277
1278 if (skip)
1279 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 else
1283 {
1284 *error = FALSE;
1285 if (!skip)
1286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001287 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 }
1291 if (skip)
1292 --emsg_skip;
1293
1294 return retval;
1295}
1296
1297/*
1298 * Top level evaluation function, returning a string. If "skip" is TRUE,
1299 * only parsing to "nextcmd" is done, without reporting errors. Return
1300 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1301 */
1302 char_u *
1303eval_to_string_skip(arg, nextcmd, skip)
1304 char_u *arg;
1305 char_u **nextcmd;
1306 int skip; /* only parse, don't execute */
1307{
Bram Moolenaar33570922005-01-25 22:26:29 +00001308 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 char_u *retval;
1310
1311 if (skip)
1312 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 retval = NULL;
1315 else
1316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 retval = vim_strsave(get_tv_string(&tv));
1318 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
1320 if (skip)
1321 --emsg_skip;
1322
1323 return retval;
1324}
1325
1326/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327 * Skip over an expression at "*pp".
1328 * Return FAIL for an error, OK otherwise.
1329 */
1330 int
1331skip_expr(pp)
1332 char_u **pp;
1333{
Bram Moolenaar33570922005-01-25 22:26:29 +00001334 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001335
1336 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001338}
1339
1340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 * When "convert" is TRUE convert a List into a sequence of lines and convert
1343 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 * Return pointer to allocated memory, or NULL for failure.
1345 */
1346 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 char_u *arg;
1349 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
Bram Moolenaar33570922005-01-25 22:26:29 +00001352 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001355#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 retval = NULL;
1361 else
1362 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 {
1365 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001366 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001367 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 if (tv.vval.v_list->lv_len > 0)
1370 ga_append(&ga, NL);
1371 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 ga_append(&ga, NUL);
1373 retval = (char_u *)ga.ga_data;
1374 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001375#ifdef FEAT_FLOAT
1376 else if (convert && tv.v_type == VAR_FLOAT)
1377 {
1378 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1379 retval = vim_strsave(numbuf);
1380 }
1381#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001382 else
1383 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386
1387 return retval;
1388}
1389
1390/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 * Call eval_to_string() without using current local variables and using
1392 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 */
1394 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 char_u *arg;
1397 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400 char_u *retval;
1401 void *save_funccalp;
1402
1403 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 ++sandbox;
1406 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001407 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001408 if (use_sandbox)
1409 --sandbox;
1410 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 restore_funccal(save_funccalp);
1412 return retval;
1413}
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415/*
1416 * Top level evaluation function, returning a number.
1417 * Evaluates "expr" silently.
1418 * Returns -1 for an error.
1419 */
1420 int
1421eval_to_number(expr)
1422 char_u *expr;
1423{
Bram Moolenaar33570922005-01-25 22:26:29 +00001424 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001426 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 ++emsg_off;
1429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 retval = -1;
1432 else
1433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001434 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437 --emsg_off;
1438
1439 return retval;
1440}
1441
Bram Moolenaara40058a2005-07-11 22:42:07 +00001442/*
1443 * Prepare v: variable "idx" to be used.
1444 * Save the current typeval in "save_tv".
1445 * When not used yet add the variable to the v: hashtable.
1446 */
1447 static void
1448prepare_vimvar(idx, save_tv)
1449 int idx;
1450 typval_T *save_tv;
1451{
1452 *save_tv = vimvars[idx].vv_tv;
1453 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1454 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1455}
1456
1457/*
1458 * Restore v: variable "idx" to typeval "save_tv".
1459 * When no longer defined, remove the variable from the v: hashtable.
1460 */
1461 static void
1462restore_vimvar(idx, save_tv)
1463 int idx;
1464 typval_T *save_tv;
1465{
1466 hashitem_T *hi;
1467
Bram Moolenaara40058a2005-07-11 22:42:07 +00001468 vimvars[idx].vv_tv = *save_tv;
1469 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1470 {
1471 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1472 if (HASHITEM_EMPTY(hi))
1473 EMSG2(_(e_intern2), "restore_vimvar()");
1474 else
1475 hash_remove(&vimvarht, hi);
1476 }
1477}
1478
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001479#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480/*
1481 * Evaluate an expression to a list with suggestions.
1482 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001483 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001484 */
1485 list_T *
1486eval_spell_expr(badword, expr)
1487 char_u *badword;
1488 char_u *expr;
1489{
1490 typval_T save_val;
1491 typval_T rettv;
1492 list_T *list = NULL;
1493 char_u *p = skipwhite(expr);
1494
1495 /* Set "v:val" to the bad word. */
1496 prepare_vimvar(VV_VAL, &save_val);
1497 vimvars[VV_VAL].vv_type = VAR_STRING;
1498 vimvars[VV_VAL].vv_str = badword;
1499 if (p_verbose == 0)
1500 ++emsg_off;
1501
1502 if (eval1(&p, &rettv, TRUE) == OK)
1503 {
1504 if (rettv.v_type != VAR_LIST)
1505 clear_tv(&rettv);
1506 else
1507 list = rettv.vval.v_list;
1508 }
1509
1510 if (p_verbose == 0)
1511 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001512 restore_vimvar(VV_VAL, &save_val);
1513
1514 return list;
1515}
1516
1517/*
1518 * "list" is supposed to contain two items: a word and a number. Return the
1519 * word in "pp" and the number as the return value.
1520 * Return -1 if anything isn't right.
1521 * Used to get the good word and score from the eval_spell_expr() result.
1522 */
1523 int
1524get_spellword(list, pp)
1525 list_T *list;
1526 char_u **pp;
1527{
1528 listitem_T *li;
1529
1530 li = list->lv_first;
1531 if (li == NULL)
1532 return -1;
1533 *pp = get_tv_string(&li->li_tv);
1534
1535 li = li->li_next;
1536 if (li == NULL)
1537 return -1;
1538 return get_tv_number(&li->li_tv);
1539}
1540#endif
1541
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 * Top level evaluation function.
1544 * Returns an allocated typval_T with the result.
1545 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 */
1547 typval_T *
1548eval_expr(arg, nextcmd)
1549 char_u *arg;
1550 char_u **nextcmd;
1551{
1552 typval_T *tv;
1553
1554 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001555 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001556 {
1557 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001558 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559 }
1560
1561 return tv;
1562}
1563
1564
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 * Uses argv[argc] for the function arguments. Only Number and String
1568 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001571 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001572call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 char_u *func;
1574 int argc;
1575 char_u **argv;
1576 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001577 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
Bram Moolenaar33570922005-01-25 22:26:29 +00001580 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 long n;
1582 int len;
1583 int i;
1584 int doesrange;
1585 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001588 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001590 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 for (i = 0; i < argc; i++)
1593 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001594 /* Pass a NULL or empty argument as an empty string */
1595 if (argv[i] == NULL || *argv[i] == NUL)
1596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001597 argvars[i].v_type = VAR_STRING;
1598 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001599 continue;
1600 }
1601
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001602 if (str_arg_only)
1603 len = 0;
1604 else
1605 /* Recognize a number argument, the others must be strings. */
1606 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (len != 0 && len == (int)STRLEN(argv[i]))
1608 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001609 argvars[i].v_type = VAR_NUMBER;
1610 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 else
1613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001614 argvars[i].v_type = VAR_STRING;
1615 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617 }
1618
1619 if (safe)
1620 {
1621 save_funccalp = save_funccal();
1622 ++sandbox;
1623 }
1624
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1626 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (safe)
1630 {
1631 --sandbox;
1632 restore_funccal(save_funccalp);
1633 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 vim_free(argvars);
1635
1636 if (ret == FAIL)
1637 clear_tv(rettv);
1638
1639 return ret;
1640}
1641
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001642/*
1643 * Call vimL function "func" and return the result as a number.
1644 * Returns -1 when calling the function fails.
1645 * Uses argv[argc] for the function arguments.
1646 */
1647 long
1648call_func_retnr(func, argc, argv, safe)
1649 char_u *func;
1650 int argc;
1651 char_u **argv;
1652 int safe; /* use the sandbox */
1653{
1654 typval_T rettv;
1655 long retval;
1656
1657 /* All arguments are passed as strings, no conversion to number. */
1658 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1659 return -1;
1660
1661 retval = get_tv_number_chk(&rettv, NULL);
1662 clear_tv(&rettv);
1663 return retval;
1664}
1665
1666#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1667 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1668
Bram Moolenaar4f688582007-07-24 12:34:30 +00001669# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001671 * Call vimL function "func" and return the result as a string.
1672 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 * Uses argv[argc] for the function arguments.
1674 */
1675 void *
1676call_func_retstr(func, argc, argv, safe)
1677 char_u *func;
1678 int argc;
1679 char_u **argv;
1680 int safe; /* use the sandbox */
1681{
1682 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001683 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001685 /* All arguments are passed as strings, no conversion to number. */
1686 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 return NULL;
1688
1689 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 return retval;
1692}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001693# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001694
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001695/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001696 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 */
1700 void *
1701call_func_retlist(func, argc, argv, safe)
1702 char_u *func;
1703 int argc;
1704 char_u **argv;
1705 int safe; /* use the sandbox */
1706{
1707 typval_T rettv;
1708
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 return NULL;
1712
1713 if (rettv.v_type != VAR_LIST)
1714 {
1715 clear_tv(&rettv);
1716 return NULL;
1717 }
1718
1719 return rettv.vval.v_list;
1720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722
1723/*
1724 * Save the current function call pointer, and set it to NULL.
1725 * Used when executing autocommands and for ":source".
1726 */
1727 void *
1728save_funccal()
1729{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001730 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 current_funccal = NULL;
1733 return (void *)fc;
1734}
1735
1736 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001737restore_funccal(vfc)
1738 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001740 funccall_T *fc = (funccall_T *)vfc;
1741
1742 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743}
1744
Bram Moolenaar05159a02005-02-26 23:04:13 +00001745#if defined(FEAT_PROFILE) || defined(PROTO)
1746/*
1747 * Prepare profiling for entering a child or something else that is not
1748 * counted for the script/function itself.
1749 * Should always be called in pair with prof_child_exit().
1750 */
1751 void
1752prof_child_enter(tm)
1753 proftime_T *tm; /* place to store waittime */
1754{
1755 funccall_T *fc = current_funccal;
1756
1757 if (fc != NULL && fc->func->uf_profiling)
1758 profile_start(&fc->prof_child);
1759 script_prof_save(tm);
1760}
1761
1762/*
1763 * Take care of time spent in a child.
1764 * Should always be called after prof_child_enter().
1765 */
1766 void
1767prof_child_exit(tm)
1768 proftime_T *tm; /* where waittime was stored */
1769{
1770 funccall_T *fc = current_funccal;
1771
1772 if (fc != NULL && fc->func->uf_profiling)
1773 {
1774 profile_end(&fc->prof_child);
1775 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1776 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1777 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1778 }
1779 script_prof_restore(tm);
1780}
1781#endif
1782
1783
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#ifdef FEAT_FOLDING
1785/*
1786 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1787 * it in "*cp". Doesn't give error messages.
1788 */
1789 int
1790eval_foldexpr(arg, cp)
1791 char_u *arg;
1792 int *cp;
1793{
Bram Moolenaar33570922005-01-25 22:26:29 +00001794 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 int retval;
1796 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001797 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1798 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001801 if (use_sandbox)
1802 ++sandbox;
1803 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 retval = 0;
1807 else
1808 {
1809 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 if (tv.v_type == VAR_NUMBER)
1811 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001812 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 retval = 0;
1814 else
1815 {
1816 /* If the result is a string, check if there is a non-digit before
1817 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if (!VIM_ISDIGIT(*s) && *s != '-')
1820 *cp = *s++;
1821 retval = atol((char *)s);
1822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001826 if (use_sandbox)
1827 --sandbox;
1828 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830 return retval;
1831}
1832#endif
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 * ":let" list all variable values
1836 * ":let var1 var2" list variable values
1837 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001838 * ":let var += expr" assignment command.
1839 * ":let var -= expr" assignment command.
1840 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 void
1844ex_let(eap)
1845 exarg_T *eap;
1846{
1847 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 int var_count = 0;
1852 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001853 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001854 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaardb552d602006-03-23 22:59:57 +00001857 argend = skip_var_list(arg, &var_count, &semicolon);
1858 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001860 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1861 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001862 expr = skipwhite(argend);
1863 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1864 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001866 /*
1867 * ":let" without "=": list variables
1868 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869 if (*arg == '[')
1870 EMSG(_(e_invarg));
1871 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001873 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001877 list_glob_vars(&first);
1878 list_buf_vars(&first);
1879 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001880#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001881 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_script_vars(&first);
1884 list_func_vars(&first);
1885 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 eap->nextcmd = check_nextcmd(arg);
1888 }
1889 else
1890 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001891 op[0] = '=';
1892 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001893 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1896 op[0] = *expr; /* +=, -= or .= */
1897 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001899 else
1900 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (eap->skip)
1903 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (eap->skip)
1906 {
1907 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 --emsg_skip;
1910 }
1911 else if (i != FAIL)
1912 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 }
1918}
1919
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920/*
1921 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1922 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 * When "nextchars" is not NULL it points to a string with characters that
1924 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1925 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 * Returns OK or FAIL;
1927 */
1928 static int
1929ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1930 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int copy; /* copy values from "tv", don't move */
1933 int semicolon; /* from skip_var_list() */
1934 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936{
1937 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001938 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 listitem_T *item;
1941 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942
1943 if (*arg != '[')
1944 {
1945 /*
1946 * ":let var = expr" or ":for var in list"
1947 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 return FAIL;
1950 return OK;
1951 }
1952
1953 /*
1954 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1955 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001956 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 {
1958 EMSG(_(e_listreq));
1959 return FAIL;
1960 }
1961
1962 i = list_len(l);
1963 if (semicolon == 0 && var_count < i)
1964 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001965 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 return FAIL;
1967 }
1968 if (var_count - semicolon > i)
1969 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001970 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 return FAIL;
1972 }
1973
1974 item = l->lv_first;
1975 while (*arg != ']')
1976 {
1977 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 item = item->li_next;
1980 if (arg == NULL)
1981 return FAIL;
1982
1983 arg = skipwhite(arg);
1984 if (*arg == ';')
1985 {
1986 /* Put the rest of the list (may be empty) in the var after ';'.
1987 * Create a new list for this. */
1988 l = list_alloc();
1989 if (l == NULL)
1990 return FAIL;
1991 while (item != NULL)
1992 {
1993 list_append_tv(l, &item->li_tv);
1994 item = item->li_next;
1995 }
1996
1997 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001998 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 ltv.vval.v_list = l;
2000 l->lv_refcount = 1;
2001
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2003 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 clear_tv(&ltv);
2005 if (arg == NULL)
2006 return FAIL;
2007 break;
2008 }
2009 else if (*arg != ',' && *arg != ']')
2010 {
2011 EMSG2(_(e_intern2), "ex_let_vars()");
2012 return FAIL;
2013 }
2014 }
2015
2016 return OK;
2017}
2018
2019/*
2020 * Skip over assignable variable "var" or list of variables "[var, var]".
2021 * Used for ":let varvar = expr" and ":for varvar in expr".
2022 * For "[var, var]" increment "*var_count" for each variable.
2023 * for "[var, var; var]" set "semicolon".
2024 * Return NULL for an error.
2025 */
2026 static char_u *
2027skip_var_list(arg, var_count, semicolon)
2028 char_u *arg;
2029 int *var_count;
2030 int *semicolon;
2031{
2032 char_u *p, *s;
2033
2034 if (*arg == '[')
2035 {
2036 /* "[var, var]": find the matching ']'. */
2037 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002038 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2041 s = skip_var_one(p);
2042 if (s == p)
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 ++*var_count;
2048
2049 p = skipwhite(s);
2050 if (*p == ']')
2051 break;
2052 else if (*p == ';')
2053 {
2054 if (*semicolon == 1)
2055 {
2056 EMSG(_("Double ; in list of variables"));
2057 return NULL;
2058 }
2059 *semicolon = 1;
2060 }
2061 else if (*p != ',')
2062 {
2063 EMSG2(_(e_invarg2), p);
2064 return NULL;
2065 }
2066 }
2067 return p + 1;
2068 }
2069 else
2070 return skip_var_one(arg);
2071}
2072
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002073/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002074 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002075 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 static char_u *
2078skip_var_one(arg)
2079 char_u *arg;
2080{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002081 if (*arg == '@' && arg[1] != NUL)
2082 return arg + 2;
2083 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2084 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085}
2086
Bram Moolenaara7043832005-01-21 11:56:39 +00002087/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 * List variables for hashtab "ht" with prefix "prefix".
2089 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002093 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002094 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002096 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002097{
Bram Moolenaar33570922005-01-25 22:26:29 +00002098 hashitem_T *hi;
2099 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002100 int todo;
2101
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002102 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2104 {
2105 if (!HASHITEM_EMPTY(hi))
2106 {
2107 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002108 di = HI2DI(hi);
2109 if (empty || di->di_tv.v_type != VAR_STRING
2110 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 }
2113 }
2114}
2115
2116/*
2117 * List global variables.
2118 */
2119 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120list_glob_vars(first)
2121 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002122{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002124}
2125
2126/*
2127 * List buffer variables.
2128 */
2129 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130list_buf_vars(first)
2131 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002132{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002133 char_u numbuf[NUMBUFLEN];
2134
Bram Moolenaar429fa852013-04-15 12:27:36 +02002135 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002137
2138 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2140 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
2143/*
2144 * List window variables.
2145 */
2146 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002147list_win_vars(first)
2148 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002150 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002152}
2153
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154#ifdef FEAT_WINDOWS
2155/*
2156 * List tab page variables.
2157 */
2158 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002159list_tab_vars(first)
2160 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002161{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002162 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164}
2165#endif
2166
Bram Moolenaara7043832005-01-21 11:56:39 +00002167/*
2168 * List Vim variables.
2169 */
2170 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171list_vim_vars(first)
2172 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175}
2176
2177/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178 * List script-local variables, if there is a script.
2179 */
2180 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181list_script_vars(first)
2182 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002183{
2184 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2186 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187}
2188
2189/*
2190 * List function variables, if there is a function.
2191 */
2192 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193list_func_vars(first)
2194 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002195{
2196 if (current_funccal != NULL)
2197 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002199}
2200
2201/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 * List variables in "arg".
2203 */
2204 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206 exarg_T *eap;
2207 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209{
2210 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 char_u *name_start;
2214 char_u *arg_subsc;
2215 char_u *tofree;
2216 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217
2218 while (!ends_excmd(*arg) && !got_int)
2219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002222 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2224 {
2225 emsg_severe = TRUE;
2226 EMSG(_(e_trailing));
2227 break;
2228 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 /* get_name_len() takes care of expanding curly braces */
2233 name_start = name = arg;
2234 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2235 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002237 /* This is mainly to keep test 49 working: when expanding
2238 * curly braces fails overrule the exception error message. */
2239 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 emsg_severe = TRUE;
2242 EMSG2(_(e_invarg2), arg);
2243 break;
2244 }
2245 error = TRUE;
2246 }
2247 else
2248 {
2249 if (tofree != NULL)
2250 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002251 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 else
2254 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 /* handle d.key, l[idx], f(expr) */
2256 arg_subsc = arg;
2257 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002265 case 'g': list_glob_vars(first); break;
2266 case 'b': list_buf_vars(first); break;
2267 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002268#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 'v': list_vim_vars(first); break;
2272 case 's': list_script_vars(first); break;
2273 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 default:
2275 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
2278 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 {
2280 char_u numbuf[NUMBUFLEN];
2281 char_u *tf;
2282 int c;
2283 char_u *s;
2284
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002285 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 c = *arg;
2287 *arg = NUL;
2288 list_one_var_a((char_u *)"",
2289 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002290 tv.v_type,
2291 s == NULL ? (char_u *)"" : s,
2292 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 *arg = c;
2294 vim_free(tf);
2295 }
2296 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300
2301 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303
2304 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
2306
2307 return arg;
2308}
2309
2310/*
2311 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2312 * Returns a pointer to the char just after the var name.
2313 * Returns NULL if there is an error.
2314 */
2315 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002318 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002320 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002321 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322{
2323 int c1;
2324 char_u *name;
2325 char_u *p;
2326 char_u *arg_end = NULL;
2327 int len;
2328 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002329 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330
2331 /*
2332 * ":let $VAR = expr": Set environment variable.
2333 */
2334 if (*arg == '$')
2335 {
2336 /* Find the end of the name. */
2337 ++arg;
2338 name = arg;
2339 len = get_env_len(&arg);
2340 if (len == 0)
2341 EMSG2(_(e_invarg2), name - 1);
2342 else
2343 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 if (op != NULL && (*op == '+' || *op == '-'))
2345 EMSG2(_(e_letwrong), op);
2346 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002347 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002349 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 {
2351 c1 = name[len];
2352 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 p = get_tv_string_chk(tv);
2354 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 {
2356 int mustfree = FALSE;
2357 char_u *s = vim_getenv(name, &mustfree);
2358
2359 if (s != NULL)
2360 {
2361 p = tofree = concat_str(s, p);
2362 if (mustfree)
2363 vim_free(s);
2364 }
2365 }
2366 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002367 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 if (STRICMP(name, "HOME") == 0)
2370 init_homedir();
2371 else if (didset_vim && STRICMP(name, "VIM") == 0)
2372 didset_vim = FALSE;
2373 else if (didset_vimruntime
2374 && STRICMP(name, "VIMRUNTIME") == 0)
2375 didset_vimruntime = FALSE;
2376 arg_end = arg;
2377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 }
2381 }
2382 }
2383
2384 /*
2385 * ":let &option = expr": Set option value.
2386 * ":let &l:option = expr": Set local option value.
2387 * ":let &g:option = expr": Set global option value.
2388 */
2389 else if (*arg == '&')
2390 {
2391 /* Find the end of the name. */
2392 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002393 if (p == NULL || (endchars != NULL
2394 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 EMSG(_(e_letunexp));
2396 else
2397 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 long n;
2399 int opt_type;
2400 long numval;
2401 char_u *stringval = NULL;
2402 char_u *s;
2403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 c1 = *p;
2405 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406
2407 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 s = get_tv_string_chk(tv); /* != NULL if number or string */
2409 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 {
2411 opt_type = get_option_value(arg, &numval,
2412 &stringval, opt_flags);
2413 if ((opt_type == 1 && *op == '.')
2414 || (opt_type == 0 && *op != '.'))
2415 EMSG2(_(e_letwrong), op);
2416 else
2417 {
2418 if (opt_type == 1) /* number */
2419 {
2420 if (*op == '+')
2421 n = numval + n;
2422 else
2423 n = numval - n;
2424 }
2425 else if (opt_type == 0 && stringval != NULL) /* string */
2426 {
2427 s = concat_str(stringval, s);
2428 vim_free(stringval);
2429 stringval = s;
2430 }
2431 }
2432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002433 if (s != NULL)
2434 {
2435 set_option_value(arg, n, s, opt_flags);
2436 arg_end = p;
2437 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 }
2441 }
2442
2443 /*
2444 * ":let @r = expr": Set register contents.
2445 */
2446 else if (*arg == '@')
2447 {
2448 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (op != NULL && (*op == '+' || *op == '-'))
2450 EMSG2(_(e_letwrong), op);
2451 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002452 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 EMSG(_(e_letunexp));
2454 else
2455 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002456 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 char_u *s;
2458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 p = get_tv_string_chk(tv);
2460 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002462 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 if (s != NULL)
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 vim_free(s);
2467 }
2468 }
2469 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 arg_end = arg + 1;
2473 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
2476 }
2477
2478 /*
2479 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002482 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002484 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002486 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2490 EMSG(_(e_letunexp));
2491 else
2492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 arg_end = p;
2495 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
2499
2500 else
2501 EMSG2(_(e_invarg2), arg);
2502
2503 return arg_end;
2504}
2505
2506/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2508 */
2509 static int
2510check_changedtick(arg)
2511 char_u *arg;
2512{
2513 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2514 {
2515 EMSG2(_(e_readonlyvar), arg);
2516 return TRUE;
2517 }
2518 return FALSE;
2519}
2520
2521/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * Get an lval: variable, Dict item or List item that can be assigned a value
2523 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2524 * "name.key", "name.key[expr]" etc.
2525 * Indexing only works if "name" is an existing List or Dictionary.
2526 * "name" points to the start of the name.
2527 * If "rettv" is not NULL it points to the value to be assigned.
2528 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2529 * wrong; must end in space or cmd separator.
2530 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002531 * flags:
2532 * GLV_QUIET: do not give error messages
2533 * GLV_NO_AUTOLOAD: do not use script autoloading
2534 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002536 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 */
2539 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 typval_T *rettv;
2543 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 int unlet;
2545 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002546 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002547 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 char_u *expr_start, *expr_end;
2551 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 dictitem_T *v;
2553 typval_T var1;
2554 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002560 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002563 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564
2565 if (skip)
2566 {
2567 /* When skipping just find the end of the name. */
2568 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002569 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 }
2571
2572 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002573 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 if (expr_start != NULL)
2575 {
2576 /* Don't expand the name when we already know there is an error. */
2577 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2578 && *p != '[' && *p != '.')
2579 {
2580 EMSG(_(e_trailing));
2581 return NULL;
2582 }
2583
2584 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2585 if (lp->ll_exp_name == NULL)
2586 {
2587 /* Report an invalid expression in braces, unless the
2588 * expression evaluation has been cancelled due to an
2589 * aborting error, an interrupt, or an exception. */
2590 if (!aborting() && !quiet)
2591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002592 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 EMSG2(_(e_invarg2), name);
2594 return NULL;
2595 }
2596 }
2597 lp->ll_name = lp->ll_exp_name;
2598 }
2599 else
2600 lp->ll_name = name;
2601
2602 /* Without [idx] or .key we are done. */
2603 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2604 return p;
2605
2606 cc = *p;
2607 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002608 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (v == NULL && !quiet)
2610 EMSG2(_(e_undefvar), lp->ll_name);
2611 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 if (v == NULL)
2613 return NULL;
2614
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 /*
2616 * Loop until no more [idx] or .key is following.
2617 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002618 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2622 && !(lp->ll_tv->v_type == VAR_DICT
2623 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_("E689: Can only index a List or Dictionary"));
2627 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (!quiet)
2632 EMSG(_("E708: [:] must come last"));
2633 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 len = -1;
2637 if (*p == '.')
2638 {
2639 key = p + 1;
2640 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2641 ;
2642 if (len == 0)
2643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (!quiet)
2645 EMSG(_(e_emptykey));
2646 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 }
2648 p = key + len;
2649 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 else
2651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 if (*p == ':')
2655 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 else
2657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 empty1 = FALSE;
2659 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002661 if (get_tv_string_chk(&var1) == NULL)
2662 {
2663 /* not a number or string */
2664 clear_tv(&var1);
2665 return NULL;
2666 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 }
2668
2669 /* Optionally get the second index [ :expr]. */
2670 if (*p == ':')
2671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002675 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676 if (!empty1)
2677 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (rettv != NULL && (rettv->v_type != VAR_LIST
2681 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
2684 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 p = skipwhite(p + 1);
2690 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 else
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (!empty1)
2698 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002701 if (get_tv_string_chk(&var2) == NULL)
2702 {
2703 /* not a number or string */
2704 if (!empty1)
2705 clear_tv(&var1);
2706 clear_tv(&var2);
2707 return NULL;
2708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725
2726 /* Skip to past ']'. */
2727 ++p;
2728 }
2729
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 {
2732 if (len == -1)
2733 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (*key == NUL)
2737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
2743 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002744 lp->ll_list = NULL;
2745 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002746 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002747
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002748 /* When assigning to a scope dictionary check that a function and
2749 * variable name is valid (only variable name unless it is l: or
2750 * g: dictionary). Disallow overwriting a builtin function. */
2751 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002752 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002753 int prevval;
2754 int wrong;
2755
2756 if (len != -1)
2757 {
2758 prevval = key[len];
2759 key[len] = NUL;
2760 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002761 else
2762 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002763 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2764 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002765 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002766 || !valid_varname(key);
2767 if (len != -1)
2768 key[len] = prevval;
2769 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002770 return NULL;
2771 }
2772
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775 /* Can't add "v:" variable. */
2776 if (lp->ll_dict == &vimvardict)
2777 {
2778 EMSG2(_(e_illvar), name);
2779 return NULL;
2780 }
2781
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002786 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 if (len == -1)
2796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 p = NULL;
2799 break;
2800 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 /* existing variable, need to check if it can be changed */
2802 else if (var_check_ro(lp->ll_di->di_flags, name))
2803 return NULL;
2804
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 if (len == -1)
2806 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 }
2809 else
2810 {
2811 /*
2812 * Get the number and item for the only or first index of the List.
2813 */
2814 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 else
2817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002818 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 clear_tv(&var1);
2820 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_list = lp->ll_tv->vval.v_list;
2823 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2824 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002826 if (lp->ll_n1 < 0)
2827 {
2828 lp->ll_n1 = 0;
2829 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2830 }
2831 }
2832 if (lp->ll_li == NULL)
2833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 if (!quiet)
2837 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 }
2840
2841 /*
2842 * May need to find the item or absolute index for the second
2843 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 * When no index given: "lp->ll_empty2" is TRUE.
2845 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002849 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002855 {
2856 if (!quiet)
2857 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2864 if (lp->ll_n1 < 0)
2865 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2866 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002867 {
2868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002872 }
2873
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002876 }
2877
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 return p;
2879}
2880
2881/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 */
2884 static void
2885clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887{
2888 vim_free(lp->ll_exp_name);
2889 vim_free(lp->ll_newkey);
2890}
2891
2892/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002893 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 */
2897 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904{
2905 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002906 listitem_T *ri;
2907 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908
2909 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 cc = *endp;
2914 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 if (op != NULL && *op != '=')
2916 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002920 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002921 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002922 {
2923 if (tv_op(&tv, rettv, op) == OK)
2924 set_var(lp->ll_name, &tv, FALSE);
2925 clear_tv(&tv);
2926 }
2927 }
2928 else
2929 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 else if (tv_check_lock(lp->ll_newkey == NULL
2934 ? lp->ll_tv->v_lock
2935 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2936 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 else if (lp->ll_range)
2938 {
2939 /*
2940 * Assign the List values to the list items.
2941 */
2942 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 if (op != NULL && *op != '=')
2945 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2946 else
2947 {
2948 clear_tv(&lp->ll_li->li_tv);
2949 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2950 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 ri = ri->li_next;
2952 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2953 break;
2954 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002957 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 ri = NULL;
2960 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 lp->ll_li = lp->ll_li->li_next;
2964 ++lp->ll_n1;
2965 }
2966 if (ri != NULL)
2967 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 else if (lp->ll_empty2
2969 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 : lp->ll_n1 != lp->ll_n2)
2971 EMSG(_("E711: List value has not enough items"));
2972 }
2973 else
2974 {
2975 /*
2976 * Assign to a List or Dictionary item.
2977 */
2978 if (lp->ll_newkey != NULL)
2979 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002980 if (op != NULL && *op != '=')
2981 {
2982 EMSG2(_(e_letwrong), op);
2983 return;
2984 }
2985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002987 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 if (di == NULL)
2989 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002990 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2991 {
2992 vim_free(di);
2993 return;
2994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 else if (op != NULL && *op != '=')
2998 {
2999 tv_op(lp->ll_tv, rettv, op);
3000 return;
3001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003004
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 /*
3006 * Assign the value to the variable or list item.
3007 */
3008 if (copy)
3009 copy_tv(rettv, lp->ll_tv);
3010 else
3011 {
3012 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003013 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015 }
3016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017}
3018
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003019/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3021 * Returns OK or FAIL.
3022 */
3023 static int
3024tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003025 typval_T *tv1;
3026 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 char_u *op;
3028{
3029 long n;
3030 char_u numbuf[NUMBUFLEN];
3031 char_u *s;
3032
3033 /* Can't do anything with a Funcref or a Dict on the right. */
3034 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3035 {
3036 switch (tv1->v_type)
3037 {
3038 case VAR_DICT:
3039 case VAR_FUNC:
3040 break;
3041
3042 case VAR_LIST:
3043 if (*op != '+' || tv2->v_type != VAR_LIST)
3044 break;
3045 /* List += List */
3046 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3047 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3048 return OK;
3049
3050 case VAR_NUMBER:
3051 case VAR_STRING:
3052 if (tv2->v_type == VAR_LIST)
3053 break;
3054 if (*op == '+' || *op == '-')
3055 {
3056 /* nr += nr or nr -= nr*/
3057 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#ifdef FEAT_FLOAT
3059 if (tv2->v_type == VAR_FLOAT)
3060 {
3061 float_T f = n;
3062
3063 if (*op == '+')
3064 f += tv2->vval.v_float;
3065 else
3066 f -= tv2->vval.v_float;
3067 clear_tv(tv1);
3068 tv1->v_type = VAR_FLOAT;
3069 tv1->vval.v_float = f;
3070 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003072#endif
3073 {
3074 if (*op == '+')
3075 n += get_tv_number(tv2);
3076 else
3077 n -= get_tv_number(tv2);
3078 clear_tv(tv1);
3079 tv1->v_type = VAR_NUMBER;
3080 tv1->vval.v_number = n;
3081 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 }
3083 else
3084 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003085 if (tv2->v_type == VAR_FLOAT)
3086 break;
3087
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 /* str .= str */
3089 s = get_tv_string(tv1);
3090 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3091 clear_tv(tv1);
3092 tv1->v_type = VAR_STRING;
3093 tv1->vval.v_string = s;
3094 }
3095 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003096
3097#ifdef FEAT_FLOAT
3098 case VAR_FLOAT:
3099 {
3100 float_T f;
3101
3102 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3103 && tv2->v_type != VAR_NUMBER
3104 && tv2->v_type != VAR_STRING))
3105 break;
3106 if (tv2->v_type == VAR_FLOAT)
3107 f = tv2->vval.v_float;
3108 else
3109 f = get_tv_number(tv2);
3110 if (*op == '+')
3111 tv1->vval.v_float += f;
3112 else
3113 tv1->vval.v_float -= f;
3114 }
3115 return OK;
3116#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 }
3118 }
3119
3120 EMSG2(_(e_letwrong), op);
3121 return FAIL;
3122}
3123
3124/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125 * Add a watcher to a list.
3126 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003127 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 list_T *l;
3130 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131{
3132 lw->lw_next = l->lv_watch;
3133 l->lv_watch = lw;
3134}
3135
3136/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003137 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003138 * No warning when it isn't found...
3139 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003140 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 list_T *l;
3143 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144{
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146
3147 lwp = &l->lv_watch;
3148 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3149 {
3150 if (lw == lwrem)
3151 {
3152 *lwp = lw->lw_next;
3153 break;
3154 }
3155 lwp = &lw->lw_next;
3156 }
3157}
3158
3159/*
3160 * Just before removing an item from a list: advance watchers to the next
3161 * item.
3162 */
3163 static void
3164list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003165 list_T *l;
3166 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167{
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169
3170 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3171 if (lw->lw_item == item)
3172 lw->lw_item = item->li_next;
3173}
3174
3175/*
3176 * Evaluate the expression used in a ":for var in expr" command.
3177 * "arg" points to "var".
3178 * Set "*errp" to TRUE for an error, FALSE otherwise;
3179 * Return a pointer that holds the info. Null when there is an error.
3180 */
3181 void *
3182eval_for_line(arg, errp, nextcmdp, skip)
3183 char_u *arg;
3184 int *errp;
3185 char_u **nextcmdp;
3186 int skip;
3187{
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 typval_T tv;
3191 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192
3193 *errp = TRUE; /* default: there is an error */
3194
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196 if (fi == NULL)
3197 return NULL;
3198
3199 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3200 if (expr == NULL)
3201 return fi;
3202
3203 expr = skipwhite(expr);
3204 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3205 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003206 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 return fi;
3208 }
3209
3210 if (skip)
3211 ++emsg_skip;
3212 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3213 {
3214 *errp = FALSE;
3215 if (!skip)
3216 {
3217 l = tv.vval.v_list;
3218 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003219 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 clear_tv(&tv);
3222 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 else
3224 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003225 /* No need to increment the refcount, it's already set for the
3226 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 fi->fi_list = l;
3228 list_add_watch(l, &fi->fi_lw);
3229 fi->fi_lw.lw_item = l->lv_first;
3230 }
3231 }
3232 }
3233 if (skip)
3234 --emsg_skip;
3235
3236 return fi;
3237}
3238
3239/*
3240 * Use the first item in a ":for" list. Advance to the next.
3241 * Assign the values to the variable (list). "arg" points to the first one.
3242 * Return TRUE when a valid item was found, FALSE when at end of list or
3243 * something wrong.
3244 */
3245 int
3246next_for_item(fi_void, arg)
3247 void *fi_void;
3248 char_u *arg;
3249{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003250 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253
3254 item = fi->fi_lw.lw_item;
3255 if (item == NULL)
3256 result = FALSE;
3257 else
3258 {
3259 fi->fi_lw.lw_item = item->li_next;
3260 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3261 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3262 }
3263 return result;
3264}
3265
3266/*
3267 * Free the structure used to store info used by ":for".
3268 */
3269 void
3270free_for_info(fi_void)
3271 void *fi_void;
3272{
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003275 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 list_unref(fi->fi_list);
3279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 vim_free(fi);
3281}
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3284
3285 void
3286set_context_for_expression(xp, arg, cmdidx)
3287 expand_T *xp;
3288 char_u *arg;
3289 cmdidx_T cmdidx;
3290{
3291 int got_eq = FALSE;
3292 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 if (cmdidx == CMD_let)
3296 {
3297 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003298 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 {
3300 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003301 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 {
3303 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003304 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 if (vim_iswhite(*p))
3306 break;
3307 }
3308 return;
3309 }
3310 }
3311 else
3312 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3313 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 while ((xp->xp_pattern = vim_strpbrk(arg,
3315 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3316 {
3317 c = *xp->xp_pattern;
3318 if (c == '&')
3319 {
3320 c = xp->xp_pattern[1];
3321 if (c == '&')
3322 {
3323 ++xp->xp_pattern;
3324 xp->xp_context = cmdidx != CMD_let || got_eq
3325 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3326 }
3327 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3331 xp->xp_pattern += 2;
3332
3333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335 else if (c == '$')
3336 {
3337 /* environment variable */
3338 xp->xp_context = EXPAND_ENV_VARS;
3339 }
3340 else if (c == '=')
3341 {
3342 got_eq = TRUE;
3343 xp->xp_context = EXPAND_EXPRESSION;
3344 }
3345 else if (c == '<'
3346 && xp->xp_context == EXPAND_FUNCTIONS
3347 && vim_strchr(xp->xp_pattern, '(') == NULL)
3348 {
3349 /* Function name can start with "<SNR>" */
3350 break;
3351 }
3352 else if (cmdidx != CMD_let || got_eq)
3353 {
3354 if (c == '"') /* string */
3355 {
3356 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3357 if (c == '\\' && xp->xp_pattern[1] != NUL)
3358 ++xp->xp_pattern;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '\'') /* literal string */
3362 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003363 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3365 /* skip */ ;
3366 xp->xp_context = EXPAND_NOTHING;
3367 }
3368 else if (c == '|')
3369 {
3370 if (xp->xp_pattern[1] == '|')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = EXPAND_EXPRESSION;
3374 }
3375 else
3376 xp->xp_context = EXPAND_COMMANDS;
3377 }
3378 else
3379 xp->xp_context = EXPAND_EXPRESSION;
3380 }
3381 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003382 /* Doesn't look like something valid, expand as an expression
3383 * anyway. */
3384 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 arg = xp->xp_pattern;
3386 if (*arg != NUL)
3387 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3388 /* skip */ ;
3389 }
3390 xp->xp_pattern = arg;
3391}
3392
3393#endif /* FEAT_CMDL_COMPL */
3394
3395/*
3396 * ":1,25call func(arg1, arg2)" function call.
3397 */
3398 void
3399ex_call(eap)
3400 exarg_T *eap;
3401{
3402 char_u *arg = eap->arg;
3403 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003405 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 linenr_T lnum;
3409 int doesrange;
3410 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003413 if (eap->skip)
3414 {
3415 /* trans_function_name() doesn't work well when skipping, use eval0()
3416 * instead to skip to any following command, e.g. for:
3417 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003418 ++emsg_skip;
3419 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3420 clear_tv(&rettv);
3421 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003422 return;
3423 }
3424
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003425 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003426 if (fudi.fd_newkey != NULL)
3427 {
3428 /* Still need to give an error message for missing key. */
3429 EMSG2(_(e_dictkey), fudi.fd_newkey);
3430 vim_free(fudi.fd_newkey);
3431 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003432 if (tofree == NULL)
3433 return;
3434
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003435 /* Increase refcount on dictionary, it could get deleted when evaluating
3436 * the arguments. */
3437 if (fudi.fd_dict != NULL)
3438 ++fudi.fd_dict->dv_refcount;
3439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003440 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003441 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003442 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar532c7802005-01-27 14:44:31 +00003444 /* Skip white space to allow ":call func ()". Not good, but required for
3445 * backward compatibility. */
3446 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003447 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 if (*startarg != '(')
3450 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003451 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 goto end;
3453 }
3454
3455 /*
3456 * When skipping, evaluate the function once, to find the end of the
3457 * arguments.
3458 * When the function takes a range, this is discovered after the first
3459 * call, and the loop is broken.
3460 */
3461 if (eap->skip)
3462 {
3463 ++emsg_skip;
3464 lnum = eap->line2; /* do it once, also with an invalid range */
3465 }
3466 else
3467 lnum = eap->line1;
3468 for ( ; lnum <= eap->line2; ++lnum)
3469 {
3470 if (!eap->skip && eap->addr_count > 0)
3471 {
3472 curwin->w_cursor.lnum = lnum;
3473 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003474#ifdef FEAT_VIRTUALEDIT
3475 curwin->w_cursor.coladd = 0;
3476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
3478 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003479 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 eap->line1, eap->line2, &doesrange,
3481 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 failed = TRUE;
3484 break;
3485 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003486
3487 /* Handle a function returning a Funcref, Dictionary or List. */
3488 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3489 {
3490 failed = TRUE;
3491 break;
3492 }
3493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (doesrange || eap->skip)
3496 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003499 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (aborting())
3503 break;
3504 }
3505 if (eap->skip)
3506 --emsg_skip;
3507
3508 if (!failed)
3509 {
3510 /* Check for trailing illegal characters and a following command. */
3511 if (!ends_excmd(*arg))
3512 {
3513 emsg_severe = TRUE;
3514 EMSG(_(e_trailing));
3515 }
3516 else
3517 eap->nextcmd = check_nextcmd(arg);
3518 }
3519
3520end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003521 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003522 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
3525/*
3526 * ":unlet[!] var1 ... " command.
3527 */
3528 void
3529ex_unlet(eap)
3530 exarg_T *eap;
3531{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003532 ex_unletlock(eap, eap->arg, 0);
3533}
3534
3535/*
3536 * ":lockvar" and ":unlockvar" commands
3537 */
3538 void
3539ex_lockvar(eap)
3540 exarg_T *eap;
3541{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 int deep = 2;
3544
3545 if (eap->forceit)
3546 deep = -1;
3547 else if (vim_isdigit(*arg))
3548 {
3549 deep = getdigits(&arg);
3550 arg = skipwhite(arg);
3551 }
3552
3553 ex_unletlock(eap, arg, deep);
3554}
3555
3556/*
3557 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3558 */
3559 static void
3560ex_unletlock(eap, argstart, deep)
3561 exarg_T *eap;
3562 char_u *argstart;
3563 int deep;
3564{
3565 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003568 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569
3570 do
3571 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003572 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003573 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003574 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 if (lv.ll_name == NULL)
3576 error = TRUE; /* error but continue parsing */
3577 if (name_end == NULL || (!vim_iswhite(*name_end)
3578 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003580 if (name_end != NULL)
3581 {
3582 emsg_severe = TRUE;
3583 EMSG(_(e_trailing));
3584 }
3585 if (!(eap->skip || error))
3586 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588 }
3589
3590 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003591 {
3592 if (eap->cmdidx == CMD_unlet)
3593 {
3594 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3595 error = TRUE;
3596 }
3597 else
3598 {
3599 if (do_lock_var(&lv, name_end, deep,
3600 eap->cmdidx == CMD_lockvar) == FAIL)
3601 error = TRUE;
3602 }
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 if (!eap->skip)
3606 clear_lval(&lv);
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 arg = skipwhite(name_end);
3609 } while (!ends_excmd(*arg));
3610
3611 eap->nextcmd = check_nextcmd(arg);
3612}
3613
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003616 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003618 int forceit;
3619{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 int ret = OK;
3621 int cc;
3622
3623 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 cc = *name_end;
3626 *name_end = NUL;
3627
3628 /* Normal name or expanded name. */
3629 if (check_changedtick(lp->ll_name))
3630 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3636 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 else if (lp->ll_range)
3638 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003639 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640
3641 /* Delete a range of List items. */
3642 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3643 {
3644 li = lp->ll_li->li_next;
3645 listitem_remove(lp->ll_list, lp->ll_li);
3646 lp->ll_li = li;
3647 ++lp->ll_n1;
3648 }
3649 }
3650 else
3651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 /* unlet a List item. */
3654 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003657 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 }
3659
3660 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661}
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663/*
3664 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
Bram Moolenaar33570922005-01-25 22:26:29 +00003672 hashtab_T *ht;
3673 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003674 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003675 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar33570922005-01-25 22:26:29 +00003677 ht = find_var_ht(name, &varname);
3678 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 hi = hash_find(ht, varname);
3681 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003683 di = HI2DI(hi);
3684 if (var_check_fixed(di->di_flags, name)
3685 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
3687 delete_var(ht, hi);
3688 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 if (forceit)
3692 return OK;
3693 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return FAIL;
3695}
3696
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003697/*
3698 * Lock or unlock variable indicated by "lp".
3699 * "deep" is the levels to go (-1 for unlimited);
3700 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3701 */
3702 static int
3703do_lock_var(lp, name_end, deep, lock)
3704 lval_T *lp;
3705 char_u *name_end;
3706 int deep;
3707 int lock;
3708{
3709 int ret = OK;
3710 int cc;
3711 dictitem_T *di;
3712
3713 if (deep == 0) /* nothing to do */
3714 return OK;
3715
3716 if (lp->ll_tv == NULL)
3717 {
3718 cc = *name_end;
3719 *name_end = NUL;
3720
3721 /* Normal name or expanded name. */
3722 if (check_changedtick(lp->ll_name))
3723 ret = FAIL;
3724 else
3725 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003726 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 if (di == NULL)
3728 ret = FAIL;
3729 else
3730 {
3731 if (lock)
3732 di->di_flags |= DI_FLAGS_LOCK;
3733 else
3734 di->di_flags &= ~DI_FLAGS_LOCK;
3735 item_lock(&di->di_tv, deep, lock);
3736 }
3737 }
3738 *name_end = cc;
3739 }
3740 else if (lp->ll_range)
3741 {
3742 listitem_T *li = lp->ll_li;
3743
3744 /* (un)lock a range of List items. */
3745 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3746 {
3747 item_lock(&li->li_tv, deep, lock);
3748 li = li->li_next;
3749 ++lp->ll_n1;
3750 }
3751 }
3752 else if (lp->ll_list != NULL)
3753 /* (un)lock a List item. */
3754 item_lock(&lp->ll_li->li_tv, deep, lock);
3755 else
3756 /* un(lock) a Dictionary item. */
3757 item_lock(&lp->ll_di->di_tv, deep, lock);
3758
3759 return ret;
3760}
3761
3762/*
3763 * Lock or unlock an item. "deep" is nr of levels to go.
3764 */
3765 static void
3766item_lock(tv, deep, lock)
3767 typval_T *tv;
3768 int deep;
3769 int lock;
3770{
3771 static int recurse = 0;
3772 list_T *l;
3773 listitem_T *li;
3774 dict_T *d;
3775 hashitem_T *hi;
3776 int todo;
3777
3778 if (recurse >= DICT_MAXNEST)
3779 {
3780 EMSG(_("E743: variable nested too deep for (un)lock"));
3781 return;
3782 }
3783 if (deep == 0)
3784 return;
3785 ++recurse;
3786
3787 /* lock/unlock the item itself */
3788 if (lock)
3789 tv->v_lock |= VAR_LOCKED;
3790 else
3791 tv->v_lock &= ~VAR_LOCKED;
3792
3793 switch (tv->v_type)
3794 {
3795 case VAR_LIST:
3796 if ((l = tv->vval.v_list) != NULL)
3797 {
3798 if (lock)
3799 l->lv_lock |= VAR_LOCKED;
3800 else
3801 l->lv_lock &= ~VAR_LOCKED;
3802 if (deep < 0 || deep > 1)
3803 /* recursive: lock/unlock the items the List contains */
3804 for (li = l->lv_first; li != NULL; li = li->li_next)
3805 item_lock(&li->li_tv, deep - 1, lock);
3806 }
3807 break;
3808 case VAR_DICT:
3809 if ((d = tv->vval.v_dict) != NULL)
3810 {
3811 if (lock)
3812 d->dv_lock |= VAR_LOCKED;
3813 else
3814 d->dv_lock &= ~VAR_LOCKED;
3815 if (deep < 0 || deep > 1)
3816 {
3817 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003818 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3820 {
3821 if (!HASHITEM_EMPTY(hi))
3822 {
3823 --todo;
3824 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3825 }
3826 }
3827 }
3828 }
3829 }
3830 --recurse;
3831}
3832
Bram Moolenaara40058a2005-07-11 22:42:07 +00003833/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003834 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3835 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003836 */
3837 static int
3838tv_islocked(tv)
3839 typval_T *tv;
3840{
3841 return (tv->v_lock & VAR_LOCKED)
3842 || (tv->v_type == VAR_LIST
3843 && tv->vval.v_list != NULL
3844 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3845 || (tv->v_type == VAR_DICT
3846 && tv->vval.v_dict != NULL
3847 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3848}
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3851/*
3852 * Delete all "menutrans_" variables.
3853 */
3854 void
3855del_menutrans_vars()
3856{
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003858 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003861 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003863 {
3864 if (!HASHITEM_EMPTY(hi))
3865 {
3866 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003867 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3868 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003869 }
3870 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872}
3873#endif
3874
3875#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3876
3877/*
3878 * Local string buffer for the next two functions to store a variable name
3879 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3880 * get_user_var_name().
3881 */
3882
3883static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3884
3885static char_u *varnamebuf = NULL;
3886static int varnamebuflen = 0;
3887
3888/*
3889 * Function to concatenate a prefix and a variable name.
3890 */
3891 static char_u *
3892cat_prefix_varname(prefix, name)
3893 int prefix;
3894 char_u *name;
3895{
3896 int len;
3897
3898 len = (int)STRLEN(name) + 3;
3899 if (len > varnamebuflen)
3900 {
3901 vim_free(varnamebuf);
3902 len += 10; /* some additional space */
3903 varnamebuf = alloc(len);
3904 if (varnamebuf == NULL)
3905 {
3906 varnamebuflen = 0;
3907 return NULL;
3908 }
3909 varnamebuflen = len;
3910 }
3911 *varnamebuf = prefix;
3912 varnamebuf[1] = ':';
3913 STRCPY(varnamebuf + 2, name);
3914 return varnamebuf;
3915}
3916
3917/*
3918 * Function given to ExpandGeneric() to obtain the list of user defined
3919 * (global/buffer/window/built-in) variable names.
3920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 char_u *
3922get_user_var_name(xp, idx)
3923 expand_T *xp;
3924 int idx;
3925{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003926 static long_u gdone;
3927 static long_u bdone;
3928 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003929#ifdef FEAT_WINDOWS
3930 static long_u tdone;
3931#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003932 static int vidx;
3933 static hashitem_T *hi;
3934 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
3936 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003937 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003938 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939#ifdef FEAT_WINDOWS
3940 tdone = 0;
3941#endif
3942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943
3944 /* Global variables */
3945 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003949 else
3950 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 while (HASHITEM_EMPTY(hi))
3952 ++hi;
3953 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3954 return cat_prefix_varname('g', hi->hi_key);
3955 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003957
3958 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003959 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003964 else
3965 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003966 while (HASHITEM_EMPTY(hi))
3967 ++hi;
3968 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 return (char_u *)"b:changedtick";
3974 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003975
3976 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003977 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003980 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003981 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003982 else
3983 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003984 while (HASHITEM_EMPTY(hi))
3985 ++hi;
3986 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003988
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003989#ifdef FEAT_WINDOWS
3990 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003991 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003992 if (tdone < ht->ht_used)
3993 {
3994 if (tdone++ == 0)
3995 hi = ht->ht_array;
3996 else
3997 ++hi;
3998 while (HASHITEM_EMPTY(hi))
3999 ++hi;
4000 return cat_prefix_varname('t', hi->hi_key);
4001 }
4002#endif
4003
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 /* v: variables */
4005 if (vidx < VV_LEN)
4006 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 vim_free(varnamebuf);
4009 varnamebuf = NULL;
4010 varnamebuflen = 0;
4011 return NULL;
4012}
4013
4014#endif /* FEAT_CMDL_COMPL */
4015
4016/*
4017 * types for expressions.
4018 */
4019typedef enum
4020{
4021 TYPE_UNKNOWN = 0
4022 , TYPE_EQUAL /* == */
4023 , TYPE_NEQUAL /* != */
4024 , TYPE_GREATER /* > */
4025 , TYPE_GEQUAL /* >= */
4026 , TYPE_SMALLER /* < */
4027 , TYPE_SEQUAL /* <= */
4028 , TYPE_MATCH /* =~ */
4029 , TYPE_NOMATCH /* !~ */
4030} exptype_T;
4031
4032/*
4033 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4036 */
4037
4038/*
4039 * Handle zero level expression.
4040 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004042 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * Return OK or FAIL.
4044 */
4045 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u **nextcmd;
4050 int evaluate;
4051{
4052 int ret;
4053 char_u *p;
4054
4055 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004056 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (ret == FAIL || !ends_excmd(*p))
4058 {
4059 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004060 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 /*
4062 * Report the invalid expression unless the expression evaluation has
4063 * been cancelled due to an aborting error, an interrupt, or an
4064 * exception.
4065 */
4066 if (!aborting())
4067 EMSG2(_(e_invexpr2), arg);
4068 ret = FAIL;
4069 }
4070 if (nextcmd != NULL)
4071 *nextcmd = check_nextcmd(p);
4072
4073 return ret;
4074}
4075
4076/*
4077 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004078 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 *
4080 * "arg" must point to the first non-white of the expression.
4081 * "arg" is advanced to the next non-white after the recognized expression.
4082 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004083 * Note: "rettv.v_lock" is not set.
4084 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 * Return OK or FAIL.
4086 */
4087 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004088eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 int evaluate;
4092{
4093 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /*
4097 * Get the first variable.
4098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004099 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 return FAIL;
4101
4102 if ((*arg)[0] == '?')
4103 {
4104 result = FALSE;
4105 if (evaluate)
4106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107 int error = FALSE;
4108
4109 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 if (error)
4113 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115
4116 /*
4117 * Get the second variable.
4118 */
4119 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 return FAIL;
4122
4123 /*
4124 * Check for the ":".
4125 */
4126 if ((*arg)[0] != ':')
4127 {
4128 EMSG(_("E109: Missing ':' after '?'"));
4129 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 return FAIL;
4132 }
4133
4134 /*
4135 * Get the third variable.
4136 */
4137 *arg = skipwhite(*arg + 1);
4138 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4139 {
4140 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 return FAIL;
4143 }
4144 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147
4148 return OK;
4149}
4150
4151/*
4152 * Handle first level expression:
4153 * expr2 || expr2 || expr2 logical OR
4154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
4158 * Return OK or FAIL.
4159 */
4160 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004161eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 int evaluate;
4165{
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 long result;
4168 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 /*
4178 * Repeat until there is no following "||".
4179 */
4180 first = TRUE;
4181 result = FALSE;
4182 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4183 {
4184 if (evaluate && first)
4185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 first = FALSE;
4192 }
4193
4194 /*
4195 * Get the second variable.
4196 */
4197 *arg = skipwhite(*arg + 2);
4198 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4199 return FAIL;
4200
4201 /*
4202 * Compute the result.
4203 */
4204 if (evaluate && !result)
4205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209 if (error)
4210 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
4212 if (evaluate)
4213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 rettv->v_type = VAR_NUMBER;
4215 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217 }
4218
4219 return OK;
4220}
4221
4222/*
4223 * Handle second level expression:
4224 * expr3 && expr3 && expr3 logical AND
4225 *
4226 * "arg" must point to the first non-white of the expression.
4227 * "arg" is advanced to the next non-white after the recognized expression.
4228 *
4229 * Return OK or FAIL.
4230 */
4231 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int evaluate;
4236{
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 long result;
4239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 /*
4249 * Repeat until there is no following "&&".
4250 */
4251 first = TRUE;
4252 result = TRUE;
4253 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4254 {
4255 if (evaluate && first)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 first = FALSE;
4263 }
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 2);
4269 if (eval4(arg, &var2, evaluate && result) == FAIL)
4270 return FAIL;
4271
4272 /*
4273 * Compute the result.
4274 */
4275 if (evaluate && result)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (evaluate)
4284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * Handle third level expression:
4295 * var1 == var2
4296 * var1 =~ var2
4297 * var1 != var2
4298 * var1 !~ var2
4299 * var1 > var2
4300 * var1 >= var2
4301 * var1 < var2
4302 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303 * var1 is var2
4304 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 *
4306 * "arg" must point to the first non-white of the expression.
4307 * "arg" is advanced to the next non-white after the recognized expression.
4308 *
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int evaluate;
4316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 char_u *p;
4319 int i;
4320 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004321 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 int len = 2;
4323 long n1, n2;
4324 char_u *s1, *s2;
4325 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4326 regmatch_T regmatch;
4327 int ic;
4328 char_u *save_cpo;
4329
4330 /*
4331 * Get the first variable.
4332 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004333 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 return FAIL;
4335
4336 p = *arg;
4337 switch (p[0])
4338 {
4339 case '=': if (p[1] == '=')
4340 type = TYPE_EQUAL;
4341 else if (p[1] == '~')
4342 type = TYPE_MATCH;
4343 break;
4344 case '!': if (p[1] == '=')
4345 type = TYPE_NEQUAL;
4346 else if (p[1] == '~')
4347 type = TYPE_NOMATCH;
4348 break;
4349 case '>': if (p[1] != '=')
4350 {
4351 type = TYPE_GREATER;
4352 len = 1;
4353 }
4354 else
4355 type = TYPE_GEQUAL;
4356 break;
4357 case '<': if (p[1] != '=')
4358 {
4359 type = TYPE_SMALLER;
4360 len = 1;
4361 }
4362 else
4363 type = TYPE_SEQUAL;
4364 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004365 case 'i': if (p[1] == 's')
4366 {
4367 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4368 len = 5;
4369 if (!vim_isIDc(p[len]))
4370 {
4371 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4372 type_is = TRUE;
4373 }
4374 }
4375 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377
4378 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004379 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 */
4381 if (type != TYPE_UNKNOWN)
4382 {
4383 /* extra question mark appended: ignore case */
4384 if (p[len] == '?')
4385 {
4386 ic = TRUE;
4387 ++len;
4388 }
4389 /* extra '#' appended: match case */
4390 else if (p[len] == '#')
4391 {
4392 ic = FALSE;
4393 ++len;
4394 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004395 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 else
4397 ic = p_ic;
4398
4399 /*
4400 * Get the second variable.
4401 */
4402 *arg = skipwhite(p + len);
4403 if (eval5(arg, &var2, evaluate) == FAIL)
4404 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 return FAIL;
4407 }
4408
4409 if (evaluate)
4410 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 if (type_is && rettv->v_type != var2.v_type)
4412 {
4413 /* For "is" a different type always means FALSE, for "notis"
4414 * it means TRUE. */
4415 n1 = (type == TYPE_NEQUAL);
4416 }
4417 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4418 {
4419 if (type_is)
4420 {
4421 n1 = (rettv->v_type == var2.v_type
4422 && rettv->vval.v_list == var2.vval.v_list);
4423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 else if (rettv->v_type != var2.v_type
4427 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4428 {
4429 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004430 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 clear_tv(rettv);
4434 clear_tv(&var2);
4435 return FAIL;
4436 }
4437 else
4438 {
4439 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004440 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4441 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 if (type == TYPE_NEQUAL)
4443 n1 = !n1;
4444 }
4445 }
4446
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004447 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4448 {
4449 if (type_is)
4450 {
4451 n1 = (rettv->v_type == var2.v_type
4452 && rettv->vval.v_dict == var2.vval.v_dict);
4453 if (type == TYPE_NEQUAL)
4454 n1 = !n1;
4455 }
4456 else if (rettv->v_type != var2.v_type
4457 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4458 {
4459 if (rettv->v_type != var2.v_type)
4460 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4461 else
4462 EMSG(_("E736: Invalid operation for Dictionary"));
4463 clear_tv(rettv);
4464 clear_tv(&var2);
4465 return FAIL;
4466 }
4467 else
4468 {
4469 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004470 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4471 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004472 if (type == TYPE_NEQUAL)
4473 n1 = !n1;
4474 }
4475 }
4476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4478 {
4479 if (rettv->v_type != var2.v_type
4480 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4481 {
4482 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004483 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004484 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 clear_tv(rettv);
4487 clear_tv(&var2);
4488 return FAIL;
4489 }
4490 else
4491 {
4492 /* Compare two Funcrefs for being equal or unequal. */
4493 if (rettv->vval.v_string == NULL
4494 || var2.vval.v_string == NULL)
4495 n1 = FALSE;
4496 else
4497 n1 = STRCMP(rettv->vval.v_string,
4498 var2.vval.v_string) == 0;
4499 if (type == TYPE_NEQUAL)
4500 n1 = !n1;
4501 }
4502 }
4503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004504#ifdef FEAT_FLOAT
4505 /*
4506 * If one of the two variables is a float, compare as a float.
4507 * When using "=~" or "!~", always compare as string.
4508 */
4509 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4510 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4511 {
4512 float_T f1, f2;
4513
4514 if (rettv->v_type == VAR_FLOAT)
4515 f1 = rettv->vval.v_float;
4516 else
4517 f1 = get_tv_number(rettv);
4518 if (var2.v_type == VAR_FLOAT)
4519 f2 = var2.vval.v_float;
4520 else
4521 f2 = get_tv_number(&var2);
4522 n1 = FALSE;
4523 switch (type)
4524 {
4525 case TYPE_EQUAL: n1 = (f1 == f2); break;
4526 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4527 case TYPE_GREATER: n1 = (f1 > f2); break;
4528 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4529 case TYPE_SMALLER: n1 = (f1 < f2); break;
4530 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4531 case TYPE_UNKNOWN:
4532 case TYPE_MATCH:
4533 case TYPE_NOMATCH: break; /* avoid gcc warning */
4534 }
4535 }
4536#endif
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 /*
4539 * If one of the two variables is a number, compare as a number.
4540 * When using "=~" or "!~", always compare as string.
4541 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004542 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004545 n1 = get_tv_number(rettv);
4546 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 switch (type)
4548 {
4549 case TYPE_EQUAL: n1 = (n1 == n2); break;
4550 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4551 case TYPE_GREATER: n1 = (n1 > n2); break;
4552 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4553 case TYPE_SMALLER: n1 = (n1 < n2); break;
4554 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4555 case TYPE_UNKNOWN:
4556 case TYPE_MATCH:
4557 case TYPE_NOMATCH: break; /* avoid gcc warning */
4558 }
4559 }
4560 else
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 s1 = get_tv_string_buf(rettv, buf1);
4563 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4565 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4566 else
4567 i = 0;
4568 n1 = FALSE;
4569 switch (type)
4570 {
4571 case TYPE_EQUAL: n1 = (i == 0); break;
4572 case TYPE_NEQUAL: n1 = (i != 0); break;
4573 case TYPE_GREATER: n1 = (i > 0); break;
4574 case TYPE_GEQUAL: n1 = (i >= 0); break;
4575 case TYPE_SMALLER: n1 = (i < 0); break;
4576 case TYPE_SEQUAL: n1 = (i <= 0); break;
4577
4578 case TYPE_MATCH:
4579 case TYPE_NOMATCH:
4580 /* avoid 'l' flag in 'cpoptions' */
4581 save_cpo = p_cpo;
4582 p_cpo = (char_u *)"";
4583 regmatch.regprog = vim_regcomp(s2,
4584 RE_MAGIC + RE_STRING);
4585 regmatch.rm_ic = ic;
4586 if (regmatch.regprog != NULL)
4587 {
4588 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004589 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (type == TYPE_NOMATCH)
4591 n1 = !n1;
4592 }
4593 p_cpo = save_cpo;
4594 break;
4595
4596 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4597 }
4598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 clear_tv(rettv);
4600 clear_tv(&var2);
4601 rettv->v_type = VAR_NUMBER;
4602 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604 }
4605
4606 return OK;
4607}
4608
4609/*
4610 * Handle fourth level expression:
4611 * + number addition
4612 * - number subtraction
4613 * . string concatenation
4614 *
4615 * "arg" must point to the first non-white of the expression.
4616 * "arg" is advanced to the next non-white after the recognized expression.
4617 *
4618 * Return OK or FAIL.
4619 */
4620 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004621eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 int evaluate;
4625{
Bram Moolenaar33570922005-01-25 22:26:29 +00004626 typval_T var2;
4627 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 int op;
4629 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630#ifdef FEAT_FLOAT
4631 float_T f1 = 0, f2 = 0;
4632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 char_u *s1, *s2;
4634 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4635 char_u *p;
4636
4637 /*
4638 * Get the first variable.
4639 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004640 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 return FAIL;
4642
4643 /*
4644 * Repeat computing, until no '+', '-' or '.' is following.
4645 */
4646 for (;;)
4647 {
4648 op = **arg;
4649 if (op != '+' && op != '-' && op != '.')
4650 break;
4651
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004652 if ((op != '+' || rettv->v_type != VAR_LIST)
4653#ifdef FEAT_FLOAT
4654 && (op == '.' || rettv->v_type != VAR_FLOAT)
4655#endif
4656 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004657 {
4658 /* For "list + ...", an illegal use of the first operand as
4659 * a number cannot be determined before evaluating the 2nd
4660 * operand: if this is also a list, all is ok.
4661 * For "something . ...", "something - ..." or "non-list + ...",
4662 * we know that the first operand needs to be a string or number
4663 * without evaluating the 2nd operand. So check before to avoid
4664 * side effects after an error. */
4665 if (evaluate && get_tv_string_chk(rettv) == NULL)
4666 {
4667 clear_tv(rettv);
4668 return FAIL;
4669 }
4670 }
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 /*
4673 * Get the second variable.
4674 */
4675 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004676 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 return FAIL;
4680 }
4681
4682 if (evaluate)
4683 {
4684 /*
4685 * Compute the result.
4686 */
4687 if (op == '.')
4688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4690 s2 = get_tv_string_buf_chk(&var2, buf2);
4691 if (s2 == NULL) /* type error ? */
4692 {
4693 clear_tv(rettv);
4694 clear_tv(&var2);
4695 return FAIL;
4696 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004697 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 clear_tv(rettv);
4699 rettv->v_type = VAR_STRING;
4700 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004702 else if (op == '+' && rettv->v_type == VAR_LIST
4703 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004704 {
4705 /* concatenate Lists */
4706 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4707 &var3) == FAIL)
4708 {
4709 clear_tv(rettv);
4710 clear_tv(&var2);
4711 return FAIL;
4712 }
4713 clear_tv(rettv);
4714 *rettv = var3;
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 else
4717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 int error = FALSE;
4719
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004720#ifdef FEAT_FLOAT
4721 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004722 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723 f1 = rettv->vval.v_float;
4724 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726 else
4727#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004728 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729 n1 = get_tv_number_chk(rettv, &error);
4730 if (error)
4731 {
4732 /* This can only happen for "list + non-list". For
4733 * "non-list + ..." or "something - ...", we returned
4734 * before evaluating the 2nd operand. */
4735 clear_tv(rettv);
4736 return FAIL;
4737 }
4738#ifdef FEAT_FLOAT
4739 if (var2.v_type == VAR_FLOAT)
4740 f1 = n1;
4741#endif
4742 }
4743#ifdef FEAT_FLOAT
4744 if (var2.v_type == VAR_FLOAT)
4745 {
4746 f2 = var2.vval.v_float;
4747 n2 = 0;
4748 }
4749 else
4750#endif
4751 {
4752 n2 = get_tv_number_chk(&var2, &error);
4753 if (error)
4754 {
4755 clear_tv(rettv);
4756 clear_tv(&var2);
4757 return FAIL;
4758 }
4759#ifdef FEAT_FLOAT
4760 if (rettv->v_type == VAR_FLOAT)
4761 f2 = n2;
4762#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004765
4766#ifdef FEAT_FLOAT
4767 /* If there is a float on either side the result is a float. */
4768 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4769 {
4770 if (op == '+')
4771 f1 = f1 + f2;
4772 else
4773 f1 = f1 - f2;
4774 rettv->v_type = VAR_FLOAT;
4775 rettv->vval.v_float = f1;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004778#endif
4779 {
4780 if (op == '+')
4781 n1 = n1 + n2;
4782 else
4783 n1 = n1 - n2;
4784 rettv->v_type = VAR_NUMBER;
4785 rettv->vval.v_number = n1;
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 }
4791 return OK;
4792}
4793
4794/*
4795 * Handle fifth level expression:
4796 * * number multiplication
4797 * / number division
4798 * % number modulo
4799 *
4800 * "arg" must point to the first non-white of the expression.
4801 * "arg" is advanced to the next non-white after the recognized expression.
4802 *
4803 * Return OK or FAIL.
4804 */
4805 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004806eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004810 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
Bram Moolenaar33570922005-01-25 22:26:29 +00004812 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 int op;
4814 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815#ifdef FEAT_FLOAT
4816 int use_float = FALSE;
4817 float_T f1 = 0, f2;
4818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 /*
4822 * Get the first variable.
4823 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004824 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return FAIL;
4826
4827 /*
4828 * Repeat computing, until no '*', '/' or '%' is following.
4829 */
4830 for (;;)
4831 {
4832 op = **arg;
4833 if (op != '*' && op != '/' && op != '%')
4834 break;
4835
4836 if (evaluate)
4837 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#ifdef FEAT_FLOAT
4839 if (rettv->v_type == VAR_FLOAT)
4840 {
4841 f1 = rettv->vval.v_float;
4842 use_float = TRUE;
4843 n1 = 0;
4844 }
4845 else
4846#endif
4847 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004849 if (error)
4850 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else
4853 n1 = 0;
4854
4855 /*
4856 * Get the second variable.
4857 */
4858 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004859 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return FAIL;
4861
4862 if (evaluate)
4863 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864#ifdef FEAT_FLOAT
4865 if (var2.v_type == VAR_FLOAT)
4866 {
4867 if (!use_float)
4868 {
4869 f1 = n1;
4870 use_float = TRUE;
4871 }
4872 f2 = var2.vval.v_float;
4873 n2 = 0;
4874 }
4875 else
4876#endif
4877 {
4878 n2 = get_tv_number_chk(&var2, &error);
4879 clear_tv(&var2);
4880 if (error)
4881 return FAIL;
4882#ifdef FEAT_FLOAT
4883 if (use_float)
4884 f2 = n2;
4885#endif
4886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004890 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892#ifdef FEAT_FLOAT
4893 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 if (op == '*')
4896 f1 = f1 * f2;
4897 else if (op == '/')
4898 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004899# ifdef VMS
4900 /* VMS crashes on divide by zero, work around it */
4901 if (f2 == 0.0)
4902 {
4903 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004904 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004905 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 }
4910 else
4911 f1 = f1 / f2;
4912# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 /* We rely on the floating point library to handle divide
4914 * by zero to result in "inf" and not a crash. */
4915 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004916# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004920 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 return FAIL;
4922 }
4923 rettv->v_type = VAR_FLOAT;
4924 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929 if (op == '*')
4930 n1 = n1 * n2;
4931 else if (op == '/')
4932 {
4933 if (n2 == 0) /* give an error message? */
4934 {
4935 if (n1 == 0)
4936 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4937 else if (n1 < 0)
4938 n1 = -0x7fffffffL;
4939 else
4940 n1 = 0x7fffffffL;
4941 }
4942 else
4943 n1 = n1 / n2;
4944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946 {
4947 if (n2 == 0) /* give an error message? */
4948 n1 = 0;
4949 else
4950 n1 = n1 % n2;
4951 }
4952 rettv->v_type = VAR_NUMBER;
4953 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 }
4957
4958 return OK;
4959}
4960
4961/*
4962 * Handle sixth level expression:
4963 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004964 * "string" string constant
4965 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 * &option-name option value
4967 * @r register contents
4968 * identifier variable value
4969 * function() function call
4970 * $VAR environment variable
4971 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004972 * [expr, expr] List
4973 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * Also handle:
4976 * ! in front logical NOT
4977 * - in front unary minus
4978 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 * trailing [] subscript in String or List
4980 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 *
4982 * "arg" must point to the first non-white of the expression.
4983 * "arg" is advanced to the next non-white after the recognized expression.
4984 *
4985 * Return OK or FAIL.
4986 */
4987 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004988eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004992 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 long n;
4995 int len;
4996 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 char_u *start_leader, *end_leader;
4998 int ret = OK;
4999 char_u *alias;
5000
5001 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006
5007 /*
5008 * Skip '!' and '-' characters. They are handled later.
5009 */
5010 start_leader = *arg;
5011 while (**arg == '!' || **arg == '-' || **arg == '+')
5012 *arg = skipwhite(*arg + 1);
5013 end_leader = *arg;
5014
5015 switch (**arg)
5016 {
5017 /*
5018 * Number constant.
5019 */
5020 case '0':
5021 case '1':
5022 case '2':
5023 case '3':
5024 case '4':
5025 case '5':
5026 case '6':
5027 case '7':
5028 case '8':
5029 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 {
5031#ifdef FEAT_FLOAT
5032 char_u *p = skipdigits(*arg + 1);
5033 int get_float = FALSE;
5034
5035 /* We accept a float when the format matches
5036 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005037 * strict to avoid backwards compatibility problems.
5038 * Don't look for a float after the "." operator, so that
5039 * ":let vers = 1.2.3" doesn't fail. */
5040 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042 get_float = TRUE;
5043 p = skipdigits(p + 2);
5044 if (*p == 'e' || *p == 'E')
5045 {
5046 ++p;
5047 if (*p == '-' || *p == '+')
5048 ++p;
5049 if (!vim_isdigit(*p))
5050 get_float = FALSE;
5051 else
5052 p = skipdigits(p + 1);
5053 }
5054 if (ASCII_ISALPHA(*p) || *p == '.')
5055 get_float = FALSE;
5056 }
5057 if (get_float)
5058 {
5059 float_T f;
5060
5061 *arg += string2float(*arg, &f);
5062 if (evaluate)
5063 {
5064 rettv->v_type = VAR_FLOAT;
5065 rettv->vval.v_float = f;
5066 }
5067 }
5068 else
5069#endif
5070 {
5071 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5072 *arg += len;
5073 if (evaluate)
5074 {
5075 rettv->v_type = VAR_NUMBER;
5076 rettv->vval.v_number = n;
5077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082 /*
5083 * String constant: "string".
5084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005085 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 break;
5093
5094 /*
5095 * List: [expr, expr]
5096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 * Dictionary: {key: val, key: val}
5102 */
5103 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5104 break;
5105
5106 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005107 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 break;
5111
5112 /*
5113 * Environment variable: $VAR.
5114 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 break;
5117
5118 /*
5119 * Register contents: @r.
5120 */
5121 case '@': ++*arg;
5122 if (evaluate)
5123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005124 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005125 rettv->vval.v_string = get_reg_contents(**arg,
5126 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 if (**arg != NUL)
5129 ++*arg;
5130 break;
5131
5132 /*
5133 * nested expression: (expression).
5134 */
5135 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (**arg == ')')
5138 ++*arg;
5139 else if (ret == OK)
5140 {
5141 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 ret = FAIL;
5144 }
5145 break;
5146
Bram Moolenaar8c711452005-01-14 21:53:12 +00005147 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 break;
5149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150
5151 if (ret == NOTDONE)
5152 {
5153 /*
5154 * Must be a variable or function name.
5155 * Can also be a curly-braces kind of name: {expr}.
5156 */
5157 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005158 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 if (alias != NULL)
5160 s = alias;
5161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005162 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 ret = FAIL;
5164 else
5165 {
5166 if (**arg == '(') /* recursive! */
5167 {
5168 /* If "s" is the name of a variable of type VAR_FUNC
5169 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005170 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171
5172 /* Invoke the function. */
5173 ret = get_func_tv(s, len, rettv, arg,
5174 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005176
5177 /* If evaluate is FALSE rettv->v_type was not set in
5178 * get_func_tv, but it's needed in handle_subscript() to parse
5179 * what follows. So set it here. */
5180 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5181 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005182 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005183 rettv->v_type = VAR_FUNC;
5184 }
5185
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 /* Stop the expression evaluation when immediately
5187 * aborting on error, or when an interrupt occurred or
5188 * an exception was thrown but not caught. */
5189 if (aborting())
5190 {
5191 if (ret == OK)
5192 clear_tv(rettv);
5193 ret = FAIL;
5194 }
5195 }
5196 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005197 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005198 else
5199 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005201 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 *arg = skipwhite(*arg);
5205
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005206 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5207 * expr(expr). */
5208 if (ret == OK)
5209 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 /*
5212 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5213 */
5214 if (ret == OK && evaluate && end_leader > start_leader)
5215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 int val = 0;
5218#ifdef FEAT_FLOAT
5219 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005221 if (rettv->v_type == VAR_FLOAT)
5222 f = rettv->vval.v_float;
5223 else
5224#endif
5225 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 clear_tv(rettv);
5229 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 else
5232 {
5233 while (end_leader > start_leader)
5234 {
5235 --end_leader;
5236 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005237 {
5238#ifdef FEAT_FLOAT
5239 if (rettv->v_type == VAR_FLOAT)
5240 f = !f;
5241 else
5242#endif
5243 val = !val;
5244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 {
5247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 f = -f;
5250 else
5251#endif
5252 val = -val;
5253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005254 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005255#ifdef FEAT_FLOAT
5256 if (rettv->v_type == VAR_FLOAT)
5257 {
5258 clear_tv(rettv);
5259 rettv->vval.v_float = f;
5260 }
5261 else
5262#endif
5263 {
5264 clear_tv(rettv);
5265 rettv->v_type = VAR_NUMBER;
5266 rettv->vval.v_number = val;
5267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270
5271 return ret;
5272}
5273
5274/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005275 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5276 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5278 */
5279 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285{
5286 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005287 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 long len = -1;
5290 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005294 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 if (verbose)
5297 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
5299 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005300#ifdef FEAT_FLOAT
5301 else if (rettv->v_type == VAR_FLOAT)
5302 {
5303 if (verbose)
5304 EMSG(_(e_float_as_string));
5305 return FAIL;
5306 }
5307#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 /*
5312 * dict.name
5313 */
5314 key = *arg + 1;
5315 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5316 ;
5317 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 }
5321 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005323 /*
5324 * something[idx]
5325 *
5326 * Get the (first) variable from inside the [].
5327 */
5328 *arg = skipwhite(*arg + 1);
5329 if (**arg == ':')
5330 empty1 = TRUE;
5331 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5332 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5334 {
5335 /* not a number or string */
5336 clear_tv(&var1);
5337 return FAIL;
5338 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339
5340 /*
5341 * Get the second variable from inside the [:].
5342 */
5343 if (**arg == ':')
5344 {
5345 range = TRUE;
5346 *arg = skipwhite(*arg + 1);
5347 if (**arg == ']')
5348 empty2 = TRUE;
5349 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005351 if (!empty1)
5352 clear_tv(&var1);
5353 return FAIL;
5354 }
5355 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5356 {
5357 /* not a number or string */
5358 if (!empty1)
5359 clear_tv(&var1);
5360 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 return FAIL;
5362 }
5363 }
5364
5365 /* Check for the ']'. */
5366 if (**arg != ']')
5367 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005368 if (verbose)
5369 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005370 clear_tv(&var1);
5371 if (range)
5372 clear_tv(&var2);
5373 return FAIL;
5374 }
5375 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 }
5377
5378 if (evaluate)
5379 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005380 n1 = 0;
5381 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 n1 = get_tv_number(&var1);
5384 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386 if (range)
5387 {
5388 if (empty2)
5389 n2 = -1;
5390 else
5391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 n2 = get_tv_number(&var2);
5393 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 }
5395 }
5396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005397 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 {
5399 case VAR_NUMBER:
5400 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 len = (long)STRLEN(s);
5403 if (range)
5404 {
5405 /* The resulting variable is a substring. If the indexes
5406 * are out of range the result is empty. */
5407 if (n1 < 0)
5408 {
5409 n1 = len + n1;
5410 if (n1 < 0)
5411 n1 = 0;
5412 }
5413 if (n2 < 0)
5414 n2 = len + n2;
5415 else if (n2 >= len)
5416 n2 = len;
5417 if (n1 >= len || n2 < 0 || n1 > n2)
5418 s = NULL;
5419 else
5420 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5421 }
5422 else
5423 {
5424 /* The resulting variable is a string of a single
5425 * character. If the index is too big or negative the
5426 * result is empty. */
5427 if (n1 >= len || n1 < 0)
5428 s = NULL;
5429 else
5430 s = vim_strnsave(s + n1, 1);
5431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005432 clear_tv(rettv);
5433 rettv->v_type = VAR_STRING;
5434 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 break;
5436
5437 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 if (n1 < 0)
5440 n1 = len + n1;
5441 if (!empty1 && (n1 < 0 || n1 >= len))
5442 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005443 /* For a range we allow invalid values and return an empty
5444 * list. A list index out of range is an error. */
5445 if (!range)
5446 {
5447 if (verbose)
5448 EMSGN(_(e_listidx), n1);
5449 return FAIL;
5450 }
5451 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 }
5453 if (range)
5454 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005455 list_T *l;
5456 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457
5458 if (n2 < 0)
5459 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005460 else if (n2 >= len)
5461 n2 = len - 1;
5462 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005463 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 l = list_alloc();
5465 if (l == NULL)
5466 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 n1 <= n2; ++n1)
5469 {
5470 if (list_append_tv(l, &item->li_tv) == FAIL)
5471 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005472 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 return FAIL;
5474 }
5475 item = item->li_next;
5476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 rettv->v_type = VAR_LIST;
5479 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005480 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 }
5482 else
5483 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005484 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 clear_tv(rettv);
5486 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489
5490 case VAR_DICT:
5491 if (range)
5492 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005493 if (verbose)
5494 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 if (len == -1)
5496 clear_tv(&var1);
5497 return FAIL;
5498 }
5499 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501
5502 if (len == -1)
5503 {
5504 key = get_tv_string(&var1);
5505 if (*key == NUL)
5506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005507 if (verbose)
5508 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005509 clear_tv(&var1);
5510 return FAIL;
5511 }
5512 }
5513
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005514 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005516 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005517 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 if (len == -1)
5519 clear_tv(&var1);
5520 if (item == NULL)
5521 return FAIL;
5522
5523 copy_tv(&item->di_tv, &var1);
5524 clear_tv(rettv);
5525 *rettv = var1;
5526 }
5527 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 }
5529 }
5530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 return OK;
5532}
5533
5534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 * Get an option value.
5536 * "arg" points to the '&' or '+' before the option name.
5537 * "arg" is advanced to character after the option name.
5538 * Return OK or FAIL.
5539 */
5540 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 int evaluate;
5545{
5546 char_u *option_end;
5547 long numval;
5548 char_u *stringval;
5549 int opt_type;
5550 int c;
5551 int working = (**arg == '+'); /* has("+option") */
5552 int ret = OK;
5553 int opt_flags;
5554
5555 /*
5556 * Isolate the option name and find its value.
5557 */
5558 option_end = find_option_end(arg, &opt_flags);
5559 if (option_end == NULL)
5560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 EMSG2(_("E112: Option name missing: %s"), *arg);
5563 return FAIL;
5564 }
5565
5566 if (!evaluate)
5567 {
5568 *arg = option_end;
5569 return OK;
5570 }
5571
5572 c = *option_end;
5573 *option_end = NUL;
5574 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 if (opt_type == -3) /* invalid name */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 EMSG2(_("E113: Unknown option: %s"), *arg);
5581 ret = FAIL;
5582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
5585 if (opt_type == -2) /* hidden string option */
5586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 rettv->v_type = VAR_STRING;
5588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590 else if (opt_type == -1) /* hidden number option */
5591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 rettv->v_type = VAR_NUMBER;
5593 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else if (opt_type == 1) /* number option */
5596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 rettv->v_type = VAR_NUMBER;
5598 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
5600 else /* string option */
5601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 rettv->v_type = VAR_STRING;
5603 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605 }
5606 else if (working && (opt_type == -2 || opt_type == -1))
5607 ret = FAIL;
5608
5609 *option_end = c; /* put back for error messages */
5610 *arg = option_end;
5611
5612 return ret;
5613}
5614
5615/*
5616 * Allocate a variable for a string constant.
5617 * Return OK or FAIL.
5618 */
5619 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 int evaluate;
5624{
5625 char_u *p;
5626 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 int extra = 0;
5628
5629 /*
5630 * Find the end of the string, skipping backslashed characters.
5631 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005632 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 if (*p == '\\' && p[1] != NUL)
5635 {
5636 ++p;
5637 /* A "\<x>" form occupies at least 4 characters, and produces up
5638 * to 6 characters: reserve space for 2 extra */
5639 if (*p == '<')
5640 extra += 2;
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643
5644 if (*p != '"')
5645 {
5646 EMSG2(_("E114: Missing quote: %s"), *arg);
5647 return FAIL;
5648 }
5649
5650 /* If only parsing, set *arg and return here */
5651 if (!evaluate)
5652 {
5653 *arg = p + 1;
5654 return OK;
5655 }
5656
5657 /*
5658 * Copy the string into allocated memory, handling backslashed
5659 * characters.
5660 */
5661 name = alloc((unsigned)(p - *arg + extra));
5662 if (name == NULL)
5663 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 rettv->v_type = VAR_STRING;
5665 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 if (*p == '\\')
5670 {
5671 switch (*++p)
5672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 case 'b': *name++ = BS; ++p; break;
5674 case 'e': *name++ = ESC; ++p; break;
5675 case 'f': *name++ = FF; ++p; break;
5676 case 'n': *name++ = NL; ++p; break;
5677 case 'r': *name++ = CAR; ++p; break;
5678 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 case 'X': /* hex: "\x1", "\x12" */
5681 case 'x':
5682 case 'u': /* Unicode: "\u0023" */
5683 case 'U':
5684 if (vim_isxdigit(p[1]))
5685 {
5686 int n, nr;
5687 int c = toupper(*p);
5688
5689 if (c == 'X')
5690 n = 2;
5691 else
5692 n = 4;
5693 nr = 0;
5694 while (--n >= 0 && vim_isxdigit(p[1]))
5695 {
5696 ++p;
5697 nr = (nr << 4) + hex2nr(*p);
5698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700#ifdef FEAT_MBYTE
5701 /* For "\u" store the number according to
5702 * 'encoding'. */
5703 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else
5706#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 break;
5710
5711 /* octal: "\1", "\12", "\123" */
5712 case '0':
5713 case '1':
5714 case '2':
5715 case '3':
5716 case '4':
5717 case '5':
5718 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 case '7': *name = *p++ - '0';
5720 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 *name = (*name << 3) + *p++ - '0';
5723 if (*p >= '0' && *p <= '7')
5724 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 break;
5728
5729 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 if (extra != 0)
5732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 break;
5735 }
5736 /* FALLTHROUGH */
5737
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 break;
5740 }
5741 }
5742 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005743 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 *arg = p + 1;
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 return OK;
5750}
5751
5752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 * Return OK or FAIL.
5755 */
5756 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 int evaluate;
5761{
5762 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 int reduce = 0;
5765
5766 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 */
5769 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 break;
5775 ++reduce;
5776 ++p;
5777 }
5778 }
5779
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 return FAIL;
5784 }
5785
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 if (!evaluate)
5788 {
5789 *arg = p + 1;
5790 return OK;
5791 }
5792
5793 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 */
5796 str = alloc((unsigned)((p - *arg) - reduce));
5797 if (str == NULL)
5798 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 rettv->v_type = VAR_STRING;
5800 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 break;
5808 ++p;
5809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 *arg = p + 1;
5814
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 return OK;
5816}
5817
5818/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005819 * Allocate a variable for a List and fill it from "*arg".
5820 * Return OK or FAIL.
5821 */
5822 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005823get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005825 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 int evaluate;
5827{
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 list_T *l = NULL;
5829 typval_T tv;
5830 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831
5832 if (evaluate)
5833 {
5834 l = list_alloc();
5835 if (l == NULL)
5836 return FAIL;
5837 }
5838
5839 *arg = skipwhite(*arg + 1);
5840 while (**arg != ']' && **arg != NUL)
5841 {
5842 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5843 goto failret;
5844 if (evaluate)
5845 {
5846 item = listitem_alloc();
5847 if (item != NULL)
5848 {
5849 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005850 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851 list_append(l, item);
5852 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005853 else
5854 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855 }
5856
5857 if (**arg == ']')
5858 break;
5859 if (**arg != ',')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 goto failret;
5863 }
5864 *arg = skipwhite(*arg + 1);
5865 }
5866
5867 if (**arg != ']')
5868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870failret:
5871 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005872 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 return FAIL;
5874 }
5875
5876 *arg = skipwhite(*arg + 1);
5877 if (evaluate)
5878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005879 rettv->v_type = VAR_LIST;
5880 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005881 ++l->lv_refcount;
5882 }
5883
5884 return OK;
5885}
5886
5887/*
5888 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005889 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005891 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892list_alloc()
5893{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005894 list_T *l;
5895
5896 l = (list_T *)alloc_clear(sizeof(list_T));
5897 if (l != NULL)
5898 {
5899 /* Prepend the list to the list of lists for garbage collection. */
5900 if (first_list != NULL)
5901 first_list->lv_used_prev = l;
5902 l->lv_used_prev = NULL;
5903 l->lv_used_next = first_list;
5904 first_list = l;
5905 }
5906 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907}
5908
5909/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005910 * Allocate an empty list for a return value.
5911 * Returns OK or FAIL.
5912 */
5913 static int
5914rettv_list_alloc(rettv)
5915 typval_T *rettv;
5916{
5917 list_T *l = list_alloc();
5918
5919 if (l == NULL)
5920 return FAIL;
5921
5922 rettv->vval.v_list = l;
5923 rettv->v_type = VAR_LIST;
5924 ++l->lv_refcount;
5925 return OK;
5926}
5927
5928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 * Unreference a list: decrement the reference count and free it when it
5930 * becomes zero.
5931 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005932 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005936 if (l != NULL && --l->lv_refcount <= 0)
5937 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938}
5939
5940/*
5941 * Free a list, including all items it points to.
5942 * Ignores the reference count.
5943 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005944 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005945list_free(l, recurse)
5946 list_T *l;
5947 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948{
Bram Moolenaar33570922005-01-25 22:26:29 +00005949 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005951 /* Remove the list from the list of lists for garbage collection. */
5952 if (l->lv_used_prev == NULL)
5953 first_list = l->lv_used_next;
5954 else
5955 l->lv_used_prev->lv_used_next = l->lv_used_next;
5956 if (l->lv_used_next != NULL)
5957 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5958
Bram Moolenaard9fba312005-06-26 22:34:35 +00005959 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 /* Remove the item before deleting it. */
5962 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005963 if (recurse || (item->li_tv.v_type != VAR_LIST
5964 && item->li_tv.v_type != VAR_DICT))
5965 clear_tv(&item->li_tv);
5966 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 }
5968 vim_free(l);
5969}
5970
5971/*
5972 * Allocate a list item.
5973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975listitem_alloc()
5976{
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978}
5979
5980/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005981 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005983 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005987 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 vim_free(item);
5989}
5990
5991/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005992 * Remove a list item from a List and free it. Also clears the value.
5993 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005994 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 list_T *l;
5997 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998{
5999 list_remove(l, item, item);
6000 listitem_free(item);
6001}
6002
6003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 * Get the number of items in a list.
6005 */
6006 static long
6007list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 if (l == NULL)
6011 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006012 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013}
6014
6015/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016 * Return TRUE when two lists have exactly the same values.
6017 */
6018 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006019list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 list_T *l1;
6021 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006022 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006023 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024{
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006027 if (l1 == NULL || l2 == NULL)
6028 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006029 if (l1 == l2)
6030 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006031 if (list_len(l1) != list_len(l2))
6032 return FALSE;
6033
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006034 for (item1 = l1->lv_first, item2 = l2->lv_first;
6035 item1 != NULL && item2 != NULL;
6036 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006038 return FALSE;
6039 return item1 == NULL && item2 == NULL;
6040}
6041
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006042#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6043 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006044/*
6045 * Return the dictitem that an entry in a hashtable points to.
6046 */
6047 dictitem_T *
6048dict_lookup(hi)
6049 hashitem_T *hi;
6050{
6051 return HI2DI(hi);
6052}
6053#endif
6054
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006055/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056 * Return TRUE when two dictionaries have exactly the same key/values.
6057 */
6058 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 dict_T *d1;
6061 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006062 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006063 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064{
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 hashitem_T *hi;
6066 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006067 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006069 if (d1 == NULL || d2 == NULL)
6070 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006071 if (d1 == d2)
6072 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006073 if (dict_len(d1) != dict_len(d2))
6074 return FALSE;
6075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006076 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006078 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006079 if (!HASHITEM_EMPTY(hi))
6080 {
6081 item2 = dict_find(d2, hi->hi_key, -1);
6082 if (item2 == NULL)
6083 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006085 return FALSE;
6086 --todo;
6087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006088 }
6089 return TRUE;
6090}
6091
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092static int tv_equal_recurse_limit;
6093
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006096 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006097 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 */
6099 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 typval_T *tv1;
6102 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 int ic; /* ignore case */
6104 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105{
6106 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006107 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006109 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006111 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006114 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115 * recursiveness to a limit. We guess they are equal then.
6116 * A fixed limit has the problem of still taking an awful long time.
6117 * Reduce the limit every time running into it. That should work fine for
6118 * deeply linked structures that are not recursively linked and catch
6119 * recursiveness quickly. */
6120 if (!recursive)
6121 tv_equal_recurse_limit = 1000;
6122 if (recursive_cnt >= tv_equal_recurse_limit)
6123 {
6124 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006125 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006126 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006130 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006131 ++recursive_cnt;
6132 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6133 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006134 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135
6136 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006137 ++recursive_cnt;
6138 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6139 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006140 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006141
6142 case VAR_FUNC:
6143 return (tv1->vval.v_string != NULL
6144 && tv2->vval.v_string != NULL
6145 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6146
6147 case VAR_NUMBER:
6148 return tv1->vval.v_number == tv2->vval.v_number;
6149
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006150#ifdef FEAT_FLOAT
6151 case VAR_FLOAT:
6152 return tv1->vval.v_float == tv2->vval.v_float;
6153#endif
6154
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006155 case VAR_STRING:
6156 s1 = get_tv_string_buf(tv1, buf1);
6157 s2 = get_tv_string_buf(tv2, buf2);
6158 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006159 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006160
6161 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162 return TRUE;
6163}
6164
6165/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 * Locate item with index "n" in list "l" and return it.
6167 * A negative index is counted from the end; -1 is the last item.
6168 * Returns NULL when "n" is out of range.
6169 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006170 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 long n;
6174{
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 long idx;
6177
6178 if (l == NULL)
6179 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180
6181 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006183 n = l->lv_len + n;
6184
6185 /* Check for index out of range. */
6186 if (n < 0 || n >= l->lv_len)
6187 return NULL;
6188
6189 /* When there is a cached index may start search from there. */
6190 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006192 if (n < l->lv_idx / 2)
6193 {
6194 /* closest to the start of the list */
6195 item = l->lv_first;
6196 idx = 0;
6197 }
6198 else if (n > (l->lv_idx + l->lv_len) / 2)
6199 {
6200 /* closest to the end of the list */
6201 item = l->lv_last;
6202 idx = l->lv_len - 1;
6203 }
6204 else
6205 {
6206 /* closest to the cached index */
6207 item = l->lv_idx_item;
6208 idx = l->lv_idx;
6209 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 }
6211 else
6212 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213 if (n < l->lv_len / 2)
6214 {
6215 /* closest to the start of the list */
6216 item = l->lv_first;
6217 idx = 0;
6218 }
6219 else
6220 {
6221 /* closest to the end of the list */
6222 item = l->lv_last;
6223 idx = l->lv_len - 1;
6224 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006226
6227 while (n > idx)
6228 {
6229 /* search forward */
6230 item = item->li_next;
6231 ++idx;
6232 }
6233 while (n < idx)
6234 {
6235 /* search backward */
6236 item = item->li_prev;
6237 --idx;
6238 }
6239
6240 /* cache the used index */
6241 l->lv_idx = idx;
6242 l->lv_idx_item = item;
6243
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244 return item;
6245}
6246
6247/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006248 * Get list item "l[idx]" as a number.
6249 */
6250 static long
6251list_find_nr(l, idx, errorp)
6252 list_T *l;
6253 long idx;
6254 int *errorp; /* set to TRUE when something wrong */
6255{
6256 listitem_T *li;
6257
6258 li = list_find(l, idx);
6259 if (li == NULL)
6260 {
6261 if (errorp != NULL)
6262 *errorp = TRUE;
6263 return -1L;
6264 }
6265 return get_tv_number_chk(&li->li_tv, errorp);
6266}
6267
6268/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006269 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6270 */
6271 char_u *
6272list_find_str(l, idx)
6273 list_T *l;
6274 long idx;
6275{
6276 listitem_T *li;
6277
6278 li = list_find(l, idx - 1);
6279 if (li == NULL)
6280 {
6281 EMSGN(_(e_listidx), idx);
6282 return NULL;
6283 }
6284 return get_tv_string(&li->li_tv);
6285}
6286
6287/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006288 * Locate "item" list "l" and return its index.
6289 * Returns -1 when "item" is not in the list.
6290 */
6291 static long
6292list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006293 list_T *l;
6294 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006295{
6296 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006298
6299 if (l == NULL)
6300 return -1;
6301 idx = 0;
6302 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6303 ++idx;
6304 if (li == NULL)
6305 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006306 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006307}
6308
6309/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 * Append item "item" to the end of list "l".
6311 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006312 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 list_T *l;
6315 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316{
6317 if (l->lv_last == NULL)
6318 {
6319 /* empty list */
6320 l->lv_first = item;
6321 l->lv_last = item;
6322 item->li_prev = NULL;
6323 }
6324 else
6325 {
6326 l->lv_last->li_next = item;
6327 item->li_prev = l->lv_last;
6328 l->lv_last = item;
6329 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 item->li_next = NULL;
6332}
6333
6334/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006338 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 list_T *l;
6341 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006343 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 copy_tv(tv, &li->li_tv);
6348 list_append(l, li);
6349 return OK;
6350}
6351
6352/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006353 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006354 * Return FAIL when out of memory.
6355 */
6356 int
6357list_append_dict(list, dict)
6358 list_T *list;
6359 dict_T *dict;
6360{
6361 listitem_T *li = listitem_alloc();
6362
6363 if (li == NULL)
6364 return FAIL;
6365 li->li_tv.v_type = VAR_DICT;
6366 li->li_tv.v_lock = 0;
6367 li->li_tv.vval.v_dict = dict;
6368 list_append(list, li);
6369 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 return OK;
6371}
6372
6373/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006374 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006375 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Returns FAIL when out of memory.
6377 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006378 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006379list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006380 list_T *l;
6381 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383{
6384 listitem_T *li = listitem_alloc();
6385
6386 if (li == NULL)
6387 return FAIL;
6388 list_append(l, li);
6389 li->li_tv.v_type = VAR_STRING;
6390 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006391 if (str == NULL)
6392 li->li_tv.vval.v_string = NULL;
6393 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006394 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006395 return FAIL;
6396 return OK;
6397}
6398
6399/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006400 * Append "n" to list "l".
6401 * Returns FAIL when out of memory.
6402 */
6403 static int
6404list_append_number(l, n)
6405 list_T *l;
6406 varnumber_T n;
6407{
6408 listitem_T *li;
6409
6410 li = listitem_alloc();
6411 if (li == NULL)
6412 return FAIL;
6413 li->li_tv.v_type = VAR_NUMBER;
6414 li->li_tv.v_lock = 0;
6415 li->li_tv.vval.v_number = n;
6416 list_append(l, li);
6417 return OK;
6418}
6419
6420/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006421 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 * If "item" is NULL append at the end.
6423 * Return FAIL when out of memory.
6424 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006427 list_T *l;
6428 typval_T *tv;
6429 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430{
Bram Moolenaar33570922005-01-25 22:26:29 +00006431 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432
6433 if (ni == NULL)
6434 return FAIL;
6435 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006436 list_insert(l, ni, item);
6437 return OK;
6438}
6439
6440 void
6441list_insert(l, ni, item)
6442 list_T *l;
6443 listitem_T *ni;
6444 listitem_T *item;
6445{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 if (item == NULL)
6447 /* Append new item at end of list. */
6448 list_append(l, ni);
6449 else
6450 {
6451 /* Insert new item before existing item. */
6452 ni->li_prev = item->li_prev;
6453 ni->li_next = item;
6454 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_idx;
6458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006460 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 l->lv_idx_item = NULL;
6463 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006465 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467}
6468
6469/*
6470 * Extend "l1" with "l2".
6471 * If "bef" is NULL append at the end, otherwise insert before this item.
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
6475list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 list_T *l1;
6477 list_T *l2;
6478 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479{
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006481 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 /* We also quit the loop when we have inserted the original item count of
6484 * the list, avoid a hang when we extend a list with itself. */
6485 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6487 return FAIL;
6488 return OK;
6489}
6490
6491/*
6492 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6493 * Return FAIL when out of memory.
6494 */
6495 static int
6496list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 list_T *l1;
6498 list_T *l2;
6499 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006503 if (l1 == NULL || l2 == NULL)
6504 return FAIL;
6505
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 if (l == NULL)
6509 return FAIL;
6510 tv->v_type = VAR_LIST;
6511 tv->vval.v_list = l;
6512
6513 /* append all items from the second list */
6514 return list_extend(l, l2, NULL);
6515}
6516
6517/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006520 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * Returns NULL when out of memory.
6522 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528{
Bram Moolenaar33570922005-01-25 22:26:29 +00006529 list_T *copy;
6530 listitem_T *item;
6531 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532
6533 if (orig == NULL)
6534 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535
6536 copy = list_alloc();
6537 if (copy != NULL)
6538 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 if (copyID != 0)
6540 {
6541 /* Do this before adding the items, because one of the items may
6542 * refer back to this list. */
6543 orig->lv_copyID = copyID;
6544 orig->lv_copylist = copy;
6545 }
6546 for (item = orig->lv_first; item != NULL && !got_int;
6547 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 {
6549 ni = listitem_alloc();
6550 if (ni == NULL)
6551 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006553 {
6554 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6555 {
6556 vim_free(ni);
6557 break;
6558 }
6559 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006561 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 list_append(copy, ni);
6563 }
6564 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 if (item != NULL)
6566 {
6567 list_unref(copy);
6568 copy = NULL;
6569 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 }
6571
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 return copy;
6573}
6574
6575/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006577 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006579 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *l;
6582 listitem_T *item;
6583 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587 /* notify watchers */
6588 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006590 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 list_fix_watch(l, ip);
6592 if (ip == item2)
6593 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595
6596 if (item2->li_next == NULL)
6597 l->lv_last = item->li_prev;
6598 else
6599 item2->li_next->li_prev = item->li_prev;
6600 if (item->li_prev == NULL)
6601 l->lv_first = item2->li_next;
6602 else
6603 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006604 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605}
6606
6607/*
6608 * Return an allocated string with the string representation of a list.
6609 * May return NULL.
6610 */
6611 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006612list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
6616 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617
6618 if (tv->vval.v_list == NULL)
6619 return NULL;
6620 ga_init2(&ga, (int)sizeof(char), 80);
6621 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006622 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 {
6624 vim_free(ga.ga_data);
6625 return NULL;
6626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 ga_append(&ga, ']');
6628 ga_append(&ga, NUL);
6629 return (char_u *)ga.ga_data;
6630}
6631
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006632typedef struct join_S {
6633 char_u *s;
6634 char_u *tofree;
6635} join_T;
6636
6637 static int
6638list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6639 garray_T *gap; /* to store the result in */
6640 list_T *l;
6641 char_u *sep;
6642 int echo_style;
6643 int copyID;
6644 garray_T *join_gap; /* to keep each list item string */
6645{
6646 int i;
6647 join_T *p;
6648 int len;
6649 int sumlen = 0;
6650 int first = TRUE;
6651 char_u *tofree;
6652 char_u numbuf[NUMBUFLEN];
6653 listitem_T *item;
6654 char_u *s;
6655
6656 /* Stringify each item in the list. */
6657 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6658 {
6659 if (echo_style)
6660 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6661 else
6662 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6663 if (s == NULL)
6664 return FAIL;
6665
6666 len = (int)STRLEN(s);
6667 sumlen += len;
6668
6669 ga_grow(join_gap, 1);
6670 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6671 if (tofree != NULL || s != numbuf)
6672 {
6673 p->s = s;
6674 p->tofree = tofree;
6675 }
6676 else
6677 {
6678 p->s = vim_strnsave(s, len);
6679 p->tofree = p->s;
6680 }
6681
6682 line_breakcheck();
6683 }
6684
6685 /* Allocate result buffer with its total size, avoid re-allocation and
6686 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6687 if (join_gap->ga_len >= 2)
6688 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6689 if (ga_grow(gap, sumlen + 2) == FAIL)
6690 return FAIL;
6691
6692 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6693 {
6694 if (first)
6695 first = FALSE;
6696 else
6697 ga_concat(gap, sep);
6698 p = ((join_T *)join_gap->ga_data) + i;
6699
6700 if (p->s != NULL)
6701 ga_concat(gap, p->s);
6702 line_breakcheck();
6703 }
6704
6705 return OK;
6706}
6707
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006710 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006713 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006714list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006716 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006718 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006719 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721 garray_T join_ga;
6722 int retval;
6723 join_T *p;
6724 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006726 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6727 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6728
6729 /* Dispose each item in join_ga. */
6730 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 p = (join_T *)join_ga.ga_data;
6733 for (i = 0; i < join_ga.ga_len; ++i)
6734 {
6735 vim_free(p->tofree);
6736 ++p;
6737 }
6738 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006739 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740
6741 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006742}
6743
6744/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006745 * Garbage collection for lists and dictionaries.
6746 *
6747 * We use reference counts to be able to free most items right away when they
6748 * are no longer used. But for composite items it's possible that it becomes
6749 * unused while the reference count is > 0: When there is a recursive
6750 * reference. Example:
6751 * :let l = [1, 2, 3]
6752 * :let d = {9: l}
6753 * :let l[1] = d
6754 *
6755 * Since this is quite unusual we handle this with garbage collection: every
6756 * once in a while find out which lists and dicts are not referenced from any
6757 * variable.
6758 *
6759 * Here is a good reference text about garbage collection (refers to Python
6760 * but it applies to all reference-counting mechanisms):
6761 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006763
6764/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006765 * Do garbage collection for lists and dicts.
6766 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 int
6769garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006770{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 buf_T *buf;
6773 win_T *wp;
6774 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006775 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006776 int did_free;
6777 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006778#ifdef FEAT_WINDOWS
6779 tabpage_T *tp;
6780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006782 /* Only do this once. */
6783 want_garbage_collect = FALSE;
6784 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006785 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006787 /* We advance by two because we add one for items referenced through
6788 * previous_funccal. */
6789 current_copyID += COPYID_INC;
6790 copyID = current_copyID;
6791
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792 /*
6793 * 1. Go through all accessible variables and mark all lists and dicts
6794 * with copyID.
6795 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006796
6797 /* Don't free variables in the previous_funccal list unless they are only
6798 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006799 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6801 {
6802 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6803 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6804 }
6805
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006806 /* script-local variables */
6807 for (i = 1; i <= ga_scripts.ga_len; ++i)
6808 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6809
6810 /* buffer-local variables */
6811 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006812 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
6814 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006815 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006816 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006817#ifdef FEAT_AUTOCMD
6818 if (aucmd_win != NULL)
6819 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6820#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006821
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006822#ifdef FEAT_WINDOWS
6823 /* tabpage-local variables */
6824 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006825 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#endif
6827
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 /* global variables */
6829 set_ref_in_ht(&globvarht, copyID);
6830
6831 /* function-local variables */
6832 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6833 {
6834 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6835 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6836 }
6837
Bram Moolenaard812df62008-11-09 12:46:09 +00006838 /* v: vars */
6839 set_ref_in_ht(&vimvarht, copyID);
6840
Bram Moolenaar1dced572012-04-05 16:54:08 +02006841#ifdef FEAT_LUA
6842 set_ref_in_lua(copyID);
6843#endif
6844
Bram Moolenaardb913952012-06-29 12:54:53 +02006845#ifdef FEAT_PYTHON
6846 set_ref_in_python(copyID);
6847#endif
6848
6849#ifdef FEAT_PYTHON3
6850 set_ref_in_python3(copyID);
6851#endif
6852
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006853 /*
6854 * 2. Free lists and dictionaries that are not referenced.
6855 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006856 did_free = free_unref_items(copyID);
6857
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006858 /*
6859 * 3. Check if any funccal can be freed now.
6860 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 for (pfc = &previous_funccal; *pfc != NULL; )
6862 {
6863 if (can_free_funccal(*pfc, copyID))
6864 {
6865 fc = *pfc;
6866 *pfc = fc->caller;
6867 free_funccal(fc, TRUE);
6868 did_free = TRUE;
6869 did_free_funccal = TRUE;
6870 }
6871 else
6872 pfc = &(*pfc)->caller;
6873 }
6874 if (did_free_funccal)
6875 /* When a funccal was freed some more items might be garbage
6876 * collected, so run again. */
6877 (void)garbage_collect();
6878
6879 return did_free;
6880}
6881
6882/*
6883 * Free lists and dictionaries that are no longer referenced.
6884 */
6885 static int
6886free_unref_items(copyID)
6887 int copyID;
6888{
6889 dict_T *dd;
6890 list_T *ll;
6891 int did_free = FALSE;
6892
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 */
6896 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006897 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006899 /* Free the Dictionary and ordinary items it contains, but don't
6900 * recurse into Lists and Dictionaries, they will be in the list
6901 * of dicts or list of lists. */
6902 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 did_free = TRUE;
6904
6905 /* restart, next dict may also have been freed */
6906 dd = first_dict;
6907 }
6908 else
6909 dd = dd->dv_used_next;
6910
6911 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006912 * Go through the list of lists and free items without the copyID.
6913 * But don't free a list that has a watcher (used in a for loop), these
6914 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 */
6916 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006917 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6918 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006920 /* Free the List and ordinary items it contains, but don't recurse
6921 * into Lists and Dictionaries, they will be in the list of dicts
6922 * or list of lists. */
6923 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 did_free = TRUE;
6925
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006926 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 ll = first_list;
6928 }
6929 else
6930 ll = ll->lv_used_next;
6931
6932 return did_free;
6933}
6934
6935/*
6936 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6937 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006938 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939set_ref_in_ht(ht, copyID)
6940 hashtab_T *ht;
6941 int copyID;
6942{
6943 int todo;
6944 hashitem_T *hi;
6945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006946 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947 for (hi = ht->ht_array; todo > 0; ++hi)
6948 if (!HASHITEM_EMPTY(hi))
6949 {
6950 --todo;
6951 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6952 }
6953}
6954
6955/*
6956 * Mark all lists and dicts referenced through list "l" with "copyID".
6957 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006958 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959set_ref_in_list(l, copyID)
6960 list_T *l;
6961 int copyID;
6962{
6963 listitem_T *li;
6964
6965 for (li = l->lv_first; li != NULL; li = li->li_next)
6966 set_ref_in_item(&li->li_tv, copyID);
6967}
6968
6969/*
6970 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6971 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006972 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973set_ref_in_item(tv, copyID)
6974 typval_T *tv;
6975 int copyID;
6976{
6977 dict_T *dd;
6978 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006979
6980 switch (tv->v_type)
6981 {
6982 case VAR_DICT:
6983 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006984 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006985 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 /* Didn't see this dict yet. */
6987 dd->dv_copyID = copyID;
6988 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991
6992 case VAR_LIST:
6993 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006994 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 /* Didn't see this list yet. */
6997 ll->lv_copyID = copyID;
6998 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003}
7004
7005/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006 * Allocate an empty header for a dictionary.
7007 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007008 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007009dict_alloc()
7010{
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012
Bram Moolenaar33570922005-01-25 22:26:29 +00007013 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007014 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007015 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007016 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 if (first_dict != NULL)
7018 first_dict->dv_used_prev = d;
7019 d->dv_used_next = first_dict;
7020 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007021 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022
Bram Moolenaar33570922005-01-25 22:26:29 +00007023 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007024 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007025 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007027 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007029 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030}
7031
7032/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007033 * Allocate an empty dict for a return value.
7034 * Returns OK or FAIL.
7035 */
7036 static int
7037rettv_dict_alloc(rettv)
7038 typval_T *rettv;
7039{
7040 dict_T *d = dict_alloc();
7041
7042 if (d == NULL)
7043 return FAIL;
7044
7045 rettv->vval.v_dict = d;
7046 rettv->v_type = VAR_DICT;
7047 ++d->dv_refcount;
7048 return OK;
7049}
7050
7051
7052/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007053 * Unreference a Dictionary: decrement the reference count and free it when it
7054 * becomes zero.
7055 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007056 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007058 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007060 if (d != NULL && --d->dv_refcount <= 0)
7061 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062}
7063
7064/*
7065 * Free a Dictionary, including all items it contains.
7066 * Ignores the reference count.
7067 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007068 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007069dict_free(d, recurse)
7070 dict_T *d;
7071 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 /* Remove the dict from the list of dicts for garbage collection. */
7078 if (d->dv_used_prev == NULL)
7079 first_dict = d->dv_used_next;
7080 else
7081 d->dv_used_prev->dv_used_next = d->dv_used_next;
7082 if (d->dv_used_next != NULL)
7083 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7084
7085 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007086 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007087 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090 if (!HASHITEM_EMPTY(hi))
7091 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007092 /* Remove the item before deleting it, just in case there is
7093 * something recursive causing trouble. */
7094 di = HI2DI(hi);
7095 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007096 if (recurse || (di->di_tv.v_type != VAR_LIST
7097 && di->di_tv.v_type != VAR_DICT))
7098 clear_tv(&di->di_tv);
7099 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007100 --todo;
7101 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 vim_free(d);
7105}
7106
7107/*
7108 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 * The "key" is copied to the new item.
7110 * Note that the value of the item "di_tv" still needs to be initialized!
7111 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007113 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114dictitem_alloc(key)
7115 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116{
Bram Moolenaar33570922005-01-25 22:26:29 +00007117 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007119 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007121 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007123 di->di_flags = 0;
7124 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007126}
7127
7128/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 * Make a copy of a Dictionary item.
7130 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134{
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007137 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7138 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139 if (di != NULL)
7140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007142 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 copy_tv(&org->di_tv, &di->di_tv);
7144 }
7145 return di;
7146}
7147
7148/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149 * Remove item "item" from Dictionary "dict" and free it.
7150 */
7151 static void
7152dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 dict_T *dict;
7154 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155{
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159 if (HASHITEM_EMPTY(hi))
7160 EMSG2(_(e_intern2), "dictitem_remove()");
7161 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 dictitem_free(item);
7164}
7165
7166/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167 * Free a dict item. Also clears the value.
7168 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007169 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007171 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173 clear_tv(&item->di_tv);
7174 vim_free(item);
7175}
7176
7177/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7179 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 * Returns NULL when out of memory.
7182 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007188{
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *copy;
7190 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007192 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007193
7194 if (orig == NULL)
7195 return NULL;
7196
7197 copy = dict_alloc();
7198 if (copy != NULL)
7199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 if (copyID != 0)
7201 {
7202 orig->dv_copyID = copyID;
7203 orig->dv_copydict = copy;
7204 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007205 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 --todo;
7211
7212 di = dictitem_alloc(hi->hi_key);
7213 if (di == NULL)
7214 break;
7215 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007216 {
7217 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7218 copyID) == FAIL)
7219 {
7220 vim_free(di);
7221 break;
7222 }
7223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 else
7225 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7226 if (dict_add(copy, di) == FAIL)
7227 {
7228 dictitem_free(di);
7229 break;
7230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007235 if (todo > 0)
7236 {
7237 dict_unref(copy);
7238 copy = NULL;
7239 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007240 }
7241
7242 return copy;
7243}
7244
7245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007247 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007249 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 dict_T *d;
7252 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253{
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255}
7256
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007258 * Add a number or string entry to dictionary "d".
7259 * When "str" is NULL use number "nr", otherwise use "str".
7260 * Returns FAIL when out of memory and when key already exists.
7261 */
7262 int
7263dict_add_nr_str(d, key, nr, str)
7264 dict_T *d;
7265 char *key;
7266 long nr;
7267 char_u *str;
7268{
7269 dictitem_T *item;
7270
7271 item = dictitem_alloc((char_u *)key);
7272 if (item == NULL)
7273 return FAIL;
7274 item->di_tv.v_lock = 0;
7275 if (str == NULL)
7276 {
7277 item->di_tv.v_type = VAR_NUMBER;
7278 item->di_tv.vval.v_number = nr;
7279 }
7280 else
7281 {
7282 item->di_tv.v_type = VAR_STRING;
7283 item->di_tv.vval.v_string = vim_strsave(str);
7284 }
7285 if (dict_add(d, item) == FAIL)
7286 {
7287 dictitem_free(item);
7288 return FAIL;
7289 }
7290 return OK;
7291}
7292
7293/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007294 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007295 * Returns FAIL when out of memory and when key already exists.
7296 */
7297 int
7298dict_add_list(d, key, list)
7299 dict_T *d;
7300 char *key;
7301 list_T *list;
7302{
7303 dictitem_T *item;
7304
7305 item = dictitem_alloc((char_u *)key);
7306 if (item == NULL)
7307 return FAIL;
7308 item->di_tv.v_lock = 0;
7309 item->di_tv.v_type = VAR_LIST;
7310 item->di_tv.vval.v_list = list;
7311 if (dict_add(d, item) == FAIL)
7312 {
7313 dictitem_free(item);
7314 return FAIL;
7315 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007316 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007317 return OK;
7318}
7319
7320/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 * Get the number of items in a Dictionary.
7322 */
7323 static long
7324dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007327 if (d == NULL)
7328 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007329 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330}
7331
7332/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 * Find item "key[len]" in Dictionary "d".
7334 * If "len" is negative use strlen(key).
7335 * Returns NULL when not found.
7336 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007337 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340 char_u *key;
7341 int len;
7342{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343#define AKEYLEN 200
7344 char_u buf[AKEYLEN];
7345 char_u *akey;
7346 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 if (len < 0)
7350 akey = key;
7351 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 tofree = akey = vim_strnsave(key, len);
7354 if (akey == NULL)
7355 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007356 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 else
7358 {
7359 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007360 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 akey = buf;
7362 }
7363
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 vim_free(tofree);
7366 if (HASHITEM_EMPTY(hi))
7367 return NULL;
7368 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369}
7370
7371/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007372 * Get a string item from a dictionary.
7373 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007374 * Returns NULL if the entry doesn't exist or out of memory.
7375 */
7376 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007377get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007378 dict_T *d;
7379 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007381{
7382 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007383 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007384
7385 di = dict_find(d, key, -1);
7386 if (di == NULL)
7387 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007388 s = get_tv_string(&di->di_tv);
7389 if (save && s != NULL)
7390 s = vim_strsave(s);
7391 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007392}
7393
7394/*
7395 * Get a number item from a dictionary.
7396 * Returns 0 if the entry doesn't exist or out of memory.
7397 */
7398 long
7399get_dict_number(d, key)
7400 dict_T *d;
7401 char_u *key;
7402{
7403 dictitem_T *di;
7404
7405 di = dict_find(d, key, -1);
7406 if (di == NULL)
7407 return 0;
7408 return get_tv_number(&di->di_tv);
7409}
7410
7411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 * Return an allocated string with the string representation of a Dictionary.
7413 * May return NULL.
7414 */
7415 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007416dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007418 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419{
7420 garray_T ga;
7421 int first = TRUE;
7422 char_u *tofree;
7423 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 return NULL;
7431 ga_init2(&ga, (int)sizeof(char), 80);
7432 ga_append(&ga, '{');
7433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007434 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007439 --todo;
7440
7441 if (first)
7442 first = FALSE;
7443 else
7444 ga_concat(&ga, (char_u *)", ");
7445
7446 tofree = string_quote(hi->hi_key, FALSE);
7447 if (tofree != NULL)
7448 {
7449 ga_concat(&ga, tofree);
7450 vim_free(tofree);
7451 }
7452 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007453 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007454 if (s != NULL)
7455 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007457 if (s == NULL)
7458 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 if (todo > 0)
7462 {
7463 vim_free(ga.ga_data);
7464 return NULL;
7465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466
7467 ga_append(&ga, '}');
7468 ga_append(&ga, NUL);
7469 return (char_u *)ga.ga_data;
7470}
7471
7472/*
7473 * Allocate a variable for a Dictionary and fill it from "*arg".
7474 * Return OK or FAIL. Returns NOTDONE for {expr}.
7475 */
7476 static int
7477get_dict_tv(arg, rettv, evaluate)
7478 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007479 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 int evaluate;
7481{
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 dict_T *d = NULL;
7483 typval_T tvkey;
7484 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007485 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489
7490 /*
7491 * First check if it's not a curly-braces thing: {expr}.
7492 * Must do this without evaluating, otherwise a function may be called
7493 * twice. Unfortunately this means we need to call eval1() twice for the
7494 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 if (*start != '}')
7498 {
7499 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7500 return FAIL;
7501 if (*start == '}')
7502 return NOTDONE;
7503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504
7505 if (evaluate)
7506 {
7507 d = dict_alloc();
7508 if (d == NULL)
7509 return FAIL;
7510 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 tvkey.v_type = VAR_UNKNOWN;
7512 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513
7514 *arg = skipwhite(*arg + 1);
7515 while (**arg != '}' && **arg != NUL)
7516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 goto failret;
7519 if (**arg != ':')
7520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007521 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 goto failret;
7524 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007525 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007527 key = get_tv_string_buf_chk(&tvkey, buf);
7528 if (key == NULL || *key == NUL)
7529 {
7530 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7531 if (key != NULL)
7532 EMSG(_(e_emptykey));
7533 clear_tv(&tvkey);
7534 goto failret;
7535 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537
7538 *arg = skipwhite(*arg + 1);
7539 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7540 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007541 if (evaluate)
7542 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 goto failret;
7544 }
7545 if (evaluate)
7546 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 if (item != NULL)
7549 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007550 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 clear_tv(&tv);
7553 goto failret;
7554 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 item = dictitem_alloc(key);
7556 clear_tv(&tvkey);
7557 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007560 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 if (dict_add(d, item) == FAIL)
7562 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 }
7564 }
7565
7566 if (**arg == '}')
7567 break;
7568 if (**arg != ',')
7569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007570 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 goto failret;
7572 }
7573 *arg = skipwhite(*arg + 1);
7574 }
7575
7576 if (**arg != '}')
7577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007578 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579failret:
7580 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007581 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 return FAIL;
7583 }
7584
7585 *arg = skipwhite(*arg + 1);
7586 if (evaluate)
7587 {
7588 rettv->v_type = VAR_DICT;
7589 rettv->vval.v_dict = d;
7590 ++d->dv_refcount;
7591 }
7592
7593 return OK;
7594}
7595
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007597 * Return a string with the string representation of a variable.
7598 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007599 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007600 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007602 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007603 */
7604 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007608 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007610{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007611 static int recurse = 0;
7612 char_u *r = NULL;
7613
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007616 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 *tofree = NULL;
7618 return NULL;
7619 }
7620 ++recurse;
7621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007622 switch (tv->v_type)
7623 {
7624 case VAR_FUNC:
7625 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 r = tv->vval.v_string;
7627 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007629 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630 if (tv->vval.v_list == NULL)
7631 {
7632 *tofree = NULL;
7633 r = NULL;
7634 }
7635 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7636 {
7637 *tofree = NULL;
7638 r = (char_u *)"[...]";
7639 }
7640 else
7641 {
7642 tv->vval.v_list->lv_copyID = copyID;
7643 *tofree = list2string(tv, copyID);
7644 r = *tofree;
7645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007647
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649 if (tv->vval.v_dict == NULL)
7650 {
7651 *tofree = NULL;
7652 r = NULL;
7653 }
7654 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7655 {
7656 *tofree = NULL;
7657 r = (char_u *)"{...}";
7658 }
7659 else
7660 {
7661 tv->vval.v_dict->dv_copyID = copyID;
7662 *tofree = dict2string(tv, copyID);
7663 r = *tofree;
7664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007665 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007666
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007667 case VAR_STRING:
7668 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 *tofree = NULL;
7670 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007671 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007673#ifdef FEAT_FLOAT
7674 case VAR_FLOAT:
7675 *tofree = NULL;
7676 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7677 r = numbuf;
7678 break;
7679#endif
7680
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007683 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007685
7686 --recurse;
7687 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688}
7689
7690/*
7691 * Return a string with the string representation of a variable.
7692 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7693 * "numbuf" is used for a number.
7694 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007695 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 */
7697 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007698tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 char_u **tofree;
7701 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007702 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703{
7704 switch (tv->v_type)
7705 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 case VAR_FUNC:
7707 *tofree = string_quote(tv->vval.v_string, TRUE);
7708 return *tofree;
7709 case VAR_STRING:
7710 *tofree = string_quote(tv->vval.v_string, FALSE);
7711 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007712#ifdef FEAT_FLOAT
7713 case VAR_FLOAT:
7714 *tofree = NULL;
7715 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7716 return numbuf;
7717#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007718 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007722 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007725 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007726}
7727
7728/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 * Return string "str" in ' quotes, doubling ' characters.
7730 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007732 */
7733 static char_u *
7734string_quote(str, function)
7735 char_u *str;
7736 int function;
7737{
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007739 char_u *p, *r, *s;
7740
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 len = (function ? 13 : 3);
7742 if (str != NULL)
7743 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007744 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 for (p = str; *p != NUL; mb_ptr_adv(p))
7746 if (*p == '\'')
7747 ++len;
7748 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007749 s = r = alloc(len);
7750 if (r != NULL)
7751 {
7752 if (function)
7753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755 r += 10;
7756 }
7757 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 if (str != NULL)
7760 for (p = str; *p != NUL; )
7761 {
7762 if (*p == '\'')
7763 *r++ = '\'';
7764 MB_COPY_CHAR(p, r);
7765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767 if (function)
7768 *r++ = ')';
7769 *r++ = NUL;
7770 }
7771 return s;
7772}
7773
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007774#ifdef FEAT_FLOAT
7775/*
7776 * Convert the string "text" to a floating point number.
7777 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7778 * this always uses a decimal point.
7779 * Returns the length of the text that was consumed.
7780 */
7781 static int
7782string2float(text, value)
7783 char_u *text;
7784 float_T *value; /* result stored here */
7785{
7786 char *s = (char *)text;
7787 float_T f;
7788
7789 f = strtod(s, &s);
7790 *value = f;
7791 return (int)((char_u *)s - text);
7792}
7793#endif
7794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * Get the value of an environment variable.
7797 * "arg" is pointing to the '$'. It is advanced to after the name.
7798 * If the environment variable was not set, silently assume it is empty.
7799 * Always return OK.
7800 */
7801 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007802get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 int evaluate;
7806{
7807 char_u *string = NULL;
7808 int len;
7809 int cc;
7810 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007811 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
7813 ++*arg;
7814 name = *arg;
7815 len = get_env_len(arg);
7816 if (evaluate)
7817 {
7818 if (len != 0)
7819 {
7820 cc = name[len];
7821 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007822 /* first try vim_getenv(), fast for normal environment vars */
7823 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007825 {
7826 if (!mustfree)
7827 string = vim_strsave(string);
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 else
7830 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007831 if (mustfree)
7832 vim_free(string);
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 /* next try expanding things like $VIM and ${HOME} */
7835 string = expand_env_save(name - 1);
7836 if (string != NULL && *string == '$')
7837 {
7838 vim_free(string);
7839 string = NULL;
7840 }
7841 }
7842 name[len] = cc;
7843 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844 rettv->v_type = VAR_STRING;
7845 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847
7848 return OK;
7849}
7850
7851/*
7852 * Array with names and number of arguments of all internal functions
7853 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7854 */
7855static struct fst
7856{
7857 char *f_name; /* function name */
7858 char f_min_argc; /* minimal number of arguments */
7859 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007861 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862} functions[] =
7863{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007866 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007867#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007868 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007869 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"append", 2, 2, f_append},
7871 {"argc", 0, 0, f_argc},
7872 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007873 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007875 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007877 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007880 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"bufexists", 1, 1, f_bufexists},
7882 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7883 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7884 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7885 {"buflisted", 1, 1, f_buflisted},
7886 {"bufloaded", 1, 1, f_bufloaded},
7887 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007888 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"bufwinnr", 1, 1, f_bufwinnr},
7890 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007891 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007892 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894#ifdef FEAT_FLOAT
7895 {"ceil", 1, 1, f_ceil},
7896#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007897 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007898 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007900 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007902#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007903 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007904 {"complete_add", 1, 1, f_complete_add},
7905 {"complete_check", 0, 0, f_complete_check},
7906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007911 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007912#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007913 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007915 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007916 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {"delete", 1, 1, f_delete},
7918 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007919 {"diff_filler", 1, 1, f_diff_filler},
7920 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007921 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"eventhandler", 0, 0, f_eventhandler},
7925 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007926 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007928#ifdef FEAT_FLOAT
7929 {"exp", 1, 1, f_exp},
7930#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007931 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007932 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007933 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7935 {"filereadable", 1, 1, f_filereadable},
7936 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007937 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007938 {"finddir", 1, 3, f_finddir},
7939 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007940#ifdef FEAT_FLOAT
7941 {"float2nr", 1, 1, f_float2nr},
7942 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007943 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007944#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007945 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"fnamemodify", 2, 2, f_fnamemodify},
7947 {"foldclosed", 1, 1, f_foldclosed},
7948 {"foldclosedend", 1, 1, f_foldclosedend},
7949 {"foldlevel", 1, 1, f_foldlevel},
7950 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007951 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007953 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007954 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007955 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007956 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007957 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"getchar", 0, 1, f_getchar},
7959 {"getcharmod", 0, 0, f_getcharmod},
7960 {"getcmdline", 0, 0, f_getcmdline},
7961 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007962 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007964 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007965 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"getfsize", 1, 1, f_getfsize},
7967 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007968 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007969 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007970 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007971 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007972 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007973 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007974 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007975 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007977 {"gettabvar", 2, 3, f_gettabvar},
7978 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"getwinposx", 0, 0, f_getwinposx},
7980 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007981 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007982 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007983 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007986 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007987 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7989 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7990 {"histadd", 2, 2, f_histadd},
7991 {"histdel", 1, 2, f_histdel},
7992 {"histget", 1, 2, f_histget},
7993 {"histnr", 1, 1, f_histnr},
7994 {"hlID", 1, 1, f_hlID},
7995 {"hlexists", 1, 1, f_hlexists},
7996 {"hostname", 0, 0, f_hostname},
7997 {"iconv", 3, 3, f_iconv},
7998 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008000 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008002 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 {"inputrestore", 0, 0, f_inputrestore},
8004 {"inputsave", 0, 0, f_inputsave},
8005 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008007 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008009 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008010 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008011 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008014 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"libcall", 3, 3, f_libcall},
8016 {"libcallnr", 3, 3, f_libcallnr},
8017 {"line", 1, 1, f_line},
8018 {"line2byte", 1, 1, f_line2byte},
8019 {"lispindent", 1, 1, f_lispindent},
8020 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008022 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008023 {"log10", 1, 1, f_log10},
8024#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008025#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008026 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008027#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008029 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008030 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008031 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008032 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008033 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008034 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008035 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008036 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008037 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008038 {"max", 1, 1, f_max},
8039 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008040#ifdef vim_mkdir
8041 {"mkdir", 1, 3, f_mkdir},
8042#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008043 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008044#ifdef FEAT_MZSCHEME
8045 {"mzeval", 1, 1, f_mzeval},
8046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008048 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008049 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008050 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008051#ifdef FEAT_FLOAT
8052 {"pow", 2, 2, f_pow},
8053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008055 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008056 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008057#ifdef FEAT_PYTHON3
8058 {"py3eval", 1, 1, f_py3eval},
8059#endif
8060#ifdef FEAT_PYTHON
8061 {"pyeval", 1, 1, f_pyeval},
8062#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008064 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008065 {"reltime", 0, 2, f_reltime},
8066 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"remote_expr", 2, 3, f_remote_expr},
8068 {"remote_foreground", 1, 1, f_remote_foreground},
8069 {"remote_peek", 1, 2, f_remote_peek},
8070 {"remote_read", 1, 1, f_remote_read},
8071 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008072 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008074 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008076 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"round", 1, 1, f_round},
8079#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008080 {"screenattr", 2, 2, f_screenattr},
8081 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008082 {"screencol", 0, 0, f_screencol},
8083 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008084 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008085 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008086 {"searchpair", 3, 7, f_searchpair},
8087 {"searchpairpos", 3, 7, f_searchpairpos},
8088 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"server2client", 2, 2, f_server2client},
8090 {"serverlist", 0, 0, f_serverlist},
8091 {"setbufvar", 3, 3, f_setbufvar},
8092 {"setcmdpos", 1, 1, f_setcmdpos},
8093 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008094 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008095 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008096 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008097 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008099 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008100 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008102#ifdef FEAT_CRYPT
8103 {"sha256", 1, 1, f_sha256},
8104#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008105 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008106 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008110 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008111#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008112 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008113 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008114 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008115 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008116 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008117#ifdef FEAT_FLOAT
8118 {"sqrt", 1, 1, f_sqrt},
8119 {"str2float", 1, 1, f_str2float},
8120#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008121 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008122 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008123 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124#ifdef HAVE_STRFTIME
8125 {"strftime", 1, 2, f_strftime},
8126#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"strlen", 1, 1, f_strlen},
8130 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008131 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008133 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008134 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"substitute", 4, 4, f_substitute},
8136 {"synID", 3, 3, f_synID},
8137 {"synIDattr", 2, 3, f_synIDattr},
8138 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008139 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008140 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008141 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008142 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008143 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008144 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008145 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008146 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008147#ifdef FEAT_FLOAT
8148 {"tan", 1, 1, f_tan},
8149 {"tanh", 1, 1, f_tanh},
8150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008152 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"tolower", 1, 1, f_tolower},
8154 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008155 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008156#ifdef FEAT_FLOAT
8157 {"trunc", 1, 1, f_trunc},
8158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008160 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008161 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008162 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008163 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"virtcol", 1, 1, f_virtcol},
8165 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008166 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"winbufnr", 1, 1, f_winbufnr},
8168 {"wincol", 0, 0, f_wincol},
8169 {"winheight", 1, 1, f_winheight},
8170 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008171 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008173 {"winrestview", 1, 1, f_winrestview},
8174 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008176 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008177 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178};
8179
8180#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8181
8182/*
8183 * Function given to ExpandGeneric() to obtain the list of internal
8184 * or user defined function names.
8185 */
8186 char_u *
8187get_function_name(xp, idx)
8188 expand_T *xp;
8189 int idx;
8190{
8191 static int intidx = -1;
8192 char_u *name;
8193
8194 if (idx == 0)
8195 intidx = -1;
8196 if (intidx < 0)
8197 {
8198 name = get_user_func_name(xp, idx);
8199 if (name != NULL)
8200 return name;
8201 }
8202 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8203 {
8204 STRCPY(IObuff, functions[intidx].f_name);
8205 STRCAT(IObuff, "(");
8206 if (functions[intidx].f_max_argc == 0)
8207 STRCAT(IObuff, ")");
8208 return IObuff;
8209 }
8210
8211 return NULL;
8212}
8213
8214/*
8215 * Function given to ExpandGeneric() to obtain the list of internal or
8216 * user defined variable or function names.
8217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 char_u *
8219get_expr_name(xp, idx)
8220 expand_T *xp;
8221 int idx;
8222{
8223 static int intidx = -1;
8224 char_u *name;
8225
8226 if (idx == 0)
8227 intidx = -1;
8228 if (intidx < 0)
8229 {
8230 name = get_function_name(xp, idx);
8231 if (name != NULL)
8232 return name;
8233 }
8234 return get_user_var_name(xp, ++intidx);
8235}
8236
8237#endif /* FEAT_CMDL_COMPL */
8238
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008239#if defined(EBCDIC) || defined(PROTO)
8240/*
8241 * Compare struct fst by function name.
8242 */
8243 static int
8244compare_func_name(s1, s2)
8245 const void *s1;
8246 const void *s2;
8247{
8248 struct fst *p1 = (struct fst *)s1;
8249 struct fst *p2 = (struct fst *)s2;
8250
8251 return STRCMP(p1->f_name, p2->f_name);
8252}
8253
8254/*
8255 * Sort the function table by function name.
8256 * The sorting of the table above is ASCII dependant.
8257 * On machines using EBCDIC we have to sort it.
8258 */
8259 static void
8260sortFunctions()
8261{
8262 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8263
8264 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8265}
8266#endif
8267
8268
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269/*
8270 * Find internal function in table above.
8271 * Return index, or -1 if not found
8272 */
8273 static int
8274find_internal_func(name)
8275 char_u *name; /* name of the function */
8276{
8277 int first = 0;
8278 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8279 int cmp;
8280 int x;
8281
8282 /*
8283 * Find the function name in the table. Binary search.
8284 */
8285 while (first <= last)
8286 {
8287 x = first + ((unsigned)(last - first) >> 1);
8288 cmp = STRCMP(name, functions[x].f_name);
8289 if (cmp < 0)
8290 last = x - 1;
8291 else if (cmp > 0)
8292 first = x + 1;
8293 else
8294 return x;
8295 }
8296 return -1;
8297}
8298
8299/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8301 * name it contains, otherwise return "name".
8302 */
8303 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008304deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 char_u *name;
8306 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008307 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308{
Bram Moolenaar33570922005-01-25 22:26:29 +00008309 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008310 int cc;
8311
8312 cc = name[*lenp];
8313 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008314 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008317 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 {
8320 *lenp = 0;
8321 return (char_u *)""; /* just in case */
8322 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008323 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 }
8326
8327 return name;
8328}
8329
8330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 * Allocate a variable for the result of a function.
8332 * Return OK or FAIL.
8333 */
8334 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008335get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8336 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 char_u *name; /* name of the function */
8338 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 char_u **arg; /* argument, pointing to the '(' */
8341 linenr_T firstline; /* first line of range */
8342 linenr_T lastline; /* last line of range */
8343 int *doesrange; /* return: function handled range */
8344 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 char_u *argp;
8348 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008349 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 int argcount = 0; /* number of arguments found */
8351
8352 /*
8353 * Get the arguments.
8354 */
8355 argp = *arg;
8356 while (argcount < MAX_FUNC_ARGS)
8357 {
8358 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8359 if (*argp == ')' || *argp == ',' || *argp == NUL)
8360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8362 {
8363 ret = FAIL;
8364 break;
8365 }
8366 ++argcount;
8367 if (*argp != ',')
8368 break;
8369 }
8370 if (*argp == ')')
8371 ++argp;
8372 else
8373 ret = FAIL;
8374
8375 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008377 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 {
8380 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008381 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008383 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385
8386 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008387 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 *arg = skipwhite(argp);
8390 return ret;
8391}
8392
8393
8394/*
8395 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008396 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008397 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 */
8399 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008401 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008402 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008404 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008406 typval_T *argvars; /* vars for arguments, must have "argcount"
8407 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 linenr_T firstline; /* first line of range */
8409 linenr_T lastline; /* last line of range */
8410 int *doesrange; /* return: function handled range */
8411 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
8414 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415#define ERROR_UNKNOWN 0
8416#define ERROR_TOOMANY 1
8417#define ERROR_TOOFEW 2
8418#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008419#define ERROR_DICT 4
8420#define ERROR_NONE 5
8421#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 int error = ERROR_NONE;
8423 int i;
8424 int llen;
8425 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426#define FLEN_FIXED 40
8427 char_u fname_buf[FLEN_FIXED + 1];
8428 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008429 char_u *name;
8430
8431 /* Make a copy of the name, if it comes from a funcref variable it could
8432 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008433 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008434 if (name == NULL)
8435 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
8437 /*
8438 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8439 * Change <SNR>123_name() to K_SNR 123_name().
8440 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 llen = eval_fname_script(name);
8443 if (llen > 0)
8444 {
8445 fname_buf[0] = K_SPECIAL;
8446 fname_buf[1] = KS_EXTRA;
8447 fname_buf[2] = (int)KE_SNR;
8448 i = 3;
8449 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8450 {
8451 if (current_SID <= 0)
8452 error = ERROR_SCRIPT;
8453 else
8454 {
8455 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8456 i = (int)STRLEN(fname_buf);
8457 }
8458 }
8459 if (i + STRLEN(name + llen) < FLEN_FIXED)
8460 {
8461 STRCPY(fname_buf + i, name + llen);
8462 fname = fname_buf;
8463 }
8464 else
8465 {
8466 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8467 if (fname == NULL)
8468 error = ERROR_OTHER;
8469 else
8470 {
8471 mch_memmove(fname, fname_buf, (size_t)i);
8472 STRCPY(fname + i, name + llen);
8473 }
8474 }
8475 }
8476 else
8477 fname = name;
8478
8479 *doesrange = FALSE;
8480
8481
8482 /* execute the function if no errors detected and executing */
8483 if (evaluate && error == ERROR_NONE)
8484 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008485 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8486 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 error = ERROR_UNKNOWN;
8488
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008489 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
8491 /*
8492 * User defined function.
8493 */
8494 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008495
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008497 /* Trigger FuncUndefined event, may load the function. */
8498 if (fp == NULL
8499 && apply_autocmds(EVENT_FUNCUNDEFINED,
8500 fname, fname, TRUE, NULL)
8501 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008503 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 fp = find_func(fname);
8505 }
8506#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008508 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008509 {
8510 /* loaded a package, search for the function again */
8511 fp = find_func(fname);
8512 }
8513
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (fp != NULL)
8515 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008518 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008520 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008522 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 else
8525 {
8526 /*
8527 * Call the user function.
8528 * Save and restore search patterns, script variables and
8529 * redo buffer.
8530 */
8531 save_search_patterns();
8532 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008533 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008534 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008536 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8537 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8538 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008539 /* Function was unreferenced while being used, free it
8540 * now. */
8541 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 restoreRedobuff();
8543 restore_search_patterns();
8544 error = ERROR_NONE;
8545 }
8546 }
8547 }
8548 else
8549 {
8550 /*
8551 * Find the function name in the table, call its implementation.
8552 */
8553 i = find_internal_func(fname);
8554 if (i >= 0)
8555 {
8556 if (argcount < functions[i].f_min_argc)
8557 error = ERROR_TOOFEW;
8558 else if (argcount > functions[i].f_max_argc)
8559 error = ERROR_TOOMANY;
8560 else
8561 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008562 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 error = ERROR_NONE;
8565 }
8566 }
8567 }
8568 /*
8569 * The function call (or "FuncUndefined" autocommand sequence) might
8570 * have been aborted by an error, an interrupt, or an explicitly thrown
8571 * exception that has not been caught so far. This situation can be
8572 * tested for by calling aborting(). For an error in an internal
8573 * function or for the "E132" error in call_user_func(), however, the
8574 * throw point at which the "force_abort" flag (temporarily reset by
8575 * emsg()) is normally updated has not been reached yet. We need to
8576 * update that flag first to make aborting() reliable.
8577 */
8578 update_force_abort();
8579 }
8580 if (error == ERROR_NONE)
8581 ret = OK;
8582
8583 /*
8584 * Report an error unless the argument evaluation or function call has been
8585 * cancelled due to an aborting error, an interrupt, or an exception.
8586 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008587 if (!aborting())
8588 {
8589 switch (error)
8590 {
8591 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008592 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008593 break;
8594 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008595 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008596 break;
8597 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008598 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 name);
8600 break;
8601 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008602 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008603 name);
8604 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008606 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008607 name);
8608 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008609 }
8610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (fname != name && fname != fname_buf)
8613 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008614 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
8616 return ret;
8617}
8618
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008619/*
8620 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008621 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008622 */
8623 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008624emsg_funcname(ermsg, name)
8625 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008626 char_u *name;
8627{
8628 char_u *p;
8629
8630 if (*name == K_SPECIAL)
8631 p = concat_str((char_u *)"<SNR>", name + 3);
8632 else
8633 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008634 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008635 if (p != name)
8636 vim_free(p);
8637}
8638
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008639/*
8640 * Return TRUE for a non-zero Number and a non-empty String.
8641 */
8642 static int
8643non_zero_arg(argvars)
8644 typval_T *argvars;
8645{
8646 return ((argvars[0].v_type == VAR_NUMBER
8647 && argvars[0].vval.v_number != 0)
8648 || (argvars[0].v_type == VAR_STRING
8649 && argvars[0].vval.v_string != NULL
8650 && *argvars[0].vval.v_string != NUL));
8651}
8652
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653/*********************************************
8654 * Implementation of the built-in functions
8655 */
8656
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008657#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008658static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8659
8660/*
8661 * Get the float value of "argvars[0]" into "f".
8662 * Returns FAIL when the argument is not a Number or Float.
8663 */
8664 static int
8665get_float_arg(argvars, f)
8666 typval_T *argvars;
8667 float_T *f;
8668{
8669 if (argvars[0].v_type == VAR_FLOAT)
8670 {
8671 *f = argvars[0].vval.v_float;
8672 return OK;
8673 }
8674 if (argvars[0].v_type == VAR_NUMBER)
8675 {
8676 *f = (float_T)argvars[0].vval.v_number;
8677 return OK;
8678 }
8679 EMSG(_("E808: Number or Float required"));
8680 return FAIL;
8681}
8682
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683/*
8684 * "abs(expr)" function
8685 */
8686 static void
8687f_abs(argvars, rettv)
8688 typval_T *argvars;
8689 typval_T *rettv;
8690{
8691 if (argvars[0].v_type == VAR_FLOAT)
8692 {
8693 rettv->v_type = VAR_FLOAT;
8694 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8695 }
8696 else
8697 {
8698 varnumber_T n;
8699 int error = FALSE;
8700
8701 n = get_tv_number_chk(&argvars[0], &error);
8702 if (error)
8703 rettv->vval.v_number = -1;
8704 else if (n > 0)
8705 rettv->vval.v_number = n;
8706 else
8707 rettv->vval.v_number = -n;
8708 }
8709}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008710
8711/*
8712 * "acos()" function
8713 */
8714 static void
8715f_acos(argvars, rettv)
8716 typval_T *argvars;
8717 typval_T *rettv;
8718{
8719 float_T f;
8720
8721 rettv->v_type = VAR_FLOAT;
8722 if (get_float_arg(argvars, &f) == OK)
8723 rettv->vval.v_float = acos(f);
8724 else
8725 rettv->vval.v_float = 0.0;
8726}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008727#endif
8728
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 */
8732 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008733f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *argvars;
8735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
Bram Moolenaar33570922005-01-25 22:26:29 +00008737 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008740 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008742 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008743 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008744 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008745 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008746 }
8747 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008748 EMSG(_(e_listreq));
8749}
8750
8751/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008752 * "and(expr, expr)" function
8753 */
8754 static void
8755f_and(argvars, rettv)
8756 typval_T *argvars;
8757 typval_T *rettv;
8758{
8759 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8760 & get_tv_number_chk(&argvars[1], NULL);
8761}
8762
8763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008764 * "append(lnum, string/list)" function
8765 */
8766 static void
8767f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 typval_T *argvars;
8769 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008770{
8771 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 list_T *l = NULL;
8774 listitem_T *li = NULL;
8775 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 long added = 0;
8777
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008778 /* When coming here from Insert mode, sync undo, so that this can be
8779 * undone separately from what was previously inserted. */
8780 if (u_sync_once == 2)
8781 {
8782 u_sync_once = 1; /* notify that u_sync() was called */
8783 u_sync(TRUE);
8784 }
8785
Bram Moolenaar0d660222005-01-07 21:51:51 +00008786 lnum = get_tv_lnum(argvars);
8787 if (lnum >= 0
8788 && lnum <= curbuf->b_ml.ml_line_count
8789 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008790 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008791 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008792 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008793 l = argvars[1].vval.v_list;
8794 if (l == NULL)
8795 return;
8796 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008797 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008798 for (;;)
8799 {
8800 if (l == NULL)
8801 tv = &argvars[1]; /* append a string */
8802 else if (li == NULL)
8803 break; /* end of list */
8804 else
8805 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 line = get_tv_string_chk(tv);
8807 if (line == NULL) /* type error */
8808 {
8809 rettv->vval.v_number = 1; /* Failed */
8810 break;
8811 }
8812 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008813 ++added;
8814 if (l == NULL)
8815 break;
8816 li = li->li_next;
8817 }
8818
8819 appended_lines_mark(lnum, added);
8820 if (curwin->w_cursor.lnum > lnum)
8821 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823 else
8824 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827/*
8828 * "argc()" function
8829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008832 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838/*
8839 * "argidx()" function
8840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008843 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847}
8848
8849/*
8850 * "argv(nr)" function
8851 */
8852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 typval_T *argvars;
8855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856{
8857 int idx;
8858
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008859 if (argvars[0].v_type != VAR_UNKNOWN)
8860 {
8861 idx = get_tv_number_chk(&argvars[0], NULL);
8862 if (idx >= 0 && idx < ARGCOUNT)
8863 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8864 else
8865 rettv->vval.v_string = NULL;
8866 rettv->v_type = VAR_STRING;
8867 }
8868 else if (rettv_list_alloc(rettv) == OK)
8869 for (idx = 0; idx < ARGCOUNT; ++idx)
8870 list_append_string(rettv->vval.v_list,
8871 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872}
8873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008875/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008877 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008878 static void
8879f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008882{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008883 float_T f;
8884
8885 rettv->v_type = VAR_FLOAT;
8886 if (get_float_arg(argvars, &f) == OK)
8887 rettv->vval.v_float = asin(f);
8888 else
8889 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008890}
8891
8892/*
8893 * "atan()" function
8894 */
8895 static void
8896f_atan(argvars, rettv)
8897 typval_T *argvars;
8898 typval_T *rettv;
8899{
8900 float_T f;
8901
8902 rettv->v_type = VAR_FLOAT;
8903 if (get_float_arg(argvars, &f) == OK)
8904 rettv->vval.v_float = atan(f);
8905 else
8906 rettv->vval.v_float = 0.0;
8907}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008908
8909/*
8910 * "atan2()" function
8911 */
8912 static void
8913f_atan2(argvars, rettv)
8914 typval_T *argvars;
8915 typval_T *rettv;
8916{
8917 float_T fx, fy;
8918
8919 rettv->v_type = VAR_FLOAT;
8920 if (get_float_arg(argvars, &fx) == OK
8921 && get_float_arg(&argvars[1], &fy) == OK)
8922 rettv->vval.v_float = atan2(fx, fy);
8923 else
8924 rettv->vval.v_float = 0.0;
8925}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008926#endif
8927
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928/*
8929 * "browse(save, title, initdir, default)" function
8930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936#ifdef FEAT_BROWSE
8937 int save;
8938 char_u *title;
8939 char_u *initdir;
8940 char_u *defname;
8941 char_u buf[NUMBUFLEN];
8942 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 save = get_tv_number_chk(&argvars[0], &error);
8946 title = get_tv_string_chk(&argvars[1]);
8947 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8948 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 if (error || title == NULL || initdir == NULL || defname == NULL)
8951 rettv->vval.v_string = NULL;
8952 else
8953 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008954 do_browse(save ? BROWSE_SAVE : 0,
8955 title, defname, NULL, initdir, NULL, curbuf);
8956#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008958#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008960}
8961
8962/*
8963 * "browsedir(title, initdir)" function
8964 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008967 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008969{
8970#ifdef FEAT_BROWSE
8971 char_u *title;
8972 char_u *initdir;
8973 char_u buf[NUMBUFLEN];
8974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008975 title = get_tv_string_chk(&argvars[0]);
8976 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008978 if (title == NULL || initdir == NULL)
8979 rettv->vval.v_string = NULL;
8980 else
8981 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008982 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
Bram Moolenaar33570922005-01-25 22:26:29 +00008989static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008990
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991/*
8992 * Find a buffer by number or exact name.
8993 */
8994 static buf_T *
8995find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 if (avar->v_type == VAR_NUMBER)
9001 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009002 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009004 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009005 if (buf == NULL)
9006 {
9007 /* No full path name match, try a match with a URL or a "nofile"
9008 * buffer, these don't use the full path. */
9009 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9010 if (buf->b_fname != NULL
9011 && (path_with_url(buf->b_fname)
9012#ifdef FEAT_QUICKFIX
9013 || bt_nofile(buf)
9014#endif
9015 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009017 break;
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 }
9020 return buf;
9021}
9022
9023/*
9024 * "bufexists(expr)" function
9025 */
9026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009028 typval_T *argvars;
9029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032}
9033
9034/*
9035 * "buflisted(expr)" function
9036 */
9037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009038f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 typval_T *argvars;
9040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 buf_T *buf;
9043
9044 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046}
9047
9048/*
9049 * "bufloaded(expr)" function
9050 */
9051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009053 typval_T *argvars;
9054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 buf_T *buf;
9057
9058 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060}
9061
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009062static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064/*
9065 * Get buffer by number or pattern.
9066 */
9067 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009068get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009070 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 int save_magic;
9074 char_u *save_cpo;
9075 buf_T *buf;
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 if (tv->v_type == VAR_NUMBER)
9078 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009079 if (tv->v_type != VAR_STRING)
9080 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 if (name == NULL || *name == NUL)
9082 return curbuf;
9083 if (name[0] == '$' && name[1] == NUL)
9084 return lastbuf;
9085
9086 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9087 save_magic = p_magic;
9088 p_magic = TRUE;
9089 save_cpo = p_cpo;
9090 p_cpo = (char_u *)"";
9091
9092 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009093 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
9095 p_magic = save_magic;
9096 p_cpo = save_cpo;
9097
9098 /* If not found, try expanding the name, like done for bufexists(). */
9099 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
9102 return buf;
9103}
9104
9105/*
9106 * "bufname(expr)" function
9107 */
9108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *argvars;
9111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113 buf_T *buf;
9114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009117 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 --emsg_off;
9124}
9125
9126/*
9127 * "bufnr(expr)" function
9128 */
9129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009135 int error = FALSE;
9136 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009140 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009141 --emsg_off;
9142
9143 /* If the buffer isn't found and the second argument is not zero create a
9144 * new buffer. */
9145 if (buf == NULL
9146 && argvars[1].v_type != VAR_UNKNOWN
9147 && get_tv_number_chk(&argvars[1], &error) != 0
9148 && !error
9149 && (name = get_tv_string_chk(&argvars[0])) != NULL
9150 && !error)
9151 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
9159/*
9160 * "bufwinnr(nr)" function
9161 */
9162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166{
9167#ifdef FEAT_WINDOWS
9168 win_T *wp;
9169 int winnr = 0;
9170#endif
9171 buf_T *buf;
9172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009173 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009175 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176#ifdef FEAT_WINDOWS
9177 for (wp = firstwin; wp; wp = wp->w_next)
9178 {
9179 ++winnr;
9180 if (wp->w_buffer == buf)
9181 break;
9182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186#endif
9187 --emsg_off;
9188}
9189
9190/*
9191 * "byte2line(byte)" function
9192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
9198#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200#else
9201 long boff = 0;
9202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 (linenr_T)0, &boff);
9209#endif
9210}
9211
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009212 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009213byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009214 typval_T *argvars;
9215 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009216 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009217{
9218#ifdef FEAT_MBYTE
9219 char_u *t;
9220#endif
9221 char_u *str;
9222 long idx;
9223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 str = get_tv_string_chk(&argvars[0]);
9225 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009226 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009227 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009228 return;
9229
9230#ifdef FEAT_MBYTE
9231 t = str;
9232 for ( ; idx > 0; idx--)
9233 {
9234 if (*t == NUL) /* EOL reached */
9235 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009236 if (enc_utf8 && comp)
9237 t += utf_ptr2len(t);
9238 else
9239 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009241 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009242#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009243 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245#endif
9246}
9247
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009248/*
9249 * "byteidx()" function
9250 */
9251 static void
9252f_byteidx(argvars, rettv)
9253 typval_T *argvars;
9254 typval_T *rettv;
9255{
9256 byteidx(argvars, rettv, FALSE);
9257}
9258
9259/*
9260 * "byteidxcomp()" function
9261 */
9262 static void
9263f_byteidxcomp(argvars, rettv)
9264 typval_T *argvars;
9265 typval_T *rettv;
9266{
9267 byteidx(argvars, rettv, TRUE);
9268}
9269
Bram Moolenaardb913952012-06-29 12:54:53 +02009270 int
9271func_call(name, args, selfdict, rettv)
9272 char_u *name;
9273 typval_T *args;
9274 dict_T *selfdict;
9275 typval_T *rettv;
9276{
9277 listitem_T *item;
9278 typval_T argv[MAX_FUNC_ARGS + 1];
9279 int argc = 0;
9280 int dummy;
9281 int r = 0;
9282
9283 for (item = args->vval.v_list->lv_first; item != NULL;
9284 item = item->li_next)
9285 {
9286 if (argc == MAX_FUNC_ARGS)
9287 {
9288 EMSG(_("E699: Too many arguments"));
9289 break;
9290 }
9291 /* Make a copy of each argument. This is needed to be able to set
9292 * v_lock to VAR_FIXED in the copy without changing the original list.
9293 */
9294 copy_tv(&item->li_tv, &argv[argc++]);
9295 }
9296
9297 if (item == NULL)
9298 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9299 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9300 &dummy, TRUE, selfdict);
9301
9302 /* Free the arguments. */
9303 while (argc > 0)
9304 clear_tv(&argv[--argc]);
9305
9306 return r;
9307}
9308
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009309/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009310 * "call(func, arglist)" function
9311 */
9312 static void
9313f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 typval_T *argvars;
9315 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009316{
9317 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009318 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009320 if (argvars[1].v_type != VAR_LIST)
9321 {
9322 EMSG(_(e_listreq));
9323 return;
9324 }
9325 if (argvars[1].vval.v_list == NULL)
9326 return;
9327
9328 if (argvars[0].v_type == VAR_FUNC)
9329 func = argvars[0].vval.v_string;
9330 else
9331 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 if (*func == NUL)
9333 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009334
Bram Moolenaare9a41262005-01-15 22:18:47 +00009335 if (argvars[2].v_type != VAR_UNKNOWN)
9336 {
9337 if (argvars[2].v_type != VAR_DICT)
9338 {
9339 EMSG(_(e_dictreq));
9340 return;
9341 }
9342 selfdict = argvars[2].vval.v_dict;
9343 }
9344
Bram Moolenaardb913952012-06-29 12:54:53 +02009345 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009346}
9347
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009348#ifdef FEAT_FLOAT
9349/*
9350 * "ceil({float})" function
9351 */
9352 static void
9353f_ceil(argvars, rettv)
9354 typval_T *argvars;
9355 typval_T *rettv;
9356{
9357 float_T f;
9358
9359 rettv->v_type = VAR_FLOAT;
9360 if (get_float_arg(argvars, &f) == OK)
9361 rettv->vval.v_float = ceil(f);
9362 else
9363 rettv->vval.v_float = 0.0;
9364}
9365#endif
9366
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009367/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009368 * "changenr()" function
9369 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009370 static void
9371f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009372 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009373 typval_T *rettv;
9374{
9375 rettv->vval.v_number = curbuf->b_u_seq_cur;
9376}
9377
9378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 * "char2nr(string)" function
9380 */
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386#ifdef FEAT_MBYTE
9387 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009388 {
9389 int utf8 = 0;
9390
9391 if (argvars[1].v_type != VAR_UNKNOWN)
9392 utf8 = get_tv_number_chk(&argvars[1], NULL);
9393
9394 if (utf8)
9395 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9396 else
9397 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 else
9400#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402}
9403
9404/*
9405 * "cindent(lnum)" function
9406 */
9407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
9412#ifdef FEAT_CINDENT
9413 pos_T pos;
9414 linenr_T lnum;
9415
9416 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9419 {
9420 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 curwin->w_cursor = pos;
9423 }
9424 else
9425#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427}
9428
9429/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009430 * "clearmatches()" function
9431 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009432 static void
9433f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009434 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009435 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009436{
9437#ifdef FEAT_SEARCH_EXTRA
9438 clear_matches(curwin);
9439#endif
9440}
9441
9442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 * "col(string)" function
9444 */
9445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009446f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009447 typval_T *argvars;
9448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 colnr_T col = 0;
9451 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009454 fp = var2fpos(&argvars[0], FALSE, &fnum);
9455 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 {
9457 if (fp->col == MAXCOL)
9458 {
9459 /* '> can be MAXCOL, get the length of the line then */
9460 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009461 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 else
9463 col = MAXCOL;
9464 }
9465 else
9466 {
9467 col = fp->col + 1;
9468#ifdef FEAT_VIRTUALEDIT
9469 /* col(".") when the cursor is on the NUL at the end of the line
9470 * because of "coladd" can be seen as an extra column. */
9471 if (virtual_active() && fp == &curwin->w_cursor)
9472 {
9473 char_u *p = ml_get_cursor();
9474
9475 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9476 curwin->w_virtcol - curwin->w_cursor.coladd))
9477 {
9478# ifdef FEAT_MBYTE
9479 int l;
9480
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009481 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 col += l;
9483# else
9484 if (*p != NUL && p[1] == NUL)
9485 ++col;
9486# endif
9487 }
9488 }
9489#endif
9490 }
9491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493}
9494
Bram Moolenaar572cb562005-08-05 21:35:02 +00009495#if defined(FEAT_INS_EXPAND)
9496/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009497 * "complete()" function
9498 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009499 static void
9500f_complete(argvars, rettv)
9501 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009502 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009503{
9504 int startcol;
9505
9506 if ((State & INSERT) == 0)
9507 {
9508 EMSG(_("E785: complete() can only be used in Insert mode"));
9509 return;
9510 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009511
9512 /* Check for undo allowed here, because if something was already inserted
9513 * the line was already saved for undo and this check isn't done. */
9514 if (!undo_allowed())
9515 return;
9516
Bram Moolenaarade00832006-03-10 21:46:58 +00009517 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9518 {
9519 EMSG(_(e_invarg));
9520 return;
9521 }
9522
9523 startcol = get_tv_number_chk(&argvars[0], NULL);
9524 if (startcol <= 0)
9525 return;
9526
9527 set_completion(startcol - 1, argvars[1].vval.v_list);
9528}
9529
9530/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009531 * "complete_add()" function
9532 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009533 static void
9534f_complete_add(argvars, rettv)
9535 typval_T *argvars;
9536 typval_T *rettv;
9537{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009538 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009539}
9540
9541/*
9542 * "complete_check()" function
9543 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009544 static void
9545f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009546 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009547 typval_T *rettv;
9548{
9549 int saved = RedrawingDisabled;
9550
9551 RedrawingDisabled = 0;
9552 ins_compl_check_keys(0);
9553 rettv->vval.v_number = compl_interrupted;
9554 RedrawingDisabled = saved;
9555}
9556#endif
9557
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558/*
9559 * "confirm(message, buttons[, default [, type]])" function
9560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009563 typval_T *argvars UNUSED;
9564 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9567 char_u *message;
9568 char_u *buttons = NULL;
9569 char_u buf[NUMBUFLEN];
9570 char_u buf2[NUMBUFLEN];
9571 int def = 1;
9572 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 char_u *typestr;
9574 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 message = get_tv_string_chk(&argvars[0]);
9577 if (message == NULL)
9578 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009579 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9582 if (buttons == NULL)
9583 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009586 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9590 if (typestr == NULL)
9591 error = TRUE;
9592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 switch (TOUPPER_ASC(*typestr))
9595 {
9596 case 'E': type = VIM_ERROR; break;
9597 case 'Q': type = VIM_QUESTION; break;
9598 case 'I': type = VIM_INFO; break;
9599 case 'W': type = VIM_WARNING; break;
9600 case 'G': type = VIM_GENERIC; break;
9601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 }
9604 }
9605 }
9606
9607 if (buttons == NULL || *buttons == NUL)
9608 buttons = (char_u *)_("&Ok");
9609
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009610 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009612 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613#endif
9614}
9615
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009616/*
9617 * "copy()" function
9618 */
9619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 typval_T *argvars;
9622 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009624 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009627#ifdef FEAT_FLOAT
9628/*
9629 * "cos()" function
9630 */
9631 static void
9632f_cos(argvars, rettv)
9633 typval_T *argvars;
9634 typval_T *rettv;
9635{
9636 float_T f;
9637
9638 rettv->v_type = VAR_FLOAT;
9639 if (get_float_arg(argvars, &f) == OK)
9640 rettv->vval.v_float = cos(f);
9641 else
9642 rettv->vval.v_float = 0.0;
9643}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009644
9645/*
9646 * "cosh()" function
9647 */
9648 static void
9649f_cosh(argvars, rettv)
9650 typval_T *argvars;
9651 typval_T *rettv;
9652{
9653 float_T f;
9654
9655 rettv->v_type = VAR_FLOAT;
9656 if (get_float_arg(argvars, &f) == OK)
9657 rettv->vval.v_float = cosh(f);
9658 else
9659 rettv->vval.v_float = 0.0;
9660}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009661#endif
9662
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009664 * "count()" function
9665 */
9666 static void
9667f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009668 typval_T *argvars;
9669 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009670{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009671 long n = 0;
9672 int ic = FALSE;
9673
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009675 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009676 listitem_T *li;
9677 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009679
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 if ((l = argvars[0].vval.v_list) != NULL)
9681 {
9682 li = l->lv_first;
9683 if (argvars[2].v_type != VAR_UNKNOWN)
9684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 int error = FALSE;
9686
9687 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009688 if (argvars[3].v_type != VAR_UNKNOWN)
9689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 idx = get_tv_number_chk(&argvars[3], &error);
9691 if (!error)
9692 {
9693 li = list_find(l, idx);
9694 if (li == NULL)
9695 EMSGN(_(e_listidx), idx);
9696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009698 if (error)
9699 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 }
9701
9702 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009703 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009704 ++n;
9705 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 else if (argvars[0].v_type == VAR_DICT)
9708 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 int todo;
9710 dict_T *d;
9711 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009712
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009713 if ((d = argvars[0].vval.v_dict) != NULL)
9714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 int error = FALSE;
9716
Bram Moolenaare9a41262005-01-15 22:18:47 +00009717 if (argvars[2].v_type != VAR_UNKNOWN)
9718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009719 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009720 if (argvars[3].v_type != VAR_UNKNOWN)
9721 EMSG(_(e_invarg));
9722 }
9723
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009724 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009725 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009726 {
9727 if (!HASHITEM_EMPTY(hi))
9728 {
9729 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009730 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009731 ++n;
9732 }
9733 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009734 }
9735 }
9736 else
9737 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009738 rettv->vval.v_number = n;
9739}
9740
9741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9743 *
9744 * Checks the existence of a cscope connection.
9745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009748 typval_T *argvars UNUSED;
9749 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751#ifdef FEAT_CSCOPE
9752 int num = 0;
9753 char_u *dbpath = NULL;
9754 char_u *prepend = NULL;
9755 char_u buf[NUMBUFLEN];
9756
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009757 if (argvars[0].v_type != VAR_UNKNOWN
9758 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 num = (int)get_tv_number(&argvars[0]);
9761 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009762 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 }
9765
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#endif
9768}
9769
9770/*
9771 * "cursor(lnum, col)" function
9772 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009773 * Moves the cursor to the specified line and column.
9774 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009778 typval_T *argvars;
9779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
9781 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009782#ifdef FEAT_VIRTUALEDIT
9783 long coladd = 0;
9784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009786 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009787 if (argvars[1].v_type == VAR_UNKNOWN)
9788 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009789 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009790
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009791 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009792 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009793 line = pos.lnum;
9794 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009795#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009796 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009797#endif
9798 }
9799 else
9800 {
9801 line = get_tv_lnum(argvars);
9802 col = get_tv_number_chk(&argvars[1], NULL);
9803#ifdef FEAT_VIRTUALEDIT
9804 if (argvars[2].v_type != VAR_UNKNOWN)
9805 coladd = get_tv_number_chk(&argvars[2], NULL);
9806#endif
9807 }
9808 if (line < 0 || col < 0
9809#ifdef FEAT_VIRTUALEDIT
9810 || coladd < 0
9811#endif
9812 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (line > 0)
9815 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 if (col > 0)
9817 curwin->w_cursor.col = col - 1;
9818#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009819 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820#endif
9821
9822 /* Make sure the cursor is in a valid position. */
9823 check_cursor();
9824#ifdef FEAT_MBYTE
9825 /* Correct cursor for multi-byte character. */
9826 if (has_mbyte)
9827 mb_adjust_cursor();
9828#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009829
9830 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009831 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832}
9833
9834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009835 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 */
9837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009839 typval_T *argvars;
9840 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009842 int noref = 0;
9843
9844 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009845 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009846 if (noref < 0 || noref > 1)
9847 EMSG(_(e_invarg));
9848 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009849 {
9850 current_copyID += COPYID_INC;
9851 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853}
9854
9855/*
9856 * "delete()" function
9857 */
9858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009860 typval_T *argvars;
9861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862{
9863 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869/*
9870 * "did_filetype()" function
9871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009873f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009874 typval_T *argvars UNUSED;
9875 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#endif
9880}
9881
9882/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 * "diff_filler()" function
9884 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009887 typval_T *argvars UNUSED;
9888 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009889{
9890#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009891 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892#endif
9893}
9894
9895/*
9896 * "diff_hlID()" function
9897 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009900 typval_T *argvars UNUSED;
9901 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009902{
9903#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009904 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009905 static linenr_T prev_lnum = 0;
9906 static int changedtick = 0;
9907 static int fnum = 0;
9908 static int change_start = 0;
9909 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009910 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009911 int filler_lines;
9912 int col;
9913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 if (lnum < 0) /* ignore type error in {lnum} arg */
9915 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009916 if (lnum != prev_lnum
9917 || changedtick != curbuf->b_changedtick
9918 || fnum != curbuf->b_fnum)
9919 {
9920 /* New line, buffer, change: need to get the values. */
9921 filler_lines = diff_check(curwin, lnum);
9922 if (filler_lines < 0)
9923 {
9924 if (filler_lines == -1)
9925 {
9926 change_start = MAXCOL;
9927 change_end = -1;
9928 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9929 hlID = HLF_ADD; /* added line */
9930 else
9931 hlID = HLF_CHD; /* changed line */
9932 }
9933 else
9934 hlID = HLF_ADD; /* added line */
9935 }
9936 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009937 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009938 prev_lnum = lnum;
9939 changedtick = curbuf->b_changedtick;
9940 fnum = curbuf->b_fnum;
9941 }
9942
9943 if (hlID == HLF_CHD || hlID == HLF_TXD)
9944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009946 if (col >= change_start && col <= change_end)
9947 hlID = HLF_TXD; /* changed text */
9948 else
9949 hlID = HLF_CHD; /* changed line */
9950 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009951 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009952#endif
9953}
9954
9955/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009956 * "empty({expr})" function
9957 */
9958 static void
9959f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009960 typval_T *argvars;
9961 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009962{
9963 int n;
9964
9965 switch (argvars[0].v_type)
9966 {
9967 case VAR_STRING:
9968 case VAR_FUNC:
9969 n = argvars[0].vval.v_string == NULL
9970 || *argvars[0].vval.v_string == NUL;
9971 break;
9972 case VAR_NUMBER:
9973 n = argvars[0].vval.v_number == 0;
9974 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009975#ifdef FEAT_FLOAT
9976 case VAR_FLOAT:
9977 n = argvars[0].vval.v_float == 0.0;
9978 break;
9979#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009980 case VAR_LIST:
9981 n = argvars[0].vval.v_list == NULL
9982 || argvars[0].vval.v_list->lv_first == NULL;
9983 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009984 case VAR_DICT:
9985 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009988 default:
9989 EMSG2(_(e_intern2), "f_empty()");
9990 n = 0;
9991 }
9992
9993 rettv->vval.v_number = n;
9994}
9995
9996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 * "escape({string}, {chars})" function
9998 */
9999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010001 typval_T *argvars;
10002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 char_u buf[NUMBUFLEN];
10005
Bram Moolenaar758711c2005-02-02 23:11:38 +000010006 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10007 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010008 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009}
10010
10011/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010012 * "eval()" function
10013 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010014 static void
10015f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010016 typval_T *argvars;
10017 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010018{
10019 char_u *s;
10020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010021 s = get_tv_string_chk(&argvars[0]);
10022 if (s != NULL)
10023 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10026 {
10027 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010028 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010029 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010030 else if (*s != NUL)
10031 EMSG(_(e_trailing));
10032}
10033
10034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 * "eventhandler()" function
10036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
10045/*
10046 * "executable()" function
10047 */
10048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010053 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10054}
10055
10056/*
10057 * "exepath()" function
10058 */
10059 static void
10060f_exepath(argvars, rettv)
10061 typval_T *argvars;
10062 typval_T *rettv;
10063{
10064 char_u *p = NULL;
10065
10066 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10067 rettv->v_type = VAR_STRING;
10068 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
10071/*
10072 * "exists()" function
10073 */
10074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010076 typval_T *argvars;
10077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
10079 char_u *p;
10080 char_u *name;
10081 int n = FALSE;
10082 int len = 0;
10083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (*p == '$') /* environment variable */
10086 {
10087 /* first try "normal" environment variables (fast) */
10088 if (mch_getenv(p + 1) != NULL)
10089 n = TRUE;
10090 else
10091 {
10092 /* try expanding things like $VIM and ${HOME} */
10093 p = expand_env_save(p);
10094 if (p != NULL && *p != '$')
10095 n = TRUE;
10096 vim_free(p);
10097 }
10098 }
10099 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010102 if (*skipwhite(p) != NUL)
10103 n = FALSE; /* trailing garbage */
10104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 else if (*p == '*') /* internal or user defined function */
10106 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010107 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 }
10109 else if (*p == ':')
10110 {
10111 n = cmd_exists(p + 1);
10112 }
10113 else if (*p == '#')
10114 {
10115#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010116 if (p[1] == '#')
10117 n = autocmd_supported(p + 2);
10118 else
10119 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#endif
10121 }
10122 else /* internal variable */
10123 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010124 char_u *tofree;
10125 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010127 /* get_name_len() takes care of expanding curly braces */
10128 name = p;
10129 len = get_name_len(&p, &tofree, TRUE, FALSE);
10130 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010132 if (tofree != NULL)
10133 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010134 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010135 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010137 /* handle d.key, l[idx], f(expr) */
10138 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10139 if (n)
10140 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010143 if (*p != NUL)
10144 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010146 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 }
10148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150}
10151
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010152#ifdef FEAT_FLOAT
10153/*
10154 * "exp()" function
10155 */
10156 static void
10157f_exp(argvars, rettv)
10158 typval_T *argvars;
10159 typval_T *rettv;
10160{
10161 float_T f;
10162
10163 rettv->v_type = VAR_FLOAT;
10164 if (get_float_arg(argvars, &f) == OK)
10165 rettv->vval.v_float = exp(f);
10166 else
10167 rettv->vval.v_float = 0.0;
10168}
10169#endif
10170
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171/*
10172 * "expand()" function
10173 */
10174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010175f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010176 typval_T *argvars;
10177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178{
10179 char_u *s;
10180 int len;
10181 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010182 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010185 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010188 if (argvars[1].v_type != VAR_UNKNOWN
10189 && argvars[2].v_type != VAR_UNKNOWN
10190 && get_tv_number_chk(&argvars[2], &error)
10191 && !error)
10192 {
10193 rettv->v_type = VAR_LIST;
10194 rettv->vval.v_list = NULL;
10195 }
10196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 if (*s == '%' || *s == '#' || *s == '<')
10199 {
10200 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010203 if (rettv->v_type == VAR_LIST)
10204 {
10205 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10206 list_append_string(rettv->vval.v_list, result, -1);
10207 else
10208 vim_free(result);
10209 }
10210 else
10211 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 }
10213 else
10214 {
10215 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010216 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 if (argvars[1].v_type != VAR_UNKNOWN
10218 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010219 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010220 if (!error)
10221 {
10222 ExpandInit(&xpc);
10223 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010224 if (p_wic)
10225 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010226 if (rettv->v_type == VAR_STRING)
10227 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10228 options, WILD_ALL);
10229 else if (rettv_list_alloc(rettv) != FAIL)
10230 {
10231 int i;
10232
10233 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10234 for (i = 0; i < xpc.xp_numfiles; i++)
10235 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10236 ExpandCleanup(&xpc);
10237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 }
10239 else
10240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 }
10242}
10243
10244/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010245 * Go over all entries in "d2" and add them to "d1".
10246 * When "action" is "error" then a duplicate key is an error.
10247 * When "action" is "force" then a duplicate key is overwritten.
10248 * Otherwise duplicate keys are ignored ("action" is "keep").
10249 */
10250 void
10251dict_extend(d1, d2, action)
10252 dict_T *d1;
10253 dict_T *d2;
10254 char_u *action;
10255{
10256 dictitem_T *di1;
10257 hashitem_T *hi2;
10258 int todo;
10259
10260 todo = (int)d2->dv_hashtab.ht_used;
10261 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10262 {
10263 if (!HASHITEM_EMPTY(hi2))
10264 {
10265 --todo;
10266 di1 = dict_find(d1, hi2->hi_key, -1);
10267 if (d1->dv_scope != 0)
10268 {
10269 /* Disallow replacing a builtin function in l: and g:.
10270 * Check the key to be valid when adding to any
10271 * scope. */
10272 if (d1->dv_scope == VAR_DEF_SCOPE
10273 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10274 && var_check_func_name(hi2->hi_key,
10275 di1 == NULL))
10276 break;
10277 if (!valid_varname(hi2->hi_key))
10278 break;
10279 }
10280 if (di1 == NULL)
10281 {
10282 di1 = dictitem_copy(HI2DI(hi2));
10283 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10284 dictitem_free(di1);
10285 }
10286 else if (*action == 'e')
10287 {
10288 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10289 break;
10290 }
10291 else if (*action == 'f' && HI2DI(hi2) != di1)
10292 {
10293 clear_tv(&di1->di_tv);
10294 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10295 }
10296 }
10297 }
10298}
10299
10300/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010301 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010302 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010303 */
10304 static void
10305f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010306 typval_T *argvars;
10307 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010308{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010309 char *arg_errmsg = N_("extend() argument");
10310
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010312 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010313 list_T *l1, *l2;
10314 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010317
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 l1 = argvars[0].vval.v_list;
10319 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010320 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010321 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 {
10323 if (argvars[2].v_type != VAR_UNKNOWN)
10324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 before = get_tv_number_chk(&argvars[2], &error);
10326 if (error)
10327 return; /* type error; errmsg already given */
10328
Bram Moolenaar758711c2005-02-02 23:11:38 +000010329 if (before == l1->lv_len)
10330 item = NULL;
10331 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010333 item = list_find(l1, before);
10334 if (item == NULL)
10335 {
10336 EMSGN(_(e_listidx), before);
10337 return;
10338 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010339 }
10340 }
10341 else
10342 item = NULL;
10343 list_extend(l1, l2, item);
10344
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 copy_tv(&argvars[0], rettv);
10346 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010347 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10349 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010350 dict_T *d1, *d2;
10351 char_u *action;
10352 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010353
10354 d1 = argvars[0].vval.v_dict;
10355 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010356 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010357 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010358 {
10359 /* Check the third argument. */
10360 if (argvars[2].v_type != VAR_UNKNOWN)
10361 {
10362 static char *(av[]) = {"keep", "force", "error"};
10363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 action = get_tv_string_chk(&argvars[2]);
10365 if (action == NULL)
10366 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010367 for (i = 0; i < 3; ++i)
10368 if (STRCMP(action, av[i]) == 0)
10369 break;
10370 if (i == 3)
10371 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010372 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010373 return;
10374 }
10375 }
10376 else
10377 action = (char_u *)"force";
10378
Bram Moolenaara9922d62013-05-30 13:01:18 +020010379 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010380
Bram Moolenaare9a41262005-01-15 22:18:47 +000010381 copy_tv(&argvars[0], rettv);
10382 }
10383 }
10384 else
10385 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010386}
10387
10388/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010389 * "feedkeys()" function
10390 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010391 static void
10392f_feedkeys(argvars, rettv)
10393 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010394 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010395{
10396 int remap = TRUE;
10397 char_u *keys, *flags;
10398 char_u nbuf[NUMBUFLEN];
10399 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010400 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010401
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010402 /* This is not allowed in the sandbox. If the commands would still be
10403 * executed in the sandbox it would be OK, but it probably happens later,
10404 * when "sandbox" is no longer set. */
10405 if (check_secure())
10406 return;
10407
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010408 keys = get_tv_string(&argvars[0]);
10409 if (*keys != NUL)
10410 {
10411 if (argvars[1].v_type != VAR_UNKNOWN)
10412 {
10413 flags = get_tv_string_buf(&argvars[1], nbuf);
10414 for ( ; *flags != NUL; ++flags)
10415 {
10416 switch (*flags)
10417 {
10418 case 'n': remap = FALSE; break;
10419 case 'm': remap = TRUE; break;
10420 case 't': typed = TRUE; break;
10421 }
10422 }
10423 }
10424
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010425 /* Need to escape K_SPECIAL and CSI before putting the string in the
10426 * typeahead buffer. */
10427 keys_esc = vim_strsave_escape_csi(keys);
10428 if (keys_esc != NULL)
10429 {
10430 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010431 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010432 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010433 if (vgetc_busy)
10434 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010435 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010436 }
10437}
10438
10439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 * "filereadable()" function
10441 */
10442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010443f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010444 typval_T *argvars;
10445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010447 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 char_u *p;
10449 int n;
10450
Bram Moolenaarc236c162008-07-13 17:41:49 +000010451#ifndef O_NONBLOCK
10452# define O_NONBLOCK 0
10453#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010455 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10456 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 {
10458 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010459 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 }
10461 else
10462 n = FALSE;
10463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465}
10466
10467/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010468 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 * rights to write into.
10470 */
10471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 typval_T *argvars;
10474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010476 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010479static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480
10481 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010482findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010483 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010485 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010486{
10487#ifdef FEAT_SEARCHPATH
10488 char_u *fname;
10489 char_u *fresult = NULL;
10490 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10491 char_u *p;
10492 char_u pathbuf[NUMBUFLEN];
10493 int count = 1;
10494 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010495 int error = FALSE;
10496#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010497
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010498 rettv->vval.v_string = NULL;
10499 rettv->v_type = VAR_STRING;
10500
10501#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010503
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10507 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010508 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 else
10510 {
10511 if (*p != NUL)
10512 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010515 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517 }
10518
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010519 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10520 error = TRUE;
10521
10522 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 do
10525 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010526 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010527 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 fresult = find_file_in_path_option(first ? fname : NULL,
10529 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010530 0, first, path,
10531 find_what,
10532 curbuf->b_ffname,
10533 find_what == FINDFILE_DIR
10534 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010536
10537 if (fresult != NULL && rettv->v_type == VAR_LIST)
10538 list_append_string(rettv->vval.v_list, fresult, -1);
10539
10540 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010542
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010543 if (rettv->v_type == VAR_STRING)
10544 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010545#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010546}
10547
Bram Moolenaar33570922005-01-25 22:26:29 +000010548static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10549static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010550
10551/*
10552 * Implementation of map() and filter().
10553 */
10554 static void
10555filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010556 typval_T *argvars;
10557 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010558 int map;
10559{
10560 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010562 listitem_T *li, *nli;
10563 list_T *l = NULL;
10564 dictitem_T *di;
10565 hashtab_T *ht;
10566 hashitem_T *hi;
10567 dict_T *d = NULL;
10568 typval_T save_val;
10569 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010571 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010572 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10573 char *arg_errmsg = (map ? N_("map() argument")
10574 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010575 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010576 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010580 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010581 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 return;
10583 }
10584 else if (argvars[0].v_type == VAR_DICT)
10585 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010586 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010587 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 return;
10589 }
10590 else
10591 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010592 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 return;
10594 }
10595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010596 expr = get_tv_string_buf_chk(&argvars[1], buf);
10597 /* On type errors, the preceding call has already displayed an error
10598 * message. Avoid a misleading error message for an empty string that
10599 * was not passed as argument. */
10600 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 prepare_vimvar(VV_VAL, &save_val);
10603 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010604
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010605 /* We reset "did_emsg" to be able to detect whether an error
10606 * occurred during evaluation of the expression. */
10607 save_did_emsg = did_emsg;
10608 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010609
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010610 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 vimvars[VV_KEY].vv_type = VAR_STRING;
10614
10615 ht = &d->dv_hashtab;
10616 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010617 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 if (!HASHITEM_EMPTY(hi))
10621 {
10622 --todo;
10623 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010624 if (tv_check_lock(di->di_tv.v_lock,
10625 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 break;
10627 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010628 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010629 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 break;
10631 if (!map && rem)
10632 dictitem_remove(d, di);
10633 clear_tv(&vimvars[VV_KEY].vv_tv);
10634 }
10635 }
10636 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010637 }
10638 else
10639 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010640 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 for (li = l->lv_first; li != NULL; li = nli)
10643 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010644 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010645 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010647 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010648 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010649 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010650 break;
10651 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010653 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010654 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010656
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010657 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010659
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010660 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010661 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010662
10663 copy_tv(&argvars[0], rettv);
10664}
10665
10666 static int
10667filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010668 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010669 char_u *expr;
10670 int map;
10671 int *remp;
10672{
Bram Moolenaar33570922005-01-25 22:26:29 +000010673 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010675 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010676
Bram Moolenaar33570922005-01-25 22:26:29 +000010677 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 s = expr;
10679 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010680 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 if (*s != NUL) /* check for trailing chars after expr */
10682 {
10683 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010684 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010685 }
10686 if (map)
10687 {
10688 /* map(): replace the list item value */
10689 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010690 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010691 *tv = rettv;
10692 }
10693 else
10694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 int error = FALSE;
10696
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010699 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 /* On type error, nothing has been removed; return FAIL to stop the
10701 * loop. The error message was given by get_tv_number_chk(). */
10702 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010703 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010704 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010705 retval = OK;
10706theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010707 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010708 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010709}
10710
10711/*
10712 * "filter()" function
10713 */
10714 static void
10715f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010716 typval_T *argvars;
10717 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010718{
10719 filter_map(argvars, rettv, FALSE);
10720}
10721
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010723 * "finddir({fname}[, {path}[, {count}]])" function
10724 */
10725 static void
10726f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010727 typval_T *argvars;
10728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010729{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010730 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010731}
10732
10733/*
10734 * "findfile({fname}[, {path}[, {count}]])" function
10735 */
10736 static void
10737f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010738 typval_T *argvars;
10739 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010741 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010742}
10743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010744#ifdef FEAT_FLOAT
10745/*
10746 * "float2nr({float})" function
10747 */
10748 static void
10749f_float2nr(argvars, rettv)
10750 typval_T *argvars;
10751 typval_T *rettv;
10752{
10753 float_T f;
10754
10755 if (get_float_arg(argvars, &f) == OK)
10756 {
10757 if (f < -0x7fffffff)
10758 rettv->vval.v_number = -0x7fffffff;
10759 else if (f > 0x7fffffff)
10760 rettv->vval.v_number = 0x7fffffff;
10761 else
10762 rettv->vval.v_number = (varnumber_T)f;
10763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010764}
10765
10766/*
10767 * "floor({float})" function
10768 */
10769 static void
10770f_floor(argvars, rettv)
10771 typval_T *argvars;
10772 typval_T *rettv;
10773{
10774 float_T f;
10775
10776 rettv->v_type = VAR_FLOAT;
10777 if (get_float_arg(argvars, &f) == OK)
10778 rettv->vval.v_float = floor(f);
10779 else
10780 rettv->vval.v_float = 0.0;
10781}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010782
10783/*
10784 * "fmod()" function
10785 */
10786 static void
10787f_fmod(argvars, rettv)
10788 typval_T *argvars;
10789 typval_T *rettv;
10790{
10791 float_T fx, fy;
10792
10793 rettv->v_type = VAR_FLOAT;
10794 if (get_float_arg(argvars, &fx) == OK
10795 && get_float_arg(&argvars[1], &fy) == OK)
10796 rettv->vval.v_float = fmod(fx, fy);
10797 else
10798 rettv->vval.v_float = 0.0;
10799}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010800#endif
10801
Bram Moolenaar0d660222005-01-07 21:51:51 +000010802/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010803 * "fnameescape({string})" function
10804 */
10805 static void
10806f_fnameescape(argvars, rettv)
10807 typval_T *argvars;
10808 typval_T *rettv;
10809{
10810 rettv->vval.v_string = vim_strsave_fnameescape(
10811 get_tv_string(&argvars[0]), FALSE);
10812 rettv->v_type = VAR_STRING;
10813}
10814
10815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 * "fnamemodify({fname}, {mods})" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822{
10823 char_u *fname;
10824 char_u *mods;
10825 int usedlen = 0;
10826 int len;
10827 char_u *fbuf = NULL;
10828 char_u buf[NUMBUFLEN];
10829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 fname = get_tv_string_chk(&argvars[0]);
10831 mods = get_tv_string_buf_chk(&argvars[1], buf);
10832 if (fname == NULL || mods == NULL)
10833 fname = NULL;
10834 else
10835 {
10836 len = (int)STRLEN(fname);
10837 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 vim_free(fbuf);
10846}
10847
Bram Moolenaar33570922005-01-25 22:26:29 +000010848static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849
10850/*
10851 * "foldclosed()" function
10852 */
10853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010856 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010857 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859#ifdef FEAT_FOLDING
10860 linenr_T lnum;
10861 linenr_T first, last;
10862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010863 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10865 {
10866 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10867 {
10868 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 return;
10873 }
10874 }
10875#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877}
10878
10879/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010880 * "foldclosed()" function
10881 */
10882 static void
10883f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *argvars;
10885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010886{
10887 foldclosed_both(argvars, rettv, FALSE);
10888}
10889
10890/*
10891 * "foldclosedend()" function
10892 */
10893 static void
10894f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010895 typval_T *argvars;
10896 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010897{
10898 foldclosed_both(argvars, rettv, TRUE);
10899}
10900
10901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 * "foldlevel()" function
10903 */
10904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010906 typval_T *argvars UNUSED;
10907 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908{
10909#ifdef FEAT_FOLDING
10910 linenr_T lnum;
10911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916}
10917
10918/*
10919 * "foldtext()" function
10920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
10926#ifdef FEAT_FOLDING
10927 linenr_T lnum;
10928 char_u *s;
10929 char_u *r;
10930 int len;
10931 char *txt;
10932#endif
10933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 rettv->v_type = VAR_STRING;
10935 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10938 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10939 <= curbuf->b_ml.ml_line_count
10940 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 {
10942 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10944 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 {
10946 if (!linewhite(lnum))
10947 break;
10948 ++lnum;
10949 }
10950
10951 /* Find interesting text in this line. */
10952 s = skipwhite(ml_get(lnum));
10953 /* skip C comment-start */
10954 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010957 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010959 {
10960 s = skipwhite(ml_get(lnum + 1));
10961 if (*s == '*')
10962 s = skipwhite(s + 1);
10963 }
10964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 txt = _("+-%s%3ld lines: ");
10966 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 + 20 /* for %3ld */
10969 + STRLEN(s))); /* concatenated */
10970 if (r != NULL)
10971 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10973 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10974 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 len = (int)STRLEN(r);
10976 STRCAT(r, s);
10977 /* remove 'foldmarker' and 'commentstring' */
10978 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 }
10981 }
10982#endif
10983}
10984
10985/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010986 * "foldtextresult(lnum)" function
10987 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010992{
10993#ifdef FEAT_FOLDING
10994 linenr_T lnum;
10995 char_u *text;
10996 char_u buf[51];
10997 foldinfo_T foldinfo;
10998 int fold_count;
10999#endif
11000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->v_type = VAR_STRING;
11002 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011003#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 /* treat illegal types and illegal string values for {lnum} the same */
11006 if (lnum < 0)
11007 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011008 fold_count = foldedCount(curwin, lnum, &foldinfo);
11009 if (fold_count > 0)
11010 {
11011 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11012 &foldinfo, buf);
11013 if (text == buf)
11014 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011016 }
11017#endif
11018}
11019
11020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 * "foreground()" function
11022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011025 typval_T *argvars UNUSED;
11026 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028#ifdef FEAT_GUI
11029 if (gui.in_use)
11030 gui_mch_set_foreground();
11031#else
11032# ifdef WIN32
11033 win32_set_foreground();
11034# endif
11035#endif
11036}
11037
11038/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 * "function()" function
11040 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011043 typval_T *argvars;
11044 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045{
11046 char_u *s;
11047
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011049 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011050 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011051 /* Don't check an autoload name for existence here. */
11052 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011053 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011054 else
11055 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011056 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011057 {
11058 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011059 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011060
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011061 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11062 * also be called from another script. Using trans_function_name()
11063 * would also work, but some plugins depend on the name being
11064 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011065 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011066 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011067 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011068 if (rettv->vval.v_string != NULL)
11069 {
11070 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011071 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011072 }
11073 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011074 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011075 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011077 }
11078}
11079
11080/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011081 * "garbagecollect()" function
11082 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011083 static void
11084f_garbagecollect(argvars, rettv)
11085 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011086 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011087{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011088 /* This is postponed until we are back at the toplevel, because we may be
11089 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11090 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011091
11092 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11093 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011094}
11095
11096/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011097 * "get()" function
11098 */
11099 static void
11100f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011103{
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 listitem_T *li;
11105 list_T *l;
11106 dictitem_T *di;
11107 dict_T *d;
11108 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011111 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114 int error = FALSE;
11115
11116 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11117 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011121 else if (argvars[0].v_type == VAR_DICT)
11122 {
11123 if ((d = argvars[0].vval.v_dict) != NULL)
11124 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011125 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 if (di != NULL)
11127 tv = &di->di_tv;
11128 }
11129 }
11130 else
11131 EMSG2(_(e_listdictarg), "get()");
11132
11133 if (tv == NULL)
11134 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 copy_tv(&argvars[2], rettv);
11137 }
11138 else
11139 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140}
11141
Bram Moolenaar342337a2005-07-21 21:11:17 +000011142static 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 +000011143
11144/*
11145 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011146 * Return a range (from start to end) of lines in rettv from the specified
11147 * buffer.
11148 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149 */
11150 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011151get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011152 buf_T *buf;
11153 linenr_T start;
11154 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011155 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011156 typval_T *rettv;
11157{
11158 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011159
Bram Moolenaar959a1432013-12-14 12:17:38 +010011160 rettv->v_type = VAR_STRING;
11161 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011162 if (retlist && rettv_list_alloc(rettv) == FAIL)
11163 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011164
11165 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11166 return;
11167
11168 if (!retlist)
11169 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011170 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11171 p = ml_get_buf(buf, start, FALSE);
11172 else
11173 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011174 rettv->vval.v_string = vim_strsave(p);
11175 }
11176 else
11177 {
11178 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011179 return;
11180
11181 if (start < 1)
11182 start = 1;
11183 if (end > buf->b_ml.ml_line_count)
11184 end = buf->b_ml.ml_line_count;
11185 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011186 if (list_append_string(rettv->vval.v_list,
11187 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011188 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011189 }
11190}
11191
11192/*
11193 * "getbufline()" function
11194 */
11195 static void
11196f_getbufline(argvars, rettv)
11197 typval_T *argvars;
11198 typval_T *rettv;
11199{
11200 linenr_T lnum;
11201 linenr_T end;
11202 buf_T *buf;
11203
11204 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11205 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011206 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011207 --emsg_off;
11208
Bram Moolenaar661b1822005-07-28 22:36:45 +000011209 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011210 if (argvars[2].v_type == VAR_UNKNOWN)
11211 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011212 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011213 end = get_tv_lnum_buf(&argvars[2], buf);
11214
Bram Moolenaar342337a2005-07-21 21:11:17 +000011215 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011216}
11217
Bram Moolenaar0d660222005-01-07 21:51:51 +000011218/*
11219 * "getbufvar()" function
11220 */
11221 static void
11222f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011223 typval_T *argvars;
11224 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011225{
11226 buf_T *buf;
11227 buf_T *save_curbuf;
11228 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011230 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11233 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011235 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011236
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011237 rettv->v_type = VAR_STRING;
11238 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011239
11240 if (buf != NULL && varname != NULL)
11241 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011242 /* set curbuf to be our buf, temporarily */
11243 save_curbuf = curbuf;
11244 curbuf = buf;
11245
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011247 {
11248 if (get_option_tv(&varname, rettv, TRUE) == OK)
11249 done = TRUE;
11250 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011251 else if (STRCMP(varname, "changedtick") == 0)
11252 {
11253 rettv->v_type = VAR_NUMBER;
11254 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011255 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011256 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011257 else
11258 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011259 /* Look up the variable. */
11260 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11261 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11262 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011263 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011264 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011266 done = TRUE;
11267 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011268 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011269
11270 /* restore previous notion of curbuf */
11271 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011272 }
11273
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011274 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11275 /* use the default value */
11276 copy_tv(&argvars[2], rettv);
11277
Bram Moolenaar0d660222005-01-07 21:51:51 +000011278 --emsg_off;
11279}
11280
11281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 * "getchar()" function
11283 */
11284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *argvars;
11287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288{
11289 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011292 /* Position the cursor. Needed after a message that ends in a space. */
11293 windgoto(msg_row, msg_col);
11294
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 ++no_mapping;
11296 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011297 for (;;)
11298 {
11299 if (argvars[0].v_type == VAR_UNKNOWN)
11300 /* getchar(): blocking wait. */
11301 n = safe_vgetc();
11302 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11303 /* getchar(1): only check if char avail */
11304 n = vpeekc();
11305 else if (error || vpeekc() == NUL)
11306 /* illegal argument or getchar(0) and no char avail: return zero */
11307 n = 0;
11308 else
11309 /* getchar(0) and char avail: return char */
11310 n = safe_vgetc();
11311 if (n == K_IGNORE)
11312 continue;
11313 break;
11314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 --no_mapping;
11316 --allow_keys;
11317
Bram Moolenaar219b8702006-11-01 14:32:36 +000011318 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11319 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11320 vimvars[VV_MOUSE_COL].vv_nr = 0;
11321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 if (IS_SPECIAL(n) || mod_mask != 0)
11324 {
11325 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11326 int i = 0;
11327
11328 /* Turn a special key into three bytes, plus modifier. */
11329 if (mod_mask != 0)
11330 {
11331 temp[i++] = K_SPECIAL;
11332 temp[i++] = KS_MODIFIER;
11333 temp[i++] = mod_mask;
11334 }
11335 if (IS_SPECIAL(n))
11336 {
11337 temp[i++] = K_SPECIAL;
11338 temp[i++] = K_SECOND(n);
11339 temp[i++] = K_THIRD(n);
11340 }
11341#ifdef FEAT_MBYTE
11342 else if (has_mbyte)
11343 i += (*mb_char2bytes)(n, temp + i);
11344#endif
11345 else
11346 temp[i++] = n;
11347 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 rettv->v_type = VAR_STRING;
11349 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011350
11351#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011352 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011353 {
11354 int row = mouse_row;
11355 int col = mouse_col;
11356 win_T *win;
11357 linenr_T lnum;
11358# ifdef FEAT_WINDOWS
11359 win_T *wp;
11360# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011361 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011362
11363 if (row >= 0 && col >= 0)
11364 {
11365 /* Find the window at the mouse coordinates and compute the
11366 * text position. */
11367 win = mouse_find_win(&row, &col);
11368 (void)mouse_comp_pos(win, &row, &col, &lnum);
11369# ifdef FEAT_WINDOWS
11370 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011371 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011372# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011373 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011374 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11375 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11376 }
11377 }
11378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 }
11380}
11381
11382/*
11383 * "getcharmod()" function
11384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011387 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
11393/*
11394 * "getcmdline()" function
11395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403}
11404
11405/*
11406 * "getcmdpos()" function
11407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011410 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414}
11415
11416/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011417 * "getcmdtype()" function
11418 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011419 static void
11420f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011421 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011422 typval_T *rettv;
11423{
11424 rettv->v_type = VAR_STRING;
11425 rettv->vval.v_string = alloc(2);
11426 if (rettv->vval.v_string != NULL)
11427 {
11428 rettv->vval.v_string[0] = get_cmdline_type();
11429 rettv->vval.v_string[1] = NUL;
11430 }
11431}
11432
11433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 * "getcwd()" function
11435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011438 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011441 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011444 rettv->vval.v_string = NULL;
11445 cwd = alloc(MAXPATHL);
11446 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011448 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11449 {
11450 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011452 if (rettv->vval.v_string != NULL)
11453 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011455 }
11456 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 }
11458}
11459
11460/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011461 * "getfontname()" function
11462 */
11463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011465 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
11469 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011470#ifdef FEAT_GUI
11471 if (gui.in_use)
11472 {
11473 GuiFont font;
11474 char_u *name = NULL;
11475
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011476 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011477 {
11478 /* Get the "Normal" font. Either the name saved by
11479 * hl_set_font_name() or from the font ID. */
11480 font = gui.norm_font;
11481 name = hl_get_font_name();
11482 }
11483 else
11484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011486 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11487 return;
11488 font = gui_mch_get_font(name, FALSE);
11489 if (font == NOFONT)
11490 return; /* Invalid font name, return empty string. */
11491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011493 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011494 gui_mch_free_font(font);
11495 }
11496#endif
11497}
11498
11499/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011500 * "getfperm({fname})" function
11501 */
11502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 typval_T *argvars;
11505 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011506{
11507 char_u *fname;
11508 struct stat st;
11509 char_u *perm = NULL;
11510 char_u flags[] = "rwx";
11511 int i;
11512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011516 if (mch_stat((char *)fname, &st) >= 0)
11517 {
11518 perm = vim_strsave((char_u *)"---------");
11519 if (perm != NULL)
11520 {
11521 for (i = 0; i < 9; i++)
11522 {
11523 if (st.st_mode & (1 << (8 - i)))
11524 perm[i] = flags[i % 3];
11525 }
11526 }
11527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011529}
11530
11531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 * "getfsize({fname})" function
11533 */
11534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011536 typval_T *argvars;
11537 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538{
11539 char_u *fname;
11540 struct stat st;
11541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545
11546 if (mch_stat((char *)fname, &st) >= 0)
11547 {
11548 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011551 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011553
11554 /* non-perfect check for overflow */
11555 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11556 rettv->vval.v_number = -2;
11557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 }
11559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561}
11562
11563/*
11564 * "getftime({fname})" function
11565 */
11566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *argvars;
11569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570{
11571 char_u *fname;
11572 struct stat st;
11573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011574 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575
11576 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580}
11581
11582/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011583 * "getftype({fname})" function
11584 */
11585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011586f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011587 typval_T *argvars;
11588 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011589{
11590 char_u *fname;
11591 struct stat st;
11592 char_u *type = NULL;
11593 char *t;
11594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011598 if (mch_lstat((char *)fname, &st) >= 0)
11599 {
11600#ifdef S_ISREG
11601 if (S_ISREG(st.st_mode))
11602 t = "file";
11603 else if (S_ISDIR(st.st_mode))
11604 t = "dir";
11605# ifdef S_ISLNK
11606 else if (S_ISLNK(st.st_mode))
11607 t = "link";
11608# endif
11609# ifdef S_ISBLK
11610 else if (S_ISBLK(st.st_mode))
11611 t = "bdev";
11612# endif
11613# ifdef S_ISCHR
11614 else if (S_ISCHR(st.st_mode))
11615 t = "cdev";
11616# endif
11617# ifdef S_ISFIFO
11618 else if (S_ISFIFO(st.st_mode))
11619 t = "fifo";
11620# endif
11621# ifdef S_ISSOCK
11622 else if (S_ISSOCK(st.st_mode))
11623 t = "fifo";
11624# endif
11625 else
11626 t = "other";
11627#else
11628# ifdef S_IFMT
11629 switch (st.st_mode & S_IFMT)
11630 {
11631 case S_IFREG: t = "file"; break;
11632 case S_IFDIR: t = "dir"; break;
11633# ifdef S_IFLNK
11634 case S_IFLNK: t = "link"; break;
11635# endif
11636# ifdef S_IFBLK
11637 case S_IFBLK: t = "bdev"; break;
11638# endif
11639# ifdef S_IFCHR
11640 case S_IFCHR: t = "cdev"; break;
11641# endif
11642# ifdef S_IFIFO
11643 case S_IFIFO: t = "fifo"; break;
11644# endif
11645# ifdef S_IFSOCK
11646 case S_IFSOCK: t = "socket"; break;
11647# endif
11648 default: t = "other";
11649 }
11650# else
11651 if (mch_isdir(fname))
11652 t = "dir";
11653 else
11654 t = "file";
11655# endif
11656#endif
11657 type = vim_strsave((char_u *)t);
11658 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011660}
11661
11662/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011663 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011664 */
11665 static void
11666f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011667 typval_T *argvars;
11668 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011669{
11670 linenr_T lnum;
11671 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011672 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673
11674 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011675 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011676 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011677 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011678 retlist = FALSE;
11679 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011683 retlist = TRUE;
11684 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011685
Bram Moolenaar342337a2005-07-21 21:11:17 +000011686 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011687}
11688
11689/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011690 * "getmatches()" function
11691 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011692 static void
11693f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011694 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011695 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011696{
11697#ifdef FEAT_SEARCH_EXTRA
11698 dict_T *dict;
11699 matchitem_T *cur = curwin->w_match_head;
11700
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011701 if (rettv_list_alloc(rettv) == OK)
11702 {
11703 while (cur != NULL)
11704 {
11705 dict = dict_alloc();
11706 if (dict == NULL)
11707 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011708 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11709 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11710 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11711 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11712 list_append_dict(rettv->vval.v_list, dict);
11713 cur = cur->next;
11714 }
11715 }
11716#endif
11717}
11718
11719/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011720 * "getpid()" function
11721 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011722 static void
11723f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011724 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011725 typval_T *rettv;
11726{
11727 rettv->vval.v_number = mch_get_pid();
11728}
11729
11730/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011731 * "getpos(string)" function
11732 */
11733 static void
11734f_getpos(argvars, rettv)
11735 typval_T *argvars;
11736 typval_T *rettv;
11737{
11738 pos_T *fp;
11739 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011740 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011741
11742 if (rettv_list_alloc(rettv) == OK)
11743 {
11744 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011745 fp = var2fpos(&argvars[0], TRUE, &fnum);
11746 if (fnum != -1)
11747 list_append_number(l, (varnumber_T)fnum);
11748 else
11749 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011750 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11751 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011752 list_append_number(l, (fp != NULL)
11753 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011754 : (varnumber_T)0);
11755 list_append_number(l,
11756#ifdef FEAT_VIRTUALEDIT
11757 (fp != NULL) ? (varnumber_T)fp->coladd :
11758#endif
11759 (varnumber_T)0);
11760 }
11761 else
11762 rettv->vval.v_number = FALSE;
11763}
11764
11765/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011766 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011767 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011768 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011769f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011770 typval_T *argvars UNUSED;
11771 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011772{
11773#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011774 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011775#endif
11776
Bram Moolenaar2641f772005-03-25 21:58:17 +000011777#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011778 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011779 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011780 wp = NULL;
11781 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11782 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011783 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011784 if (wp == NULL)
11785 return;
11786 }
11787
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011788 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011789 }
11790#endif
11791}
11792
11793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 * "getreg()" function
11795 */
11796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011797f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *argvars;
11799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800{
11801 char_u *strregname;
11802 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011803 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011804 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011807 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 strregname = get_tv_string_chk(&argvars[0]);
11810 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011811 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011814 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11815 return_list = get_tv_number_chk(&argvars[2], &error);
11816 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011819 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011820
11821 if (error)
11822 return;
11823
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 regname = (strregname == NULL ? '"' : *strregname);
11825 if (regname == 0)
11826 regname = '"';
11827
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011828 if (return_list)
11829 {
11830 rettv->v_type = VAR_LIST;
11831 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11832 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11833 }
11834 else
11835 {
11836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = get_reg_contents(regname,
11838 arg2 ? GREG_EXPR_SRC : 0);
11839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840}
11841
11842/*
11843 * "getregtype()" function
11844 */
11845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *argvars;
11848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
11850 char_u *strregname;
11851 int regname;
11852 char_u buf[NUMBUFLEN + 2];
11853 long reglen = 0;
11854
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011855 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 {
11857 strregname = get_tv_string_chk(&argvars[0]);
11858 if (strregname == NULL) /* type error; errmsg already given */
11859 {
11860 rettv->v_type = VAR_STRING;
11861 rettv->vval.v_string = NULL;
11862 return;
11863 }
11864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 else
11866 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868
11869 regname = (strregname == NULL ? '"' : *strregname);
11870 if (regname == 0)
11871 regname = '"';
11872
11873 buf[0] = NUL;
11874 buf[1] = NUL;
11875 switch (get_reg_type(regname, &reglen))
11876 {
11877 case MLINE: buf[0] = 'V'; break;
11878 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 case MBLOCK:
11880 buf[0] = Ctrl_V;
11881 sprintf((char *)buf + 1, "%ld", reglen + 1);
11882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->v_type = VAR_STRING;
11885 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886}
11887
11888/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011889 * "gettabvar()" function
11890 */
11891 static void
11892f_gettabvar(argvars, rettv)
11893 typval_T *argvars;
11894 typval_T *rettv;
11895{
11896 tabpage_T *tp;
11897 dictitem_T *v;
11898 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011899 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011900
11901 rettv->v_type = VAR_STRING;
11902 rettv->vval.v_string = NULL;
11903
11904 varname = get_tv_string_chk(&argvars[1]);
11905 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11906 if (tp != NULL && varname != NULL)
11907 {
11908 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011909 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011910 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011911 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011912 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011913 done = TRUE;
11914 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011915 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011916
11917 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011918 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011919}
11920
11921/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011922 * "gettabwinvar()" function
11923 */
11924 static void
11925f_gettabwinvar(argvars, rettv)
11926 typval_T *argvars;
11927 typval_T *rettv;
11928{
11929 getwinvar(argvars, rettv, 1);
11930}
11931
11932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 * "getwinposx()" function
11934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936f_getwinposx(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 = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
11949#endif
11950}
11951
11952/*
11953 * "getwinposy()" function
11954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961#ifdef FEAT_GUI
11962 if (gui.in_use)
11963 {
11964 int x, y;
11965
11966 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 }
11969#endif
11970}
11971
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011972/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011973 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011974 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011975 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011976find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011977 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011978 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011979{
11980#ifdef FEAT_WINDOWS
11981 win_T *wp;
11982#endif
11983 int nr;
11984
11985 nr = get_tv_number_chk(vp, NULL);
11986
11987#ifdef FEAT_WINDOWS
11988 if (nr < 0)
11989 return NULL;
11990 if (nr == 0)
11991 return curwin;
11992
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011993 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11994 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011995 if (--nr <= 0)
11996 break;
11997 return wp;
11998#else
11999 if (nr == 0 || nr == 1)
12000 return curwin;
12001 return NULL;
12002#endif
12003}
12004
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005/*
12006 * "getwinvar()" function
12007 */
12008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 typval_T *argvars;
12011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012013 getwinvar(argvars, rettv, 0);
12014}
12015
12016/*
12017 * getwinvar() and gettabwinvar()
12018 */
12019 static void
12020getwinvar(argvars, rettv, off)
12021 typval_T *argvars;
12022 typval_T *rettv;
12023 int off; /* 1 for gettabwinvar() */
12024{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 win_T *win, *oldcurwin;
12026 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012027 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012028 tabpage_T *tp = NULL;
12029 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012030 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012032#ifdef FEAT_WINDOWS
12033 if (off == 1)
12034 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12035 else
12036 tp = curtab;
12037#endif
12038 win = find_win_by_nr(&argvars[off], tp);
12039 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 rettv->v_type = VAR_STRING;
12043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
12045 if (win != NULL && varname != NULL)
12046 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012047 /* Set curwin to be our win, temporarily. Also set the tabpage,
12048 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012049 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012050
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012052 {
12053 if (get_option_tv(&varname, rettv, 1) == OK)
12054 done = TRUE;
12055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 else
12057 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012058 /* Look up the variable. */
12059 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12060 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012062 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012064 done = TRUE;
12065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012067
12068 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012069 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 }
12071
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012072 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12073 /* use the default return value */
12074 copy_tv(&argvars[off + 2], rettv);
12075
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 --emsg_off;
12077}
12078
12079/*
12080 * "glob()" function
12081 */
12082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 typval_T *argvars;
12085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012087 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012089 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012091 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012092 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012094 if (argvars[1].v_type != VAR_UNKNOWN)
12095 {
12096 if (get_tv_number_chk(&argvars[1], &error))
12097 options |= WILD_KEEP_ALL;
12098 if (argvars[2].v_type != VAR_UNKNOWN
12099 && get_tv_number_chk(&argvars[2], &error))
12100 {
12101 rettv->v_type = VAR_LIST;
12102 rettv->vval.v_list = NULL;
12103 }
12104 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012105 if (!error)
12106 {
12107 ExpandInit(&xpc);
12108 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012109 if (p_wic)
12110 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012111 if (rettv->v_type == VAR_STRING)
12112 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012113 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012114 else if (rettv_list_alloc(rettv) != FAIL)
12115 {
12116 int i;
12117
12118 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12119 NULL, options, WILD_ALL_KEEP);
12120 for (i = 0; i < xpc.xp_numfiles; i++)
12121 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12122
12123 ExpandCleanup(&xpc);
12124 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012125 }
12126 else
12127 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128}
12129
12130/*
12131 * "globpath()" function
12132 */
12133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012135 typval_T *argvars;
12136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012138 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012140 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012141 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012143 /* When the optional second argument is non-zero, don't remove matches
12144 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12145 if (argvars[2].v_type != VAR_UNKNOWN
12146 && get_tv_number_chk(&argvars[2], &error))
12147 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012149 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012150 rettv->vval.v_string = NULL;
12151 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012152 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12153 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154}
12155
12156/*
12157 * "has()" function
12158 */
12159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012161 typval_T *argvars;
12162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163{
12164 int i;
12165 char_u *name;
12166 int n = FALSE;
12167 static char *(has_list[]) =
12168 {
12169#ifdef AMIGA
12170 "amiga",
12171# ifdef FEAT_ARP
12172 "arp",
12173# endif
12174#endif
12175#ifdef __BEOS__
12176 "beos",
12177#endif
12178#ifdef MSDOS
12179# ifdef DJGPP
12180 "dos32",
12181# else
12182 "dos16",
12183# endif
12184#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012185#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 "mac",
12187#endif
12188#if defined(MACOS_X_UNIX)
12189 "macunix",
12190#endif
12191#ifdef OS2
12192 "os2",
12193#endif
12194#ifdef __QNX__
12195 "qnx",
12196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197#ifdef UNIX
12198 "unix",
12199#endif
12200#ifdef VMS
12201 "vms",
12202#endif
12203#ifdef WIN16
12204 "win16",
12205#endif
12206#ifdef WIN32
12207 "win32",
12208#endif
12209#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12210 "win32unix",
12211#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012212#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 "win64",
12214#endif
12215#ifdef EBCDIC
12216 "ebcdic",
12217#endif
12218#ifndef CASE_INSENSITIVE_FILENAME
12219 "fname_case",
12220#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012221#ifdef HAVE_ACL
12222 "acl",
12223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#ifdef FEAT_ARABIC
12225 "arabic",
12226#endif
12227#ifdef FEAT_AUTOCMD
12228 "autocmd",
12229#endif
12230#ifdef FEAT_BEVAL
12231 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012232# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12233 "balloon_multiline",
12234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235#endif
12236#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12237 "builtin_terms",
12238# ifdef ALL_BUILTIN_TCAPS
12239 "all_builtin_terms",
12240# endif
12241#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012242#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12243 || defined(FEAT_GUI_W32) \
12244 || defined(FEAT_GUI_MOTIF))
12245 "browsefilter",
12246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247#ifdef FEAT_BYTEOFF
12248 "byte_offset",
12249#endif
12250#ifdef FEAT_CINDENT
12251 "cindent",
12252#endif
12253#ifdef FEAT_CLIENTSERVER
12254 "clientserver",
12255#endif
12256#ifdef FEAT_CLIPBOARD
12257 "clipboard",
12258#endif
12259#ifdef FEAT_CMDL_COMPL
12260 "cmdline_compl",
12261#endif
12262#ifdef FEAT_CMDHIST
12263 "cmdline_hist",
12264#endif
12265#ifdef FEAT_COMMENTS
12266 "comments",
12267#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012268#ifdef FEAT_CONCEAL
12269 "conceal",
12270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271#ifdef FEAT_CRYPT
12272 "cryptv",
12273#endif
12274#ifdef FEAT_CSCOPE
12275 "cscope",
12276#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012277#ifdef FEAT_CURSORBIND
12278 "cursorbind",
12279#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012280#ifdef CURSOR_SHAPE
12281 "cursorshape",
12282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef DEBUG
12284 "debug",
12285#endif
12286#ifdef FEAT_CON_DIALOG
12287 "dialog_con",
12288#endif
12289#ifdef FEAT_GUI_DIALOG
12290 "dialog_gui",
12291#endif
12292#ifdef FEAT_DIFF
12293 "diff",
12294#endif
12295#ifdef FEAT_DIGRAPHS
12296 "digraphs",
12297#endif
12298#ifdef FEAT_DND
12299 "dnd",
12300#endif
12301#ifdef FEAT_EMACS_TAGS
12302 "emacs_tags",
12303#endif
12304 "eval", /* always present, of course! */
12305#ifdef FEAT_EX_EXTRA
12306 "ex_extra",
12307#endif
12308#ifdef FEAT_SEARCH_EXTRA
12309 "extra_search",
12310#endif
12311#ifdef FEAT_FKMAP
12312 "farsi",
12313#endif
12314#ifdef FEAT_SEARCHPATH
12315 "file_in_path",
12316#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012317#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012318 "filterpipe",
12319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320#ifdef FEAT_FIND_ID
12321 "find_in_path",
12322#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012323#ifdef FEAT_FLOAT
12324 "float",
12325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326#ifdef FEAT_FOLDING
12327 "folding",
12328#endif
12329#ifdef FEAT_FOOTER
12330 "footer",
12331#endif
12332#if !defined(USE_SYSTEM) && defined(UNIX)
12333 "fork",
12334#endif
12335#ifdef FEAT_GETTEXT
12336 "gettext",
12337#endif
12338#ifdef FEAT_GUI
12339 "gui",
12340#endif
12341#ifdef FEAT_GUI_ATHENA
12342# ifdef FEAT_GUI_NEXTAW
12343 "gui_neXtaw",
12344# else
12345 "gui_athena",
12346# endif
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348#ifdef FEAT_GUI_GTK
12349 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012352#ifdef FEAT_GUI_GNOME
12353 "gui_gnome",
12354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355#ifdef FEAT_GUI_MAC
12356 "gui_mac",
12357#endif
12358#ifdef FEAT_GUI_MOTIF
12359 "gui_motif",
12360#endif
12361#ifdef FEAT_GUI_PHOTON
12362 "gui_photon",
12363#endif
12364#ifdef FEAT_GUI_W16
12365 "gui_win16",
12366#endif
12367#ifdef FEAT_GUI_W32
12368 "gui_win32",
12369#endif
12370#ifdef FEAT_HANGULIN
12371 "hangul_input",
12372#endif
12373#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12374 "iconv",
12375#endif
12376#ifdef FEAT_INS_EXPAND
12377 "insert_expand",
12378#endif
12379#ifdef FEAT_JUMPLIST
12380 "jumplist",
12381#endif
12382#ifdef FEAT_KEYMAP
12383 "keymap",
12384#endif
12385#ifdef FEAT_LANGMAP
12386 "langmap",
12387#endif
12388#ifdef FEAT_LIBCALL
12389 "libcall",
12390#endif
12391#ifdef FEAT_LINEBREAK
12392 "linebreak",
12393#endif
12394#ifdef FEAT_LISP
12395 "lispindent",
12396#endif
12397#ifdef FEAT_LISTCMDS
12398 "listcmds",
12399#endif
12400#ifdef FEAT_LOCALMAP
12401 "localmap",
12402#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012403#ifdef FEAT_LUA
12404# ifndef DYNAMIC_LUA
12405 "lua",
12406# endif
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_MENU
12409 "menu",
12410#endif
12411#ifdef FEAT_SESSION
12412 "mksession",
12413#endif
12414#ifdef FEAT_MODIFY_FNAME
12415 "modify_fname",
12416#endif
12417#ifdef FEAT_MOUSE
12418 "mouse",
12419#endif
12420#ifdef FEAT_MOUSESHAPE
12421 "mouseshape",
12422#endif
12423#if defined(UNIX) || defined(VMS)
12424# ifdef FEAT_MOUSE_DEC
12425 "mouse_dec",
12426# endif
12427# ifdef FEAT_MOUSE_GPM
12428 "mouse_gpm",
12429# endif
12430# ifdef FEAT_MOUSE_JSB
12431 "mouse_jsbterm",
12432# endif
12433# ifdef FEAT_MOUSE_NET
12434 "mouse_netterm",
12435# endif
12436# ifdef FEAT_MOUSE_PTERM
12437 "mouse_pterm",
12438# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012439# ifdef FEAT_MOUSE_SGR
12440 "mouse_sgr",
12441# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012442# ifdef FEAT_SYSMOUSE
12443 "mouse_sysmouse",
12444# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012445# ifdef FEAT_MOUSE_URXVT
12446 "mouse_urxvt",
12447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448# ifdef FEAT_MOUSE_XTERM
12449 "mouse_xterm",
12450# endif
12451#endif
12452#ifdef FEAT_MBYTE
12453 "multi_byte",
12454#endif
12455#ifdef FEAT_MBYTE_IME
12456 "multi_byte_ime",
12457#endif
12458#ifdef FEAT_MULTI_LANG
12459 "multi_lang",
12460#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012461#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012462#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012463 "mzscheme",
12464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466#ifdef FEAT_OLE
12467 "ole",
12468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469#ifdef FEAT_PATH_EXTRA
12470 "path_extra",
12471#endif
12472#ifdef FEAT_PERL
12473#ifndef DYNAMIC_PERL
12474 "perl",
12475#endif
12476#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012477#ifdef FEAT_PERSISTENT_UNDO
12478 "persistent_undo",
12479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480#ifdef FEAT_PYTHON
12481#ifndef DYNAMIC_PYTHON
12482 "python",
12483#endif
12484#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012485#ifdef FEAT_PYTHON3
12486#ifndef DYNAMIC_PYTHON3
12487 "python3",
12488#endif
12489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490#ifdef FEAT_POSTSCRIPT
12491 "postscript",
12492#endif
12493#ifdef FEAT_PRINTER
12494 "printer",
12495#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012496#ifdef FEAT_PROFILE
12497 "profile",
12498#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012499#ifdef FEAT_RELTIME
12500 "reltime",
12501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502#ifdef FEAT_QUICKFIX
12503 "quickfix",
12504#endif
12505#ifdef FEAT_RIGHTLEFT
12506 "rightleft",
12507#endif
12508#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12509 "ruby",
12510#endif
12511#ifdef FEAT_SCROLLBIND
12512 "scrollbind",
12513#endif
12514#ifdef FEAT_CMDL_INFO
12515 "showcmd",
12516 "cmdline_info",
12517#endif
12518#ifdef FEAT_SIGNS
12519 "signs",
12520#endif
12521#ifdef FEAT_SMARTINDENT
12522 "smartindent",
12523#endif
12524#ifdef FEAT_SNIFF
12525 "sniff",
12526#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012527#ifdef STARTUPTIME
12528 "startuptime",
12529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530#ifdef FEAT_STL_OPT
12531 "statusline",
12532#endif
12533#ifdef FEAT_SUN_WORKSHOP
12534 "sun_workshop",
12535#endif
12536#ifdef FEAT_NETBEANS_INTG
12537 "netbeans_intg",
12538#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012539#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012540 "spell",
12541#endif
12542#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543 "syntax",
12544#endif
12545#if defined(USE_SYSTEM) || !defined(UNIX)
12546 "system",
12547#endif
12548#ifdef FEAT_TAG_BINS
12549 "tag_binary",
12550#endif
12551#ifdef FEAT_TAG_OLDSTATIC
12552 "tag_old_static",
12553#endif
12554#ifdef FEAT_TAG_ANYWHITE
12555 "tag_any_white",
12556#endif
12557#ifdef FEAT_TCL
12558# ifndef DYNAMIC_TCL
12559 "tcl",
12560# endif
12561#endif
12562#ifdef TERMINFO
12563 "terminfo",
12564#endif
12565#ifdef FEAT_TERMRESPONSE
12566 "termresponse",
12567#endif
12568#ifdef FEAT_TEXTOBJ
12569 "textobjects",
12570#endif
12571#ifdef HAVE_TGETENT
12572 "tgetent",
12573#endif
12574#ifdef FEAT_TITLE
12575 "title",
12576#endif
12577#ifdef FEAT_TOOLBAR
12578 "toolbar",
12579#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012580#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12581 "unnamedplus",
12582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#ifdef FEAT_USR_CMDS
12584 "user-commands", /* was accidentally included in 5.4 */
12585 "user_commands",
12586#endif
12587#ifdef FEAT_VIMINFO
12588 "viminfo",
12589#endif
12590#ifdef FEAT_VERTSPLIT
12591 "vertsplit",
12592#endif
12593#ifdef FEAT_VIRTUALEDIT
12594 "virtualedit",
12595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597#ifdef FEAT_VISUALEXTRA
12598 "visualextra",
12599#endif
12600#ifdef FEAT_VREPLACE
12601 "vreplace",
12602#endif
12603#ifdef FEAT_WILDIGN
12604 "wildignore",
12605#endif
12606#ifdef FEAT_WILDMENU
12607 "wildmenu",
12608#endif
12609#ifdef FEAT_WINDOWS
12610 "windows",
12611#endif
12612#ifdef FEAT_WAK
12613 "winaltkeys",
12614#endif
12615#ifdef FEAT_WRITEBACKUP
12616 "writebackup",
12617#endif
12618#ifdef FEAT_XIM
12619 "xim",
12620#endif
12621#ifdef FEAT_XFONTSET
12622 "xfontset",
12623#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012624#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012625 "xpm",
12626 "xpm_w32", /* for backward compatibility */
12627#else
12628# if defined(HAVE_XPM)
12629 "xpm",
12630# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#ifdef USE_XSMP
12633 "xsmp",
12634#endif
12635#ifdef USE_XSMP_INTERACT
12636 "xsmp_interact",
12637#endif
12638#ifdef FEAT_XCLIPBOARD
12639 "xterm_clipboard",
12640#endif
12641#ifdef FEAT_XTERM_SAVE
12642 "xterm_save",
12643#endif
12644#if defined(UNIX) && defined(FEAT_X11)
12645 "X11",
12646#endif
12647 NULL
12648 };
12649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 for (i = 0; has_list[i] != NULL; ++i)
12652 if (STRICMP(name, has_list[i]) == 0)
12653 {
12654 n = TRUE;
12655 break;
12656 }
12657
12658 if (n == FALSE)
12659 {
12660 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012661 {
12662 if (name[5] == '-'
12663 && STRLEN(name) > 11
12664 && vim_isdigit(name[6])
12665 && vim_isdigit(name[8])
12666 && vim_isdigit(name[10]))
12667 {
12668 int major = atoi((char *)name + 6);
12669 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012670
12671 /* Expect "patch-9.9.01234". */
12672 n = (major < VIM_VERSION_MAJOR
12673 || (major == VIM_VERSION_MAJOR
12674 && (minor < VIM_VERSION_MINOR
12675 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012676 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012677 }
12678 else
12679 n = has_patch(atoi((char *)name + 5));
12680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 else if (STRICMP(name, "vim_starting") == 0)
12682 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012683#ifdef FEAT_MBYTE
12684 else if (STRICMP(name, "multi_byte_encoding") == 0)
12685 n = has_mbyte;
12686#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012687#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12688 else if (STRICMP(name, "balloon_multiline") == 0)
12689 n = multiline_balloon_available();
12690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691#ifdef DYNAMIC_TCL
12692 else if (STRICMP(name, "tcl") == 0)
12693 n = tcl_enabled(FALSE);
12694#endif
12695#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12696 else if (STRICMP(name, "iconv") == 0)
12697 n = iconv_enabled(FALSE);
12698#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012699#ifdef DYNAMIC_LUA
12700 else if (STRICMP(name, "lua") == 0)
12701 n = lua_enabled(FALSE);
12702#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012703#ifdef DYNAMIC_MZSCHEME
12704 else if (STRICMP(name, "mzscheme") == 0)
12705 n = mzscheme_enabled(FALSE);
12706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707#ifdef DYNAMIC_RUBY
12708 else if (STRICMP(name, "ruby") == 0)
12709 n = ruby_enabled(FALSE);
12710#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012711#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712#ifdef DYNAMIC_PYTHON
12713 else if (STRICMP(name, "python") == 0)
12714 n = python_enabled(FALSE);
12715#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012716#endif
12717#ifdef FEAT_PYTHON3
12718#ifdef DYNAMIC_PYTHON3
12719 else if (STRICMP(name, "python3") == 0)
12720 n = python3_enabled(FALSE);
12721#endif
12722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723#ifdef DYNAMIC_PERL
12724 else if (STRICMP(name, "perl") == 0)
12725 n = perl_enabled(FALSE);
12726#endif
12727#ifdef FEAT_GUI
12728 else if (STRICMP(name, "gui_running") == 0)
12729 n = (gui.in_use || gui.starting);
12730# ifdef FEAT_GUI_W32
12731 else if (STRICMP(name, "gui_win32s") == 0)
12732 n = gui_is_win32s();
12733# endif
12734# ifdef FEAT_BROWSE
12735 else if (STRICMP(name, "browse") == 0)
12736 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12737# endif
12738#endif
12739#ifdef FEAT_SYN_HL
12740 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012741 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742#endif
12743#if defined(WIN3264)
12744 else if (STRICMP(name, "win95") == 0)
12745 n = mch_windows95();
12746#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012747#ifdef FEAT_NETBEANS_INTG
12748 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012749 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 }
12752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
12756/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012757 * "has_key()" function
12758 */
12759 static void
12760f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012761 typval_T *argvars;
12762 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012763{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012764 if (argvars[0].v_type != VAR_DICT)
12765 {
12766 EMSG(_(e_dictreq));
12767 return;
12768 }
12769 if (argvars[0].vval.v_dict == NULL)
12770 return;
12771
12772 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012773 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012774}
12775
12776/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012777 * "haslocaldir()" function
12778 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012779 static void
12780f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012781 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012782 typval_T *rettv;
12783{
12784 rettv->vval.v_number = (curwin->w_localdir != NULL);
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "hasmapto()" function
12789 */
12790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012792 typval_T *argvars;
12793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794{
12795 char_u *name;
12796 char_u *mode;
12797 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012798 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 mode = (char_u *)"nvo";
12803 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012806 if (argvars[2].v_type != VAR_UNKNOWN)
12807 abbr = get_tv_number(&argvars[2]);
12808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809
Bram Moolenaar2c932302006-03-18 21:42:09 +000012810 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814}
12815
12816/*
12817 * "histadd()" function
12818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012821 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823{
12824#ifdef FEAT_CMDHIST
12825 int histype;
12826 char_u *str;
12827 char_u buf[NUMBUFLEN];
12828#endif
12829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 if (check_restricted() || check_secure())
12832 return;
12833#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12835 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 if (histype >= 0)
12837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 if (*str != NUL)
12840 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012841 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 return;
12845 }
12846 }
12847#endif
12848}
12849
12850/*
12851 * "histdel()" function
12852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012855 typval_T *argvars UNUSED;
12856 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857{
12858#ifdef FEAT_CMDHIST
12859 int n;
12860 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012861 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012863 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12864 if (str == NULL)
12865 n = 0;
12866 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012868 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012869 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012871 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 else
12874 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012875 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 get_tv_string_buf(&argvars[1], buf));
12877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878#endif
12879}
12880
12881/*
12882 * "histget()" function
12883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
12889#ifdef FEAT_CMDHIST
12890 int type;
12891 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012892 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12895 if (str == NULL)
12896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 {
12899 type = get_histtype(str);
12900 if (argvars[1].v_type == VAR_UNKNOWN)
12901 idx = get_history_idx(type);
12902 else
12903 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12904 /* -1 on type error */
12905 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911}
12912
12913/*
12914 * "histnr()" function
12915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012918 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
12921 int i;
12922
12923#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012924 char_u *history = get_tv_string_chk(&argvars[0]);
12925
12926 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 if (i >= HIST_CMD && i < HIST_COUNT)
12928 i = get_history_idx(i);
12929 else
12930#endif
12931 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933}
12934
12935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 * "highlightID(name)" function
12937 */
12938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012940 typval_T *argvars;
12941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944}
12945
12946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012947 * "highlight_exists()" function
12948 */
12949 static void
12950f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *argvars;
12952 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012953{
12954 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12955}
12956
12957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 * "hostname()" function
12959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012962 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964{
12965 char_u hostname[256];
12966
12967 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012968 rettv->v_type = VAR_STRING;
12969 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970}
12971
12972/*
12973 * iconv() function
12974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012977 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
12980#ifdef FEAT_MBYTE
12981 char_u buf1[NUMBUFLEN];
12982 char_u buf2[NUMBUFLEN];
12983 char_u *from, *to, *str;
12984 vimconv_T vimconv;
12985#endif
12986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012987 rettv->v_type = VAR_STRING;
12988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989
12990#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 str = get_tv_string(&argvars[0]);
12992 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12993 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 vimconv.vc_type = CONV_NONE;
12995 convert_setup(&vimconv, from, to);
12996
12997 /* If the encodings are equal, no conversion needed. */
12998 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002
13003 convert_setup(&vimconv, NULL, NULL);
13004 vim_free(from);
13005 vim_free(to);
13006#endif
13007}
13008
13009/*
13010 * "indent()" function
13011 */
13012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013014 typval_T *argvars;
13015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016{
13017 linenr_T lnum;
13018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024}
13025
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013026/*
13027 * "index()" function
13028 */
13029 static void
13030f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013031 typval_T *argvars;
13032 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013033{
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 list_T *l;
13035 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013036 long idx = 0;
13037 int ic = FALSE;
13038
13039 rettv->vval.v_number = -1;
13040 if (argvars[0].v_type != VAR_LIST)
13041 {
13042 EMSG(_(e_listreq));
13043 return;
13044 }
13045 l = argvars[0].vval.v_list;
13046 if (l != NULL)
13047 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013048 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013049 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 int error = FALSE;
13052
Bram Moolenaar758711c2005-02-02 23:11:38 +000013053 /* Start at specified item. Use the cached index that list_find()
13054 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013056 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013057 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013058 ic = get_tv_number_chk(&argvars[3], &error);
13059 if (error)
13060 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013061 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013062
Bram Moolenaar758711c2005-02-02 23:11:38 +000013063 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013064 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013065 {
13066 rettv->vval.v_number = idx;
13067 break;
13068 }
13069 }
13070}
13071
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072static int inputsecret_flag = 0;
13073
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013074static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13075
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013077 * This function is used by f_input() and f_inputdialog() functions. The third
13078 * argument to f_input() specifies the type of completion to use at the
13079 * prompt. The third argument to f_inputdialog() specifies the value to return
13080 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 */
13082 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013083get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013084 typval_T *argvars;
13085 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013086 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 char_u *p = NULL;
13090 int c;
13091 char_u buf[NUMBUFLEN];
13092 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013094 int xp_type = EXPAND_NOTHING;
13095 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013098 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099
13100#ifdef NO_CONSOLE_INPUT
13101 /* While starting up, there is no place to enter text. */
13102 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104#endif
13105
13106 cmd_silent = FALSE; /* Want to see the prompt. */
13107 if (prompt != NULL)
13108 {
13109 /* Only the part of the message after the last NL is considered as
13110 * prompt for the command line */
13111 p = vim_strrchr(prompt, '\n');
13112 if (p == NULL)
13113 p = prompt;
13114 else
13115 {
13116 ++p;
13117 c = *p;
13118 *p = NUL;
13119 msg_start();
13120 msg_clr_eos();
13121 msg_puts_attr(prompt, echo_attr);
13122 msg_didout = FALSE;
13123 msg_starthere();
13124 *p = c;
13125 }
13126 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013128 if (argvars[1].v_type != VAR_UNKNOWN)
13129 {
13130 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13131 if (defstr != NULL)
13132 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013134 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013135 {
13136 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013137 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013138 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013139
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013140 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013141 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013142
Bram Moolenaar4463f292005-09-25 22:20:24 +000013143 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13144 if (xp_name == NULL)
13145 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013146
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013147 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013148
Bram Moolenaar4463f292005-09-25 22:20:24 +000013149 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13150 &xp_arg) == FAIL)
13151 return;
13152 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013153 }
13154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013156 {
13157# ifdef FEAT_EX_EXTRA
13158 int save_ex_normal_busy = ex_normal_busy;
13159 ex_normal_busy = 0;
13160# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013162 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13163 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013164# ifdef FEAT_EX_EXTRA
13165 ex_normal_busy = save_ex_normal_busy;
13166# endif
13167 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013168 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013169 && argvars[1].v_type != VAR_UNKNOWN
13170 && argvars[2].v_type != VAR_UNKNOWN)
13171 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13172 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013173
13174 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013176 /* since the user typed this, no need to wait for return */
13177 need_wait_return = FALSE;
13178 msg_didout = FALSE;
13179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 cmd_silent = cmd_silent_save;
13181}
13182
13183/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013184 * "input()" function
13185 * Also handles inputsecret() when inputsecret is set.
13186 */
13187 static void
13188f_input(argvars, rettv)
13189 typval_T *argvars;
13190 typval_T *rettv;
13191{
13192 get_user_input(argvars, rettv, FALSE);
13193}
13194
13195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 * "inputdialog()" function
13197 */
13198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013200 typval_T *argvars;
13201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202{
13203#if defined(FEAT_GUI_TEXTDIALOG)
13204 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13205 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13206 {
13207 char_u *message;
13208 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 message = get_tv_string_chk(&argvars[0]);
13212 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013213 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013214 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 else
13216 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013217 if (message != NULL && defstr != NULL
13218 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013219 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 else
13222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 if (message != NULL && defstr != NULL
13224 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013225 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226 rettv->vval.v_string = vim_strsave(
13227 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 }
13233 else
13234#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013235 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236}
13237
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013238/*
13239 * "inputlist()" function
13240 */
13241 static void
13242f_inputlist(argvars, rettv)
13243 typval_T *argvars;
13244 typval_T *rettv;
13245{
13246 listitem_T *li;
13247 int selected;
13248 int mouse_used;
13249
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013250#ifdef NO_CONSOLE_INPUT
13251 /* While starting up, there is no place to enter text. */
13252 if (no_console_input())
13253 return;
13254#endif
13255 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13256 {
13257 EMSG2(_(e_listarg), "inputlist()");
13258 return;
13259 }
13260
13261 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013262 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013263 lines_left = Rows; /* avoid more prompt */
13264 msg_scroll = TRUE;
13265 msg_clr_eos();
13266
13267 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13268 {
13269 msg_puts(get_tv_string(&li->li_tv));
13270 msg_putchar('\n');
13271 }
13272
13273 /* Ask for choice. */
13274 selected = prompt_for_number(&mouse_used);
13275 if (mouse_used)
13276 selected -= lines_left;
13277
13278 rettv->vval.v_number = selected;
13279}
13280
13281
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13283
13284/*
13285 * "inputrestore()" function
13286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291{
13292 if (ga_userinput.ga_len > 0)
13293 {
13294 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13296 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013297 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 }
13299 else if (p_verbose > 1)
13300 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013301 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 }
13304}
13305
13306/*
13307 * "inputsave()" function
13308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013310f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013311 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013314 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 if (ga_grow(&ga_userinput, 1) == OK)
13316 {
13317 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13318 + ga_userinput.ga_len);
13319 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013320 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 }
13322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013323 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324}
13325
13326/*
13327 * "inputsecret()" function
13328 */
13329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013331 typval_T *argvars;
13332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333{
13334 ++cmdline_star;
13335 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013336 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 --cmdline_star;
13338 --inputsecret_flag;
13339}
13340
13341/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013342 * "insert()" function
13343 */
13344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013345f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 typval_T *argvars;
13347 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013348{
13349 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 listitem_T *item;
13351 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013353
13354 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013355 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013356 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013357 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013358 {
13359 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360 before = get_tv_number_chk(&argvars[2], &error);
13361 if (error)
13362 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013363
Bram Moolenaar758711c2005-02-02 23:11:38 +000013364 if (before == l->lv_len)
13365 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013366 else
13367 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013368 item = list_find(l, before);
13369 if (item == NULL)
13370 {
13371 EMSGN(_(e_listidx), before);
13372 l = NULL;
13373 }
13374 }
13375 if (l != NULL)
13376 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013377 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013378 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013379 }
13380 }
13381}
13382
13383/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013384 * "invert(expr)" function
13385 */
13386 static void
13387f_invert(argvars, rettv)
13388 typval_T *argvars;
13389 typval_T *rettv;
13390{
13391 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13392}
13393
13394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 * "isdirectory()" function
13396 */
13397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013398f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013399 typval_T *argvars;
13400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013402 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403}
13404
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013405/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013406 * "islocked()" function
13407 */
13408 static void
13409f_islocked(argvars, rettv)
13410 typval_T *argvars;
13411 typval_T *rettv;
13412{
13413 lval_T lv;
13414 char_u *end;
13415 dictitem_T *di;
13416
13417 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013418 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13419 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013420 if (end != NULL && lv.ll_name != NULL)
13421 {
13422 if (*end != NUL)
13423 EMSG(_(e_trailing));
13424 else
13425 {
13426 if (lv.ll_tv == NULL)
13427 {
13428 if (check_changedtick(lv.ll_name))
13429 rettv->vval.v_number = 1; /* always locked */
13430 else
13431 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013432 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013433 if (di != NULL)
13434 {
13435 /* Consider a variable locked when:
13436 * 1. the variable itself is locked
13437 * 2. the value of the variable is locked.
13438 * 3. the List or Dict value is locked.
13439 */
13440 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13441 || tv_islocked(&di->di_tv));
13442 }
13443 }
13444 }
13445 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013446 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013447 else if (lv.ll_newkey != NULL)
13448 EMSG2(_(e_dictkey), lv.ll_newkey);
13449 else if (lv.ll_list != NULL)
13450 /* List item. */
13451 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13452 else
13453 /* Dictionary item. */
13454 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13455 }
13456 }
13457
13458 clear_lval(&lv);
13459}
13460
Bram Moolenaar33570922005-01-25 22:26:29 +000013461static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013462
13463/*
13464 * Turn a dict into a list:
13465 * "what" == 0: list of keys
13466 * "what" == 1: list of values
13467 * "what" == 2: list of items
13468 */
13469 static void
13470dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013471 typval_T *argvars;
13472 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013473 int what;
13474{
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 list_T *l2;
13476 dictitem_T *di;
13477 hashitem_T *hi;
13478 listitem_T *li;
13479 listitem_T *li2;
13480 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013481 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013482
Bram Moolenaar8c711452005-01-14 21:53:12 +000013483 if (argvars[0].v_type != VAR_DICT)
13484 {
13485 EMSG(_(e_dictreq));
13486 return;
13487 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013488 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013489 return;
13490
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013491 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013493
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013494 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013496 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013497 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013498 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013499 --todo;
13500 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013501
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013502 li = listitem_alloc();
13503 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013504 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013505 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013507 if (what == 0)
13508 {
13509 /* keys() */
13510 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013511 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013512 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13513 }
13514 else if (what == 1)
13515 {
13516 /* values() */
13517 copy_tv(&di->di_tv, &li->li_tv);
13518 }
13519 else
13520 {
13521 /* items() */
13522 l2 = list_alloc();
13523 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013524 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013525 li->li_tv.vval.v_list = l2;
13526 if (l2 == NULL)
13527 break;
13528 ++l2->lv_refcount;
13529
13530 li2 = listitem_alloc();
13531 if (li2 == NULL)
13532 break;
13533 list_append(l2, li2);
13534 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013535 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013536 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13537
13538 li2 = listitem_alloc();
13539 if (li2 == NULL)
13540 break;
13541 list_append(l2, li2);
13542 copy_tv(&di->di_tv, &li2->li_tv);
13543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013544 }
13545 }
13546}
13547
13548/*
13549 * "items(dict)" function
13550 */
13551 static void
13552f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *argvars;
13554 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013555{
13556 dict_list(argvars, rettv, 2);
13557}
13558
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013560 * "join()" function
13561 */
13562 static void
13563f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013564 typval_T *argvars;
13565 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566{
13567 garray_T ga;
13568 char_u *sep;
13569
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013570 if (argvars[0].v_type != VAR_LIST)
13571 {
13572 EMSG(_(e_listreq));
13573 return;
13574 }
13575 if (argvars[0].vval.v_list == NULL)
13576 return;
13577 if (argvars[1].v_type == VAR_UNKNOWN)
13578 sep = (char_u *)" ";
13579 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013581
13582 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583
13584 if (sep != NULL)
13585 {
13586 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013587 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 ga_append(&ga, NUL);
13589 rettv->vval.v_string = (char_u *)ga.ga_data;
13590 }
13591 else
13592 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013593}
13594
13595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013596 * "keys()" function
13597 */
13598 static void
13599f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013600 typval_T *argvars;
13601 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013602{
13603 dict_list(argvars, rettv, 0);
13604}
13605
13606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 * "last_buffer_nr()" function.
13608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013611 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
13614 int n = 0;
13615 buf_T *buf;
13616
13617 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13618 if (n < buf->b_fnum)
13619 n = buf->b_fnum;
13620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013622}
13623
13624/*
13625 * "len()" function
13626 */
13627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013631{
13632 switch (argvars[0].v_type)
13633 {
13634 case VAR_STRING:
13635 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = (varnumber_T)STRLEN(
13637 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013638 break;
13639 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013641 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013642 case VAR_DICT:
13643 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013645 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013646 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013647 break;
13648 }
13649}
13650
Bram Moolenaar33570922005-01-25 22:26:29 +000013651static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013652
13653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *argvars;
13656 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013657 int type;
13658{
13659#ifdef FEAT_LIBCALL
13660 char_u *string_in;
13661 char_u **string_result;
13662 int nr_result;
13663#endif
13664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013666 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013668
13669 if (check_restricted() || check_secure())
13670 return;
13671
13672#ifdef FEAT_LIBCALL
13673 /* The first two args must be strings, otherwise its meaningless */
13674 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013676 string_in = NULL;
13677 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013678 string_in = argvars[2].vval.v_string;
13679 if (type == VAR_NUMBER)
13680 string_result = NULL;
13681 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013683 if (mch_libcall(argvars[0].vval.v_string,
13684 argvars[1].vval.v_string,
13685 string_in,
13686 argvars[2].vval.v_number,
13687 string_result,
13688 &nr_result) == OK
13689 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013691 }
13692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693}
13694
13695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696 * "libcall()" function
13697 */
13698 static void
13699f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013700 typval_T *argvars;
13701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702{
13703 libcall_common(argvars, rettv, VAR_STRING);
13704}
13705
13706/*
13707 * "libcallnr()" function
13708 */
13709 static void
13710f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 typval_T *argvars;
13712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713{
13714 libcall_common(argvars, rettv, VAR_NUMBER);
13715}
13716
13717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 * "line(string)" function
13719 */
13720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *argvars;
13723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724{
13725 linenr_T lnum = 0;
13726 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013727 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013729 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 if (fp != NULL)
13731 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733}
13734
13735/*
13736 * "line2byte(lnum)" function
13737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013740 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742{
13743#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#else
13746 linenr_T lnum;
13747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013750 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13753 if (rettv->vval.v_number >= 0)
13754 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#endif
13756}
13757
13758/*
13759 * "lispindent(lnum)" function
13760 */
13761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765{
13766#ifdef FEAT_LISP
13767 pos_T pos;
13768 linenr_T lnum;
13769
13770 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013771 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13773 {
13774 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 curwin->w_cursor = pos;
13777 }
13778 else
13779#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781}
13782
13783/*
13784 * "localtime()" function
13785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013787f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013791 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792}
13793
Bram Moolenaar33570922005-01-25 22:26:29 +000013794static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795
13796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013797get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013798 typval_T *argvars;
13799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 int exact;
13801{
13802 char_u *keys;
13803 char_u *which;
13804 char_u buf[NUMBUFLEN];
13805 char_u *keys_buf = NULL;
13806 char_u *rhs;
13807 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013808 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013809 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013810 mapblock_T *mp;
13811 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812
13813 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814 rettv->v_type = VAR_STRING;
13815 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013817 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 if (*keys == NUL)
13819 return;
13820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013821 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013823 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013824 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013825 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013826 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013827 if (argvars[3].v_type != VAR_UNKNOWN)
13828 get_dict = get_tv_number(&argvars[3]);
13829 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 else
13832 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013833 if (which == NULL)
13834 return;
13835
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 mode = get_map_mode(&which, 0);
13837
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013838 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013839 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013841
13842 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013844 /* Return a string. */
13845 if (rhs != NULL)
13846 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847
Bram Moolenaarbd743252010-10-20 21:23:33 +020013848 }
13849 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13850 {
13851 /* Return a dictionary. */
13852 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13853 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13854 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
Bram Moolenaarbd743252010-10-20 21:23:33 +020013856 dict_add_nr_str(dict, "lhs", 0L, lhs);
13857 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13858 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13859 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13860 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13861 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13862 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013863 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013864 dict_add_nr_str(dict, "mode", 0L, mapmode);
13865
13866 vim_free(lhs);
13867 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 }
13869}
13870
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013871#ifdef FEAT_FLOAT
13872/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013873 * "log()" function
13874 */
13875 static void
13876f_log(argvars, rettv)
13877 typval_T *argvars;
13878 typval_T *rettv;
13879{
13880 float_T f;
13881
13882 rettv->v_type = VAR_FLOAT;
13883 if (get_float_arg(argvars, &f) == OK)
13884 rettv->vval.v_float = log(f);
13885 else
13886 rettv->vval.v_float = 0.0;
13887}
13888
13889/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013890 * "log10()" function
13891 */
13892 static void
13893f_log10(argvars, rettv)
13894 typval_T *argvars;
13895 typval_T *rettv;
13896{
13897 float_T f;
13898
13899 rettv->v_type = VAR_FLOAT;
13900 if (get_float_arg(argvars, &f) == OK)
13901 rettv->vval.v_float = log10(f);
13902 else
13903 rettv->vval.v_float = 0.0;
13904}
13905#endif
13906
Bram Moolenaar1dced572012-04-05 16:54:08 +020013907#ifdef FEAT_LUA
13908/*
13909 * "luaeval()" function
13910 */
13911 static void
13912f_luaeval(argvars, rettv)
13913 typval_T *argvars;
13914 typval_T *rettv;
13915{
13916 char_u *str;
13917 char_u buf[NUMBUFLEN];
13918
13919 str = get_tv_string_buf(&argvars[0], buf);
13920 do_luaeval(str, argvars + 1, rettv);
13921}
13922#endif
13923
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013925 * "map()" function
13926 */
13927 static void
13928f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013929 typval_T *argvars;
13930 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013931{
13932 filter_map(argvars, rettv, TRUE);
13933}
13934
13935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013936 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 */
13938 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013940 typval_T *argvars;
13941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944}
13945
13946/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013947 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 */
13949 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 typval_T *argvars;
13952 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013954 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955}
13956
Bram Moolenaar33570922005-01-25 22:26:29 +000013957static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958
13959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013960find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 int type;
13964{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013966 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 char_u *pat;
13969 regmatch_T regmatch;
13970 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 char_u *save_cpo;
13973 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013974 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013975 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013976 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013977 list_T *l = NULL;
13978 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013979 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013980 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981
13982 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13983 save_cpo = p_cpo;
13984 p_cpo = (char_u *)"";
13985
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013986 rettv->vval.v_number = -1;
13987 if (type == 3)
13988 {
13989 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013990 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013991 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013992 }
13993 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 rettv->v_type = VAR_STRING;
13996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013999 if (argvars[0].v_type == VAR_LIST)
14000 {
14001 if ((l = argvars[0].vval.v_list) == NULL)
14002 goto theend;
14003 li = l->lv_first;
14004 }
14005 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014006 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014007 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014008 len = (long)STRLEN(str);
14009 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14012 if (pat == NULL)
14013 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014014
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 int error = FALSE;
14018
14019 start = get_tv_number_chk(&argvars[2], &error);
14020 if (error)
14021 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014022 if (l != NULL)
14023 {
14024 li = list_find(l, start);
14025 if (li == NULL)
14026 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014027 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014028 }
14029 else
14030 {
14031 if (start < 0)
14032 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014033 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014034 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014035 /* When "count" argument is there ignore matches before "start",
14036 * otherwise skip part of the string. Differs when pattern is "^"
14037 * or "\<". */
14038 if (argvars[3].v_type != VAR_UNKNOWN)
14039 startcol = start;
14040 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014041 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014042 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014043 len -= start;
14044 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014045 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014048 nth = get_tv_number_chk(&argvars[3], &error);
14049 if (error)
14050 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 }
14052
14053 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14054 if (regmatch.regprog != NULL)
14055 {
14056 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014057
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014058 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014059 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014060 if (l != NULL)
14061 {
14062 if (li == NULL)
14063 {
14064 match = FALSE;
14065 break;
14066 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014067 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014068 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014069 if (str == NULL)
14070 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014071 }
14072
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014073 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014074
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014075 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014076 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077 if (l == NULL && !match)
14078 break;
14079
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014080 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014081 if (l != NULL)
14082 {
14083 li = li->li_next;
14084 ++idx;
14085 }
14086 else
14087 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014088#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014089 startcol = (colnr_T)(regmatch.startp[0]
14090 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014091#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014092 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014093#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014094 if (startcol > (colnr_T)len
14095 || str + startcol <= regmatch.startp[0])
14096 {
14097 match = FALSE;
14098 break;
14099 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014100 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014101 }
14102
14103 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014105 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014107 int i;
14108
14109 /* return list with matched string and submatches */
14110 for (i = 0; i < NSUBEXP; ++i)
14111 {
14112 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014113 {
14114 if (list_append_string(rettv->vval.v_list,
14115 (char_u *)"", 0) == FAIL)
14116 break;
14117 }
14118 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014119 regmatch.startp[i],
14120 (int)(regmatch.endp[i] - regmatch.startp[i]))
14121 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014122 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014123 }
14124 }
14125 else if (type == 2)
14126 {
14127 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014128 if (l != NULL)
14129 copy_tv(&li->li_tv, rettv);
14130 else
14131 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014133 }
14134 else if (l != NULL)
14135 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 else
14137 {
14138 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140 (varnumber_T)(regmatch.startp[0] - str);
14141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014144 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 }
14146 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014147 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148 }
14149
14150theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014151 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 p_cpo = save_cpo;
14153}
14154
14155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014156 * "match()" function
14157 */
14158 static void
14159f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 typval_T *argvars;
14161 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162{
14163 find_some_match(argvars, rettv, 1);
14164}
14165
14166/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014167 * "matchadd()" function
14168 */
14169 static void
14170f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014171 typval_T *argvars UNUSED;
14172 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014173{
14174#ifdef FEAT_SEARCH_EXTRA
14175 char_u buf[NUMBUFLEN];
14176 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14177 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14178 int prio = 10; /* default priority */
14179 int id = -1;
14180 int error = FALSE;
14181
14182 rettv->vval.v_number = -1;
14183
14184 if (grp == NULL || pat == NULL)
14185 return;
14186 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014187 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014188 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014189 if (argvars[3].v_type != VAR_UNKNOWN)
14190 id = get_tv_number_chk(&argvars[3], &error);
14191 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014192 if (error == TRUE)
14193 return;
14194 if (id >= 1 && id <= 3)
14195 {
14196 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14197 return;
14198 }
14199
14200 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14201#endif
14202}
14203
14204/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014205 * "matcharg()" function
14206 */
14207 static void
14208f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014209 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014210 typval_T *rettv;
14211{
14212 if (rettv_list_alloc(rettv) == OK)
14213 {
14214#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014215 int id = get_tv_number(&argvars[0]);
14216 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014217
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014218 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014220 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14221 {
14222 list_append_string(rettv->vval.v_list,
14223 syn_id2name(m->hlg_id), -1);
14224 list_append_string(rettv->vval.v_list, m->pattern, -1);
14225 }
14226 else
14227 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014228 list_append_string(rettv->vval.v_list, NULL, -1);
14229 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014230 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014231 }
14232#endif
14233 }
14234}
14235
14236/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014237 * "matchdelete()" function
14238 */
14239 static void
14240f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014241 typval_T *argvars UNUSED;
14242 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014243{
14244#ifdef FEAT_SEARCH_EXTRA
14245 rettv->vval.v_number = match_delete(curwin,
14246 (int)get_tv_number(&argvars[0]), TRUE);
14247#endif
14248}
14249
14250/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014251 * "matchend()" function
14252 */
14253 static void
14254f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014255 typval_T *argvars;
14256 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257{
14258 find_some_match(argvars, rettv, 0);
14259}
14260
14261/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262 * "matchlist()" function
14263 */
14264 static void
14265f_matchlist(argvars, rettv)
14266 typval_T *argvars;
14267 typval_T *rettv;
14268{
14269 find_some_match(argvars, rettv, 3);
14270}
14271
14272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273 * "matchstr()" function
14274 */
14275 static void
14276f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014277 typval_T *argvars;
14278 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014279{
14280 find_some_match(argvars, rettv, 2);
14281}
14282
Bram Moolenaar33570922005-01-25 22:26:29 +000014283static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014284
14285 static void
14286max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014289 int domax;
14290{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014291 long n = 0;
14292 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014294
14295 if (argvars[0].v_type == VAR_LIST)
14296 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 list_T *l;
14298 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014299
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014300 l = argvars[0].vval.v_list;
14301 if (l != NULL)
14302 {
14303 li = l->lv_first;
14304 if (li != NULL)
14305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014307 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014308 {
14309 li = li->li_next;
14310 if (li == NULL)
14311 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014313 if (domax ? i > n : i < n)
14314 n = i;
14315 }
14316 }
14317 }
14318 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014319 else if (argvars[0].v_type == VAR_DICT)
14320 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014321 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014322 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014323 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014324 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014325
14326 d = argvars[0].vval.v_dict;
14327 if (d != NULL)
14328 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014329 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014334 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014336 if (first)
14337 {
14338 n = i;
14339 first = FALSE;
14340 }
14341 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014342 n = i;
14343 }
14344 }
14345 }
14346 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014347 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014348 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014350}
14351
14352/*
14353 * "max()" function
14354 */
14355 static void
14356f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014357 typval_T *argvars;
14358 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014359{
14360 max_min(argvars, rettv, TRUE);
14361}
14362
14363/*
14364 * "min()" function
14365 */
14366 static void
14367f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014368 typval_T *argvars;
14369 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014370{
14371 max_min(argvars, rettv, FALSE);
14372}
14373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014374static int mkdir_recurse __ARGS((char_u *dir, int prot));
14375
14376/*
14377 * Create the directory in which "dir" is located, and higher levels when
14378 * needed.
14379 */
14380 static int
14381mkdir_recurse(dir, prot)
14382 char_u *dir;
14383 int prot;
14384{
14385 char_u *p;
14386 char_u *updir;
14387 int r = FAIL;
14388
14389 /* Get end of directory name in "dir".
14390 * We're done when it's "/" or "c:/". */
14391 p = gettail_sep(dir);
14392 if (p <= get_past_head(dir))
14393 return OK;
14394
14395 /* If the directory exists we're done. Otherwise: create it.*/
14396 updir = vim_strnsave(dir, (int)(p - dir));
14397 if (updir == NULL)
14398 return FAIL;
14399 if (mch_isdir(updir))
14400 r = OK;
14401 else if (mkdir_recurse(updir, prot) == OK)
14402 r = vim_mkdir_emsg(updir, prot);
14403 vim_free(updir);
14404 return r;
14405}
14406
14407#ifdef vim_mkdir
14408/*
14409 * "mkdir()" function
14410 */
14411 static void
14412f_mkdir(argvars, rettv)
14413 typval_T *argvars;
14414 typval_T *rettv;
14415{
14416 char_u *dir;
14417 char_u buf[NUMBUFLEN];
14418 int prot = 0755;
14419
14420 rettv->vval.v_number = FAIL;
14421 if (check_restricted() || check_secure())
14422 return;
14423
14424 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014425 if (*dir == NUL)
14426 rettv->vval.v_number = FAIL;
14427 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014428 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014429 if (*gettail(dir) == NUL)
14430 /* remove trailing slashes */
14431 *gettail_sep(dir) = NUL;
14432
14433 if (argvars[1].v_type != VAR_UNKNOWN)
14434 {
14435 if (argvars[2].v_type != VAR_UNKNOWN)
14436 prot = get_tv_number_chk(&argvars[2], NULL);
14437 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14438 mkdir_recurse(dir, prot);
14439 }
14440 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014441 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014442}
14443#endif
14444
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 * "mode()" function
14447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014450 typval_T *argvars;
14451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014453 char_u buf[3];
14454
14455 buf[1] = NUL;
14456 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458 if (VIsual_active)
14459 {
14460 if (VIsual_select)
14461 buf[0] = VIsual_mode + 's' - 'v';
14462 else
14463 buf[0] = VIsual_mode;
14464 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014465 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014466 || State == CONFIRM)
14467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014469 if (State == ASKMORE)
14470 buf[1] = 'm';
14471 else if (State == CONFIRM)
14472 buf[1] = '?';
14473 }
14474 else if (State == EXTERNCMD)
14475 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 else if (State & INSERT)
14477 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014478#ifdef FEAT_VREPLACE
14479 if (State & VREPLACE_FLAG)
14480 {
14481 buf[0] = 'R';
14482 buf[1] = 'v';
14483 }
14484 else
14485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 if (State & REPLACE_FLAG)
14487 buf[0] = 'R';
14488 else
14489 buf[0] = 'i';
14490 }
14491 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014494 if (exmode_active)
14495 buf[1] = 'v';
14496 }
14497 else if (exmode_active)
14498 {
14499 buf[0] = 'c';
14500 buf[1] = 'e';
14501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014505 if (finish_op)
14506 buf[1] = 'o';
14507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014509 /* Clear out the minor mode when the argument is not a non-zero number or
14510 * non-empty string. */
14511 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014512 buf[1] = NUL;
14513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->vval.v_string = vim_strsave(buf);
14515 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516}
14517
Bram Moolenaar429fa852013-04-15 12:27:36 +020014518#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014519/*
14520 * "mzeval()" function
14521 */
14522 static void
14523f_mzeval(argvars, rettv)
14524 typval_T *argvars;
14525 typval_T *rettv;
14526{
14527 char_u *str;
14528 char_u buf[NUMBUFLEN];
14529
14530 str = get_tv_string_buf(&argvars[0], buf);
14531 do_mzeval(str, rettv);
14532}
Bram Moolenaar75676462013-01-30 14:55:42 +010014533
14534 void
14535mzscheme_call_vim(name, args, rettv)
14536 char_u *name;
14537 typval_T *args;
14538 typval_T *rettv;
14539{
14540 typval_T argvars[3];
14541
14542 argvars[0].v_type = VAR_STRING;
14543 argvars[0].vval.v_string = name;
14544 copy_tv(args, &argvars[1]);
14545 argvars[2].v_type = VAR_UNKNOWN;
14546 f_call(argvars, rettv);
14547 clear_tv(&argvars[1]);
14548}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014549#endif
14550
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552 * "nextnonblank()" function
14553 */
14554 static void
14555f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014556 typval_T *argvars;
14557 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014558{
14559 linenr_T lnum;
14560
14561 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564 {
14565 lnum = 0;
14566 break;
14567 }
14568 if (*skipwhite(ml_get(lnum)) != NUL)
14569 break;
14570 }
14571 rettv->vval.v_number = lnum;
14572}
14573
14574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575 * "nr2char()" function
14576 */
14577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *argvars;
14580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581{
14582 char_u buf[NUMBUFLEN];
14583
14584#ifdef FEAT_MBYTE
14585 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014586 {
14587 int utf8 = 0;
14588
14589 if (argvars[1].v_type != VAR_UNKNOWN)
14590 utf8 = get_tv_number_chk(&argvars[1], NULL);
14591 if (utf8)
14592 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14593 else
14594 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 else
14597#endif
14598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014599 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 buf[1] = NUL;
14601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 rettv->v_type = VAR_STRING;
14603 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014604}
14605
14606/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014607 * "or(expr, expr)" function
14608 */
14609 static void
14610f_or(argvars, rettv)
14611 typval_T *argvars;
14612 typval_T *rettv;
14613{
14614 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14615 | get_tv_number_chk(&argvars[1], NULL);
14616}
14617
14618/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014619 * "pathshorten()" function
14620 */
14621 static void
14622f_pathshorten(argvars, rettv)
14623 typval_T *argvars;
14624 typval_T *rettv;
14625{
14626 char_u *p;
14627
14628 rettv->v_type = VAR_STRING;
14629 p = get_tv_string_chk(&argvars[0]);
14630 if (p == NULL)
14631 rettv->vval.v_string = NULL;
14632 else
14633 {
14634 p = vim_strsave(p);
14635 rettv->vval.v_string = p;
14636 if (p != NULL)
14637 shorten_dir(p);
14638 }
14639}
14640
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014641#ifdef FEAT_FLOAT
14642/*
14643 * "pow()" function
14644 */
14645 static void
14646f_pow(argvars, rettv)
14647 typval_T *argvars;
14648 typval_T *rettv;
14649{
14650 float_T fx, fy;
14651
14652 rettv->v_type = VAR_FLOAT;
14653 if (get_float_arg(argvars, &fx) == OK
14654 && get_float_arg(&argvars[1], &fy) == OK)
14655 rettv->vval.v_float = pow(fx, fy);
14656 else
14657 rettv->vval.v_float = 0.0;
14658}
14659#endif
14660
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014662 * "prevnonblank()" function
14663 */
14664 static void
14665f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 typval_T *argvars;
14667 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668{
14669 linenr_T lnum;
14670
14671 lnum = get_tv_lnum(argvars);
14672 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14673 lnum = 0;
14674 else
14675 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14676 --lnum;
14677 rettv->vval.v_number = lnum;
14678}
14679
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014680#ifdef HAVE_STDARG_H
14681/* This dummy va_list is here because:
14682 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14683 * - locally in the function results in a "used before set" warning
14684 * - using va_start() to initialize it gives "function with fixed args" error */
14685static va_list ap;
14686#endif
14687
Bram Moolenaar8c711452005-01-14 21:53:12 +000014688/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014689 * "printf()" function
14690 */
14691 static void
14692f_printf(argvars, rettv)
14693 typval_T *argvars;
14694 typval_T *rettv;
14695{
14696 rettv->v_type = VAR_STRING;
14697 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014698#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014699 {
14700 char_u buf[NUMBUFLEN];
14701 int len;
14702 char_u *s;
14703 int saved_did_emsg = did_emsg;
14704 char *fmt;
14705
14706 /* Get the required length, allocate the buffer and do it for real. */
14707 did_emsg = FALSE;
14708 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014709 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014710 if (!did_emsg)
14711 {
14712 s = alloc(len + 1);
14713 if (s != NULL)
14714 {
14715 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014716 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014717 }
14718 }
14719 did_emsg |= saved_did_emsg;
14720 }
14721#endif
14722}
14723
14724/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014725 * "pumvisible()" function
14726 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014727 static void
14728f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014729 typval_T *argvars UNUSED;
14730 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014731{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014732#ifdef FEAT_INS_EXPAND
14733 if (pum_visible())
14734 rettv->vval.v_number = 1;
14735#endif
14736}
14737
Bram Moolenaardb913952012-06-29 12:54:53 +020014738#ifdef FEAT_PYTHON3
14739/*
14740 * "py3eval()" function
14741 */
14742 static void
14743f_py3eval(argvars, rettv)
14744 typval_T *argvars;
14745 typval_T *rettv;
14746{
14747 char_u *str;
14748 char_u buf[NUMBUFLEN];
14749
14750 str = get_tv_string_buf(&argvars[0], buf);
14751 do_py3eval(str, rettv);
14752}
14753#endif
14754
14755#ifdef FEAT_PYTHON
14756/*
14757 * "pyeval()" function
14758 */
14759 static void
14760f_pyeval(argvars, rettv)
14761 typval_T *argvars;
14762 typval_T *rettv;
14763{
14764 char_u *str;
14765 char_u buf[NUMBUFLEN];
14766
14767 str = get_tv_string_buf(&argvars[0], buf);
14768 do_pyeval(str, rettv);
14769}
14770#endif
14771
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014772/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773 * "range()" function
14774 */
14775 static void
14776f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014777 typval_T *argvars;
14778 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014779{
14780 long start;
14781 long end;
14782 long stride = 1;
14783 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014787 if (argvars[1].v_type == VAR_UNKNOWN)
14788 {
14789 end = start - 1;
14790 start = 0;
14791 }
14792 else
14793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014797 }
14798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 if (error)
14800 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014801 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014802 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014803 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014804 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014805 else
14806 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014807 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014808 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014809 if (list_append_number(rettv->vval.v_list,
14810 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014812 }
14813}
14814
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014815/*
14816 * "readfile()" function
14817 */
14818 static void
14819f_readfile(argvars, rettv)
14820 typval_T *argvars;
14821 typval_T *rettv;
14822{
14823 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014824 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825 char_u *fname;
14826 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14828 int io_size = sizeof(buf);
14829 int readlen; /* size of last fread() */
14830 char_u *prev = NULL; /* previously read bytes, if any */
14831 long prevlen = 0; /* length of data in prev */
14832 long prevsize = 0; /* size of prev buffer */
14833 long maxline = MAXLNUM;
14834 long cnt = 0;
14835 char_u *p; /* position in buf */
14836 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014837
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014838 if (argvars[1].v_type != VAR_UNKNOWN)
14839 {
14840 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14841 binary = TRUE;
14842 if (argvars[2].v_type != VAR_UNKNOWN)
14843 maxline = get_tv_number(&argvars[2]);
14844 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014845
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014846 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014847 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014848
14849 /* Always open the file in binary mode, library functions have a mind of
14850 * their own about CR-LF conversion. */
14851 fname = get_tv_string(&argvars[0]);
14852 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14853 {
14854 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14855 return;
14856 }
14857
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014858 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014860 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014861
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014862 /* This for loop processes what was read, but is also entered at end
14863 * of file so that either:
14864 * - an incomplete line gets written
14865 * - a "binary" file gets an empty line at the end if it ends in a
14866 * newline. */
14867 for (p = buf, start = buf;
14868 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14869 ++p)
14870 {
14871 if (*p == '\n' || readlen <= 0)
14872 {
14873 listitem_T *li;
14874 char_u *s = NULL;
14875 long_u len = p - start;
14876
14877 /* Finished a line. Remove CRs before NL. */
14878 if (readlen > 0 && !binary)
14879 {
14880 while (len > 0 && start[len - 1] == '\r')
14881 --len;
14882 /* removal may cross back to the "prev" string */
14883 if (len == 0)
14884 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14885 --prevlen;
14886 }
14887 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014888 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014889 else
14890 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014891 /* Change "prev" buffer to be the right size. This way
14892 * the bytes are only copied once, and very long lines are
14893 * allocated only once. */
14894 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014895 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014896 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014897 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014898 prev = NULL; /* the list will own the string */
14899 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 }
14901 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014902 if (s == NULL)
14903 {
14904 do_outofmem_msg((long_u) prevlen + len + 1);
14905 failed = TRUE;
14906 break;
14907 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014908
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014909 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014910 {
14911 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014912 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014913 break;
14914 }
14915 li->li_tv.v_type = VAR_STRING;
14916 li->li_tv.v_lock = 0;
14917 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014918 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014919
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014920 start = p + 1; /* step over newline */
14921 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922 break;
14923 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014924 else if (*p == NUL)
14925 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014926#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14928 * when finding the BF and check the previous two bytes. */
14929 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014930 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014931 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14932 * + 1, these may be in the "prev" string. */
14933 char_u back1 = p >= buf + 1 ? p[-1]
14934 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14935 char_u back2 = p >= buf + 2 ? p[-2]
14936 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14937 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014938
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014939 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014940 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014941 char_u *dest = p - 2;
14942
14943 /* Usually a BOM is at the beginning of a file, and so at
14944 * the beginning of a line; then we can just step over it.
14945 */
14946 if (start == dest)
14947 start = p + 1;
14948 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014949 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014950 /* have to shuffle buf to close gap */
14951 int adjust_prevlen = 0;
14952
14953 if (dest < buf)
14954 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014955 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014956 dest = buf;
14957 }
14958 if (readlen > p - buf + 1)
14959 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14960 readlen -= 3 - adjust_prevlen;
14961 prevlen -= adjust_prevlen;
14962 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014963 }
14964 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014965 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014966#endif
14967 } /* for */
14968
14969 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14970 break;
14971 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014972 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014973 /* There's part of a line in buf, store it in "prev". */
14974 if (p - start + prevlen >= prevsize)
14975 {
14976 /* need bigger "prev" buffer */
14977 char_u *newprev;
14978
14979 /* A common use case is ordinary text files and "prev" gets a
14980 * fragment of a line, so the first allocation is made
14981 * small, to avoid repeatedly 'allocing' large and
14982 * 'reallocing' small. */
14983 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014984 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014985 else
14986 {
14987 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014988 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014989 prevsize = grow50pc > growmin ? grow50pc : growmin;
14990 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014991 newprev = prev == NULL ? alloc(prevsize)
14992 : vim_realloc(prev, prevsize);
14993 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014994 {
14995 do_outofmem_msg((long_u)prevsize);
14996 failed = TRUE;
14997 break;
14998 }
14999 prev = newprev;
15000 }
15001 /* Add the line part to end of "prev". */
15002 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015003 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015004 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015005 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015006
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015007 /*
15008 * For a negative line count use only the lines at the end of the file,
15009 * free the rest.
15010 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015011 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015012 while (cnt > -maxline)
15013 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015014 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015015 --cnt;
15016 }
15017
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015018 if (failed)
15019 {
15020 list_free(rettv->vval.v_list, TRUE);
15021 /* readfile doc says an empty list is returned on error */
15022 rettv->vval.v_list = list_alloc();
15023 }
15024
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015025 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015026 fclose(fd);
15027}
15028
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015029#if defined(FEAT_RELTIME)
15030static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15031
15032/*
15033 * Convert a List to proftime_T.
15034 * Return FAIL when there is something wrong.
15035 */
15036 static int
15037list2proftime(arg, tm)
15038 typval_T *arg;
15039 proftime_T *tm;
15040{
15041 long n1, n2;
15042 int error = FALSE;
15043
15044 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15045 || arg->vval.v_list->lv_len != 2)
15046 return FAIL;
15047 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15048 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15049# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015050 tm->HighPart = n1;
15051 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015052# else
15053 tm->tv_sec = n1;
15054 tm->tv_usec = n2;
15055# endif
15056 return error ? FAIL : OK;
15057}
15058#endif /* FEAT_RELTIME */
15059
15060/*
15061 * "reltime()" function
15062 */
15063 static void
15064f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015065 typval_T *argvars UNUSED;
15066 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015067{
15068#ifdef FEAT_RELTIME
15069 proftime_T res;
15070 proftime_T start;
15071
15072 if (argvars[0].v_type == VAR_UNKNOWN)
15073 {
15074 /* No arguments: get current time. */
15075 profile_start(&res);
15076 }
15077 else if (argvars[1].v_type == VAR_UNKNOWN)
15078 {
15079 if (list2proftime(&argvars[0], &res) == FAIL)
15080 return;
15081 profile_end(&res);
15082 }
15083 else
15084 {
15085 /* Two arguments: compute the difference. */
15086 if (list2proftime(&argvars[0], &start) == FAIL
15087 || list2proftime(&argvars[1], &res) == FAIL)
15088 return;
15089 profile_sub(&res, &start);
15090 }
15091
15092 if (rettv_list_alloc(rettv) == OK)
15093 {
15094 long n1, n2;
15095
15096# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015097 n1 = res.HighPart;
15098 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015099# else
15100 n1 = res.tv_sec;
15101 n2 = res.tv_usec;
15102# endif
15103 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15104 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15105 }
15106#endif
15107}
15108
15109/*
15110 * "reltimestr()" function
15111 */
15112 static void
15113f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015114 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015115 typval_T *rettv;
15116{
15117#ifdef FEAT_RELTIME
15118 proftime_T tm;
15119#endif
15120
15121 rettv->v_type = VAR_STRING;
15122 rettv->vval.v_string = NULL;
15123#ifdef FEAT_RELTIME
15124 if (list2proftime(&argvars[0], &tm) == OK)
15125 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15126#endif
15127}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015128
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15130static void make_connection __ARGS((void));
15131static int check_connection __ARGS((void));
15132
15133 static void
15134make_connection()
15135{
15136 if (X_DISPLAY == NULL
15137# ifdef FEAT_GUI
15138 && !gui.in_use
15139# endif
15140 )
15141 {
15142 x_force_connect = TRUE;
15143 setup_term_clip();
15144 x_force_connect = FALSE;
15145 }
15146}
15147
15148 static int
15149check_connection()
15150{
15151 make_connection();
15152 if (X_DISPLAY == NULL)
15153 {
15154 EMSG(_("E240: No connection to Vim server"));
15155 return FAIL;
15156 }
15157 return OK;
15158}
15159#endif
15160
15161#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015162static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163
15164 static void
15165remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015166 typval_T *argvars;
15167 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015168 int expr;
15169{
15170 char_u *server_name;
15171 char_u *keys;
15172 char_u *r = NULL;
15173 char_u buf[NUMBUFLEN];
15174# ifdef WIN32
15175 HWND w;
15176# else
15177 Window w;
15178# endif
15179
15180 if (check_restricted() || check_secure())
15181 return;
15182
15183# ifdef FEAT_X11
15184 if (check_connection() == FAIL)
15185 return;
15186# endif
15187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 server_name = get_tv_string_chk(&argvars[0]);
15189 if (server_name == NULL)
15190 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 keys = get_tv_string_buf(&argvars[1], buf);
15192# ifdef WIN32
15193 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15194# else
15195 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15196 < 0)
15197# endif
15198 {
15199 if (r != NULL)
15200 EMSG(r); /* sending worked but evaluation failed */
15201 else
15202 EMSG2(_("E241: Unable to send to %s"), server_name);
15203 return;
15204 }
15205
15206 rettv->vval.v_string = r;
15207
15208 if (argvars[2].v_type != VAR_UNKNOWN)
15209 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015211 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015214 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 v.di_tv.v_type = VAR_STRING;
15216 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 idvar = get_tv_string_chk(&argvars[2]);
15218 if (idvar != NULL)
15219 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221 }
15222}
15223#endif
15224
15225/*
15226 * "remote_expr()" function
15227 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015228 static void
15229f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015230 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015232{
15233 rettv->v_type = VAR_STRING;
15234 rettv->vval.v_string = NULL;
15235#ifdef FEAT_CLIENTSERVER
15236 remote_common(argvars, rettv, TRUE);
15237#endif
15238}
15239
15240/*
15241 * "remote_foreground()" function
15242 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015243 static void
15244f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015245 typval_T *argvars UNUSED;
15246 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248#ifdef FEAT_CLIENTSERVER
15249# ifdef WIN32
15250 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015251 {
15252 char_u *server_name = get_tv_string_chk(&argvars[0]);
15253
15254 if (server_name != NULL)
15255 serverForeground(server_name);
15256 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257# else
15258 /* Send a foreground() expression to the server. */
15259 argvars[1].v_type = VAR_STRING;
15260 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15261 argvars[2].v_type = VAR_UNKNOWN;
15262 remote_common(argvars, rettv, TRUE);
15263 vim_free(argvars[1].vval.v_string);
15264# endif
15265#endif
15266}
15267
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 static void
15269f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015270 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015271 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272{
15273#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275 char_u *s = NULL;
15276# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015277 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015279 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280
15281 if (check_restricted() || check_secure())
15282 {
15283 rettv->vval.v_number = -1;
15284 return;
15285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 serverid = get_tv_string_chk(&argvars[0]);
15287 if (serverid == NULL)
15288 {
15289 rettv->vval.v_number = -1;
15290 return; /* type error; errmsg already given */
15291 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015292# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015293 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294 if (n == 0)
15295 rettv->vval.v_number = -1;
15296 else
15297 {
15298 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15299 rettv->vval.v_number = (s != NULL);
15300 }
15301# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302 if (check_connection() == FAIL)
15303 return;
15304
15305 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015306 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307# endif
15308
15309 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 char_u *retvar;
15312
Bram Moolenaar33570922005-01-25 22:26:29 +000015313 v.di_tv.v_type = VAR_STRING;
15314 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 retvar = get_tv_string_chk(&argvars[1]);
15316 if (retvar != NULL)
15317 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015318 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015319 }
15320#else
15321 rettv->vval.v_number = -1;
15322#endif
15323}
15324
Bram Moolenaar0d660222005-01-07 21:51:51 +000015325 static void
15326f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015327 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015328 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329{
15330 char_u *r = NULL;
15331
15332#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 char_u *serverid = get_tv_string_chk(&argvars[0]);
15334
15335 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015336 {
15337# ifdef WIN32
15338 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015339 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015340
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015341 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015342 if (n != 0)
15343 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15344 if (r == NULL)
15345# else
15346 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015347 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348# endif
15349 EMSG(_("E277: Unable to read a server reply"));
15350 }
15351#endif
15352 rettv->v_type = VAR_STRING;
15353 rettv->vval.v_string = r;
15354}
15355
15356/*
15357 * "remote_send()" function
15358 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359 static void
15360f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015363{
15364 rettv->v_type = VAR_STRING;
15365 rettv->vval.v_string = NULL;
15366#ifdef FEAT_CLIENTSERVER
15367 remote_common(argvars, rettv, FALSE);
15368#endif
15369}
15370
15371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015372 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015373 */
15374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015376 typval_T *argvars;
15377 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015378{
Bram Moolenaar33570922005-01-25 22:26:29 +000015379 list_T *l;
15380 listitem_T *item, *item2;
15381 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015382 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015383 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015384 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015385 dict_T *d;
15386 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015387 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015388
Bram Moolenaar8c711452005-01-14 21:53:12 +000015389 if (argvars[0].v_type == VAR_DICT)
15390 {
15391 if (argvars[2].v_type != VAR_UNKNOWN)
15392 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015393 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015394 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 key = get_tv_string_chk(&argvars[1]);
15397 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015399 di = dict_find(d, key, -1);
15400 if (di == NULL)
15401 EMSG2(_(e_dictkey), key);
15402 else
15403 {
15404 *rettv = di->di_tv;
15405 init_tv(&di->di_tv);
15406 dictitem_remove(d, di);
15407 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015408 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015409 }
15410 }
15411 else if (argvars[0].v_type != VAR_LIST)
15412 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015413 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015414 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015416 int error = FALSE;
15417
15418 idx = get_tv_number_chk(&argvars[1], &error);
15419 if (error)
15420 ; /* type error: do nothing, errmsg already given */
15421 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015422 EMSGN(_(e_listidx), idx);
15423 else
15424 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015425 if (argvars[2].v_type == VAR_UNKNOWN)
15426 {
15427 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015428 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015429 *rettv = item->li_tv;
15430 vim_free(item);
15431 }
15432 else
15433 {
15434 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 end = get_tv_number_chk(&argvars[2], &error);
15436 if (error)
15437 ; /* type error: do nothing */
15438 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015439 EMSGN(_(e_listidx), end);
15440 else
15441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015442 int cnt = 0;
15443
15444 for (li = item; li != NULL; li = li->li_next)
15445 {
15446 ++cnt;
15447 if (li == item2)
15448 break;
15449 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015450 if (li == NULL) /* didn't find "item2" after "item" */
15451 EMSG(_(e_invrange));
15452 else
15453 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015454 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015455 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015456 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015457 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015458 l->lv_first = item;
15459 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015460 item->li_prev = NULL;
15461 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015462 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015463 }
15464 }
15465 }
15466 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015467 }
15468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469}
15470
15471/*
15472 * "rename({from}, {to})" function
15473 */
15474 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015476 typval_T *argvars;
15477 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478{
15479 char_u buf[NUMBUFLEN];
15480
15481 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015484 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15485 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486}
15487
15488/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015489 * "repeat()" function
15490 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015491 static void
15492f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015493 typval_T *argvars;
15494 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015495{
15496 char_u *p;
15497 int n;
15498 int slen;
15499 int len;
15500 char_u *r;
15501 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015502
15503 n = get_tv_number(&argvars[1]);
15504 if (argvars[0].v_type == VAR_LIST)
15505 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015506 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015507 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015508 if (list_extend(rettv->vval.v_list,
15509 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015510 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015511 }
15512 else
15513 {
15514 p = get_tv_string(&argvars[0]);
15515 rettv->v_type = VAR_STRING;
15516 rettv->vval.v_string = NULL;
15517
15518 slen = (int)STRLEN(p);
15519 len = slen * n;
15520 if (len <= 0)
15521 return;
15522
15523 r = alloc(len + 1);
15524 if (r != NULL)
15525 {
15526 for (i = 0; i < n; i++)
15527 mch_memmove(r + i * slen, p, (size_t)slen);
15528 r[len] = NUL;
15529 }
15530
15531 rettv->vval.v_string = r;
15532 }
15533}
15534
15535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 * "resolve()" function
15537 */
15538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015540 typval_T *argvars;
15541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542{
15543 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015544#ifdef HAVE_READLINK
15545 char_u *buf = NULL;
15546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549#ifdef FEAT_SHORTCUT
15550 {
15551 char_u *v = NULL;
15552
15553 v = mch_resolve_shortcut(p);
15554 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 }
15559#else
15560# ifdef HAVE_READLINK
15561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 char_u *cpy;
15563 int len;
15564 char_u *remain = NULL;
15565 char_u *q;
15566 int is_relative_to_current = FALSE;
15567 int has_trailing_pathsep = FALSE;
15568 int limit = 100;
15569
15570 p = vim_strsave(p);
15571
15572 if (p[0] == '.' && (vim_ispathsep(p[1])
15573 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15574 is_relative_to_current = TRUE;
15575
15576 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015577 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015580 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582
15583 q = getnextcomp(p);
15584 if (*q != NUL)
15585 {
15586 /* Separate the first path component in "p", and keep the
15587 * remainder (beginning with the path separator). */
15588 remain = vim_strsave(q - 1);
15589 q[-1] = NUL;
15590 }
15591
Bram Moolenaard9462e32011-04-11 21:35:11 +020015592 buf = alloc(MAXPATHL + 1);
15593 if (buf == NULL)
15594 goto fail;
15595
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 for (;;)
15597 {
15598 for (;;)
15599 {
15600 len = readlink((char *)p, (char *)buf, MAXPATHL);
15601 if (len <= 0)
15602 break;
15603 buf[len] = NUL;
15604
15605 if (limit-- == 0)
15606 {
15607 vim_free(p);
15608 vim_free(remain);
15609 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 goto fail;
15612 }
15613
15614 /* Ensure that the result will have a trailing path separator
15615 * if the argument has one. */
15616 if (remain == NULL && has_trailing_pathsep)
15617 add_pathsep(buf);
15618
15619 /* Separate the first path component in the link value and
15620 * concatenate the remainders. */
15621 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15622 if (*q != NUL)
15623 {
15624 if (remain == NULL)
15625 remain = vim_strsave(q - 1);
15626 else
15627 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015628 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 if (cpy != NULL)
15630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 vim_free(remain);
15632 remain = cpy;
15633 }
15634 }
15635 q[-1] = NUL;
15636 }
15637
15638 q = gettail(p);
15639 if (q > p && *q == NUL)
15640 {
15641 /* Ignore trailing path separator. */
15642 q[-1] = NUL;
15643 q = gettail(p);
15644 }
15645 if (q > p && !mch_isFullName(buf))
15646 {
15647 /* symlink is relative to directory of argument */
15648 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15649 if (cpy != NULL)
15650 {
15651 STRCPY(cpy, p);
15652 STRCPY(gettail(cpy), buf);
15653 vim_free(p);
15654 p = cpy;
15655 }
15656 }
15657 else
15658 {
15659 vim_free(p);
15660 p = vim_strsave(buf);
15661 }
15662 }
15663
15664 if (remain == NULL)
15665 break;
15666
15667 /* Append the first path component of "remain" to "p". */
15668 q = getnextcomp(remain + 1);
15669 len = q - remain - (*q != NUL);
15670 cpy = vim_strnsave(p, STRLEN(p) + len);
15671 if (cpy != NULL)
15672 {
15673 STRNCAT(cpy, remain, len);
15674 vim_free(p);
15675 p = cpy;
15676 }
15677 /* Shorten "remain". */
15678 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015679 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 else
15681 {
15682 vim_free(remain);
15683 remain = NULL;
15684 }
15685 }
15686
15687 /* If the result is a relative path name, make it explicitly relative to
15688 * the current directory if and only if the argument had this form. */
15689 if (!vim_ispathsep(*p))
15690 {
15691 if (is_relative_to_current
15692 && *p != NUL
15693 && !(p[0] == '.'
15694 && (p[1] == NUL
15695 || vim_ispathsep(p[1])
15696 || (p[1] == '.'
15697 && (p[2] == NUL
15698 || vim_ispathsep(p[2]))))))
15699 {
15700 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015701 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 if (cpy != NULL)
15703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 vim_free(p);
15705 p = cpy;
15706 }
15707 }
15708 else if (!is_relative_to_current)
15709 {
15710 /* Strip leading "./". */
15711 q = p;
15712 while (q[0] == '.' && vim_ispathsep(q[1]))
15713 q += 2;
15714 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015715 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 }
15717 }
15718
15719 /* Ensure that the result will have no trailing path separator
15720 * if the argument had none. But keep "/" or "//". */
15721 if (!has_trailing_pathsep)
15722 {
15723 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015724 if (after_pathsep(p, q))
15725 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 }
15727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015728 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 }
15730# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732# endif
15733#endif
15734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015735 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736
15737#ifdef HAVE_READLINK
15738fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015739 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015741 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742}
15743
15744/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015745 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 */
15747 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 typval_T *argvars;
15750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751{
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 list_T *l;
15753 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 if (argvars[0].v_type != VAR_LIST)
15756 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015757 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015758 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015759 {
15760 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015761 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015762 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763 while (li != NULL)
15764 {
15765 ni = li->li_prev;
15766 list_append(l, li);
15767 li = ni;
15768 }
15769 rettv->vval.v_list = l;
15770 rettv->v_type = VAR_LIST;
15771 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015772 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774}
15775
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015776#define SP_NOMOVE 0x01 /* don't move cursor */
15777#define SP_REPEAT 0x02 /* repeat to find outer pair */
15778#define SP_RETCOUNT 0x04 /* return matchcount */
15779#define SP_SETPCMARK 0x08 /* set previous context mark */
15780#define SP_START 0x10 /* accept match at start position */
15781#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15782#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015783
Bram Moolenaar33570922005-01-25 22:26:29 +000015784static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785
15786/*
15787 * Get flags for a search function.
15788 * Possibly sets "p_ws".
15789 * Returns BACKWARD, FORWARD or zero (for an error).
15790 */
15791 static int
15792get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015793 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015794 int *flagsp;
15795{
15796 int dir = FORWARD;
15797 char_u *flags;
15798 char_u nbuf[NUMBUFLEN];
15799 int mask;
15800
15801 if (varp->v_type != VAR_UNKNOWN)
15802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015803 flags = get_tv_string_buf_chk(varp, nbuf);
15804 if (flags == NULL)
15805 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806 while (*flags != NUL)
15807 {
15808 switch (*flags)
15809 {
15810 case 'b': dir = BACKWARD; break;
15811 case 'w': p_ws = TRUE; break;
15812 case 'W': p_ws = FALSE; break;
15813 default: mask = 0;
15814 if (flagsp != NULL)
15815 switch (*flags)
15816 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015817 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015818 case 'e': mask = SP_END; break;
15819 case 'm': mask = SP_RETCOUNT; break;
15820 case 'n': mask = SP_NOMOVE; break;
15821 case 'p': mask = SP_SUBPAT; break;
15822 case 'r': mask = SP_REPEAT; break;
15823 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015824 }
15825 if (mask == 0)
15826 {
15827 EMSG2(_(e_invarg2), flags);
15828 dir = 0;
15829 }
15830 else
15831 *flagsp |= mask;
15832 }
15833 if (dir == 0)
15834 break;
15835 ++flags;
15836 }
15837 }
15838 return dir;
15839}
15840
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015842 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015844 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015845search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015847 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015848 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015850 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 char_u *pat;
15852 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015853 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 int save_p_ws = p_ws;
15855 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015856 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015857 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015858 proftime_T tm;
15859#ifdef FEAT_RELTIME
15860 long time_limit = 0;
15861#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015862 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015863 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015865 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015866 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015867 if (dir == 0)
15868 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015869 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015870 if (flags & SP_START)
15871 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015872 if (flags & SP_END)
15873 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015874
Bram Moolenaar76929292008-01-06 19:07:36 +000015875 /* Optional arguments: line number to stop searching and timeout. */
15876 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015877 {
15878 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15879 if (lnum_stop < 0)
15880 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015881#ifdef FEAT_RELTIME
15882 if (argvars[3].v_type != VAR_UNKNOWN)
15883 {
15884 time_limit = get_tv_number_chk(&argvars[3], NULL);
15885 if (time_limit < 0)
15886 goto theend;
15887 }
15888#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015889 }
15890
Bram Moolenaar76929292008-01-06 19:07:36 +000015891#ifdef FEAT_RELTIME
15892 /* Set the time limit, if there is one. */
15893 profile_setlimit(time_limit, &tm);
15894#endif
15895
Bram Moolenaar231334e2005-07-25 20:46:57 +000015896 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015897 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015898 * Check to make sure only those flags are set.
15899 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15900 * flags cannot be set. Check for that condition also.
15901 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015902 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015903 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015904 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015905 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015906 goto theend;
15907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015909 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015910 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015911 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015912 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015914 if (flags & SP_SUBPAT)
15915 retval = subpatnum;
15916 else
15917 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015918 if (flags & SP_SETPCMARK)
15919 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015921 if (match_pos != NULL)
15922 {
15923 /* Store the match cursor position */
15924 match_pos->lnum = pos.lnum;
15925 match_pos->col = pos.col + 1;
15926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 /* "/$" will put the cursor after the end of the line, may need to
15928 * correct that here */
15929 check_cursor();
15930 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015931
15932 /* If 'n' flag is used: restore cursor position. */
15933 if (flags & SP_NOMOVE)
15934 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015935 else
15936 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015937theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015939
15940 return retval;
15941}
15942
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015943#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015944
15945/*
15946 * round() is not in C90, use ceil() or floor() instead.
15947 */
15948 float_T
15949vim_round(f)
15950 float_T f;
15951{
15952 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15953}
15954
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015955/*
15956 * "round({float})" function
15957 */
15958 static void
15959f_round(argvars, rettv)
15960 typval_T *argvars;
15961 typval_T *rettv;
15962{
15963 float_T f;
15964
15965 rettv->v_type = VAR_FLOAT;
15966 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015967 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015968 else
15969 rettv->vval.v_float = 0.0;
15970}
15971#endif
15972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015974 * "screenattr()" function
15975 */
15976 static void
15977f_screenattr(argvars, rettv)
15978 typval_T *argvars UNUSED;
15979 typval_T *rettv;
15980{
15981 int row;
15982 int col;
15983 int c;
15984
15985 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15986 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15987 if (row < 0 || row >= screen_Rows
15988 || col < 0 || col >= screen_Columns)
15989 c = -1;
15990 else
15991 c = ScreenAttrs[LineOffset[row] + col];
15992 rettv->vval.v_number = c;
15993}
15994
15995/*
15996 * "screenchar()" function
15997 */
15998 static void
15999f_screenchar(argvars, rettv)
16000 typval_T *argvars UNUSED;
16001 typval_T *rettv;
16002{
16003 int row;
16004 int col;
16005 int off;
16006 int c;
16007
16008 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16009 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16010 if (row < 0 || row >= screen_Rows
16011 || col < 0 || col >= screen_Columns)
16012 c = -1;
16013 else
16014 {
16015 off = LineOffset[row] + col;
16016#ifdef FEAT_MBYTE
16017 if (enc_utf8 && ScreenLinesUC[off] != 0)
16018 c = ScreenLinesUC[off];
16019 else
16020#endif
16021 c = ScreenLines[off];
16022 }
16023 rettv->vval.v_number = c;
16024}
16025
16026/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016027 * "screencol()" function
16028 *
16029 * First column is 1 to be consistent with virtcol().
16030 */
16031 static void
16032f_screencol(argvars, rettv)
16033 typval_T *argvars UNUSED;
16034 typval_T *rettv;
16035{
16036 rettv->vval.v_number = screen_screencol() + 1;
16037}
16038
16039/*
16040 * "screenrow()" function
16041 */
16042 static void
16043f_screenrow(argvars, rettv)
16044 typval_T *argvars UNUSED;
16045 typval_T *rettv;
16046{
16047 rettv->vval.v_number = screen_screenrow() + 1;
16048}
16049
16050/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016051 * "search()" function
16052 */
16053 static void
16054f_search(argvars, rettv)
16055 typval_T *argvars;
16056 typval_T *rettv;
16057{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016058 int flags = 0;
16059
16060 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061}
16062
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016064 * "searchdecl()" function
16065 */
16066 static void
16067f_searchdecl(argvars, rettv)
16068 typval_T *argvars;
16069 typval_T *rettv;
16070{
16071 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016072 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016073 int error = FALSE;
16074 char_u *name;
16075
16076 rettv->vval.v_number = 1; /* default: FAIL */
16077
16078 name = get_tv_string_chk(&argvars[0]);
16079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016080 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016081 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016082 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16083 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16084 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016085 if (!error && name != NULL)
16086 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016087 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016088}
16089
16090/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016091 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016093 static int
16094searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016095 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016096 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097{
16098 char_u *spat, *mpat, *epat;
16099 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016101 int dir;
16102 int flags = 0;
16103 char_u nbuf1[NUMBUFLEN];
16104 char_u nbuf2[NUMBUFLEN];
16105 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016106 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016107 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016108 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016111 spat = get_tv_string_chk(&argvars[0]);
16112 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16113 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16114 if (spat == NULL || mpat == NULL || epat == NULL)
16115 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 /* Handle the optional fourth argument: flags */
16118 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016119 if (dir == 0)
16120 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016121
16122 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016123 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16124 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016125 if ((flags & (SP_END | SP_SUBPAT)) != 0
16126 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016127 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016128 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016129 goto theend;
16130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016132 /* Using 'r' implies 'W', otherwise it doesn't work. */
16133 if (flags & SP_REPEAT)
16134 p_ws = FALSE;
16135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016136 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016137 if (argvars[3].v_type == VAR_UNKNOWN
16138 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 skip = (char_u *)"";
16140 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 if (argvars[5].v_type != VAR_UNKNOWN)
16144 {
16145 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16146 if (lnum_stop < 0)
16147 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016148#ifdef FEAT_RELTIME
16149 if (argvars[6].v_type != VAR_UNKNOWN)
16150 {
16151 time_limit = get_tv_number_chk(&argvars[6], NULL);
16152 if (time_limit < 0)
16153 goto theend;
16154 }
16155#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016156 }
16157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158 if (skip == NULL)
16159 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016161 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016162 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016163
16164theend:
16165 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016166
16167 return retval;
16168}
16169
16170/*
16171 * "searchpair()" function
16172 */
16173 static void
16174f_searchpair(argvars, rettv)
16175 typval_T *argvars;
16176 typval_T *rettv;
16177{
16178 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16179}
16180
16181/*
16182 * "searchpairpos()" function
16183 */
16184 static void
16185f_searchpairpos(argvars, rettv)
16186 typval_T *argvars;
16187 typval_T *rettv;
16188{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016189 pos_T match_pos;
16190 int lnum = 0;
16191 int col = 0;
16192
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016193 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016194 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016195
16196 if (searchpair_cmn(argvars, &match_pos) > 0)
16197 {
16198 lnum = match_pos.lnum;
16199 col = match_pos.col;
16200 }
16201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016202 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16203 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016204}
16205
16206/*
16207 * Search for a start/middle/end thing.
16208 * Used by searchpair(), see its documentation for the details.
16209 * Returns 0 or -1 for no match,
16210 */
16211 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016212do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16213 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016214 char_u *spat; /* start pattern */
16215 char_u *mpat; /* middle pattern */
16216 char_u *epat; /* end pattern */
16217 int dir; /* BACKWARD or FORWARD */
16218 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016219 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016220 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016221 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016222 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016223{
16224 char_u *save_cpo;
16225 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16226 long retval = 0;
16227 pos_T pos;
16228 pos_T firstpos;
16229 pos_T foundpos;
16230 pos_T save_cursor;
16231 pos_T save_pos;
16232 int n;
16233 int r;
16234 int nest = 1;
16235 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016236 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016237 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016238
16239 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16240 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016241 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016242
Bram Moolenaar76929292008-01-06 19:07:36 +000016243#ifdef FEAT_RELTIME
16244 /* Set the time limit, if there is one. */
16245 profile_setlimit(time_limit, &tm);
16246#endif
16247
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016248 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16249 * start/middle/end (pat3, for the top pair). */
16250 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16251 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16252 if (pat2 == NULL || pat3 == NULL)
16253 goto theend;
16254 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16255 if (*mpat == NUL)
16256 STRCPY(pat3, pat2);
16257 else
16258 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16259 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016260 if (flags & SP_START)
16261 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016262
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 save_cursor = curwin->w_cursor;
16264 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016265 clearpos(&firstpos);
16266 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 pat = pat3;
16268 for (;;)
16269 {
16270 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016271 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16273 /* didn't find it or found the first match again: FAIL */
16274 break;
16275
16276 if (firstpos.lnum == 0)
16277 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016278 if (equalpos(pos, foundpos))
16279 {
16280 /* Found the same position again. Can happen with a pattern that
16281 * has "\zs" at the end and searching backwards. Advance one
16282 * character and try again. */
16283 if (dir == BACKWARD)
16284 decl(&pos);
16285 else
16286 incl(&pos);
16287 }
16288 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016290 /* clear the start flag to avoid getting stuck here */
16291 options &= ~SEARCH_START;
16292
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 /* If the skip pattern matches, ignore this match. */
16294 if (*skip != NUL)
16295 {
16296 save_pos = curwin->w_cursor;
16297 curwin->w_cursor = pos;
16298 r = eval_to_bool(skip, &err, NULL, FALSE);
16299 curwin->w_cursor = save_pos;
16300 if (err)
16301 {
16302 /* Evaluating {skip} caused an error, break here. */
16303 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016304 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 break;
16306 }
16307 if (r)
16308 continue;
16309 }
16310
16311 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16312 {
16313 /* Found end when searching backwards or start when searching
16314 * forward: nested pair. */
16315 ++nest;
16316 pat = pat2; /* nested, don't search for middle */
16317 }
16318 else
16319 {
16320 /* Found end when searching forward or start when searching
16321 * backward: end of (nested) pair; or found middle in outer pair. */
16322 if (--nest == 1)
16323 pat = pat3; /* outer level, search for middle */
16324 }
16325
16326 if (nest == 0)
16327 {
16328 /* Found the match: return matchcount or line number. */
16329 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016330 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016332 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016333 if (flags & SP_SETPCMARK)
16334 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 curwin->w_cursor = pos;
16336 if (!(flags & SP_REPEAT))
16337 break;
16338 nest = 1; /* search for next unmatched */
16339 }
16340 }
16341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016342 if (match_pos != NULL)
16343 {
16344 /* Store the match cursor position */
16345 match_pos->lnum = curwin->w_cursor.lnum;
16346 match_pos->col = curwin->w_cursor.col + 1;
16347 }
16348
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016350 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 curwin->w_cursor = save_cursor;
16352
16353theend:
16354 vim_free(pat2);
16355 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016356 if (p_cpo == empty_option)
16357 p_cpo = save_cpo;
16358 else
16359 /* Darn, evaluating the {skip} expression changed the value. */
16360 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016361
16362 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363}
16364
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016365/*
16366 * "searchpos()" function
16367 */
16368 static void
16369f_searchpos(argvars, rettv)
16370 typval_T *argvars;
16371 typval_T *rettv;
16372{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373 pos_T match_pos;
16374 int lnum = 0;
16375 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016376 int n;
16377 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016378
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016379 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016380 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016381
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016382 n = search_cmn(argvars, &match_pos, &flags);
16383 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016384 {
16385 lnum = match_pos.lnum;
16386 col = match_pos.col;
16387 }
16388
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016389 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16390 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016391 if (flags & SP_SUBPAT)
16392 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016393}
16394
16395
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396 static void
16397f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401#ifdef FEAT_CLIENTSERVER
16402 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 char_u *server = get_tv_string_chk(&argvars[0]);
16404 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405
Bram Moolenaar0d660222005-01-07 21:51:51 +000016406 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 if (server == NULL || reply == NULL)
16408 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 if (check_restricted() || check_secure())
16410 return;
16411# ifdef FEAT_X11
16412 if (check_connection() == FAIL)
16413 return;
16414# endif
16415
16416 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016418 EMSG(_("E258: Unable to send to client"));
16419 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 rettv->vval.v_number = 0;
16422#else
16423 rettv->vval.v_number = -1;
16424#endif
16425}
16426
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427 static void
16428f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016430 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016431{
16432 char_u *r = NULL;
16433
16434#ifdef FEAT_CLIENTSERVER
16435# ifdef WIN32
16436 r = serverGetVimNames();
16437# else
16438 make_connection();
16439 if (X_DISPLAY != NULL)
16440 r = serverGetVimNames(X_DISPLAY);
16441# endif
16442#endif
16443 rettv->v_type = VAR_STRING;
16444 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * "setbufvar()" function
16449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016453 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454{
16455 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016458 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 char_u nbuf[NUMBUFLEN];
16460
16461 if (check_restricted() || check_secure())
16462 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16464 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016465 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466 varp = &argvars[2];
16467
16468 if (buf != NULL && varname != NULL && varp != NULL)
16469 {
16470 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472
16473 if (*varname == '&')
16474 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016475 long numval;
16476 char_u *strval;
16477 int error = FALSE;
16478
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016480 numval = get_tv_number_chk(varp, &error);
16481 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016482 if (!error && strval != NULL)
16483 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484 }
16485 else
16486 {
16487 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16488 if (bufvarname != NULL)
16489 {
16490 STRCPY(bufvarname, "b:");
16491 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016492 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 vim_free(bufvarname);
16494 }
16495 }
16496
16497 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500}
16501
16502/*
16503 * "setcmdpos()" function
16504 */
16505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016507 typval_T *argvars;
16508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 int pos = (int)get_tv_number(&argvars[0]) - 1;
16511
16512 if (pos >= 0)
16513 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514}
16515
16516/*
16517 * "setline()" function
16518 */
16519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016520f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 typval_T *argvars;
16522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523{
16524 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016525 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016526 list_T *l = NULL;
16527 listitem_T *li = NULL;
16528 long added = 0;
16529 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016531 lnum = get_tv_lnum(&argvars[0]);
16532 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016534 l = argvars[1].vval.v_list;
16535 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016537 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016539
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016540 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016541 for (;;)
16542 {
16543 if (l != NULL)
16544 {
16545 /* list argument, get next string */
16546 if (li == NULL)
16547 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016549 li = li->li_next;
16550 }
16551
16552 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016553 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016554 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016555
16556 /* When coming here from Insert mode, sync undo, so that this can be
16557 * undone separately from what was previously inserted. */
16558 if (u_sync_once == 2)
16559 {
16560 u_sync_once = 1; /* notify that u_sync() was called */
16561 u_sync(TRUE);
16562 }
16563
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016564 if (lnum <= curbuf->b_ml.ml_line_count)
16565 {
16566 /* existing line, replace it */
16567 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16568 {
16569 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016570 if (lnum == curwin->w_cursor.lnum)
16571 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016572 rettv->vval.v_number = 0; /* OK */
16573 }
16574 }
16575 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16576 {
16577 /* lnum is one past the last line, append the line */
16578 ++added;
16579 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16580 rettv->vval.v_number = 0; /* OK */
16581 }
16582
16583 if (l == NULL) /* only one string argument */
16584 break;
16585 ++lnum;
16586 }
16587
16588 if (added > 0)
16589 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590}
16591
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016592static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16593
Bram Moolenaar071d4272004-06-13 20:20:40 +000016594/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016595 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016596 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016597 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016599 win_T *wp UNUSED;
16600 typval_T *list_arg UNUSED;
16601 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016602 typval_T *rettv;
16603{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016604#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016605 char_u *act;
16606 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016607#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016608
Bram Moolenaar2641f772005-03-25 21:58:17 +000016609 rettv->vval.v_number = -1;
16610
16611#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016612 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016613 EMSG(_(e_listreq));
16614 else
16615 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016616 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016617
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016618 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016619 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016620 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016621 if (act == NULL)
16622 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016623 if (*act == 'a' || *act == 'r')
16624 action = *act;
16625 }
16626
Bram Moolenaar81484f42012-12-05 15:16:47 +010016627 if (l != NULL && set_errorlist(wp, l, action,
16628 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016629 rettv->vval.v_number = 0;
16630 }
16631#endif
16632}
16633
16634/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016635 * "setloclist()" function
16636 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016637 static void
16638f_setloclist(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
16642 win_T *win;
16643
16644 rettv->vval.v_number = -1;
16645
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016646 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016647 if (win != NULL)
16648 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16649}
16650
16651/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016652 * "setmatches()" function
16653 */
16654 static void
16655f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016656 typval_T *argvars UNUSED;
16657 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016658{
16659#ifdef FEAT_SEARCH_EXTRA
16660 list_T *l;
16661 listitem_T *li;
16662 dict_T *d;
16663
16664 rettv->vval.v_number = -1;
16665 if (argvars[0].v_type != VAR_LIST)
16666 {
16667 EMSG(_(e_listreq));
16668 return;
16669 }
16670 if ((l = argvars[0].vval.v_list) != NULL)
16671 {
16672
16673 /* To some extent make sure that we are dealing with a list from
16674 * "getmatches()". */
16675 li = l->lv_first;
16676 while (li != NULL)
16677 {
16678 if (li->li_tv.v_type != VAR_DICT
16679 || (d = li->li_tv.vval.v_dict) == NULL)
16680 {
16681 EMSG(_(e_invarg));
16682 return;
16683 }
16684 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16685 && dict_find(d, (char_u *)"pattern", -1) != NULL
16686 && dict_find(d, (char_u *)"priority", -1) != NULL
16687 && dict_find(d, (char_u *)"id", -1) != NULL))
16688 {
16689 EMSG(_(e_invarg));
16690 return;
16691 }
16692 li = li->li_next;
16693 }
16694
16695 clear_matches(curwin);
16696 li = l->lv_first;
16697 while (li != NULL)
16698 {
16699 d = li->li_tv.vval.v_dict;
16700 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16701 get_dict_string(d, (char_u *)"pattern", FALSE),
16702 (int)get_dict_number(d, (char_u *)"priority"),
16703 (int)get_dict_number(d, (char_u *)"id"));
16704 li = li->li_next;
16705 }
16706 rettv->vval.v_number = 0;
16707 }
16708#endif
16709}
16710
16711/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016712 * "setpos()" function
16713 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016714 static void
16715f_setpos(argvars, rettv)
16716 typval_T *argvars;
16717 typval_T *rettv;
16718{
16719 pos_T pos;
16720 int fnum;
16721 char_u *name;
16722
Bram Moolenaar08250432008-02-13 11:42:46 +000016723 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016724 name = get_tv_string_chk(argvars);
16725 if (name != NULL)
16726 {
16727 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16728 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016729 if (--pos.col < 0)
16730 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016731 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016732 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016733 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016734 if (fnum == curbuf->b_fnum)
16735 {
16736 curwin->w_cursor = pos;
16737 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016738 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016739 }
16740 else
16741 EMSG(_(e_invarg));
16742 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016743 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16744 {
16745 /* set mark */
16746 if (setmark_pos(name[1], &pos, fnum) == OK)
16747 rettv->vval.v_number = 0;
16748 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016749 else
16750 EMSG(_(e_invarg));
16751 }
16752 }
16753}
16754
16755/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016756 * "setqflist()" function
16757 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016758 static void
16759f_setqflist(argvars, rettv)
16760 typval_T *argvars;
16761 typval_T *rettv;
16762{
16763 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16764}
16765
16766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 * "setreg()" function
16768 */
16769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016770f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 typval_T *argvars;
16772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773{
16774 int regname;
16775 char_u *strregname;
16776 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016777 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 int append;
16779 char_u yank_type;
16780 long block_len;
16781
16782 block_len = -1;
16783 yank_type = MAUTO;
16784 append = FALSE;
16785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016786 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 if (strregname == NULL)
16790 return; /* type error; errmsg already given */
16791 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 if (regname == 0 || regname == '@')
16793 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016797 stropt = get_tv_string_chk(&argvars[2]);
16798 if (stropt == NULL)
16799 return; /* type error */
16800 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801 switch (*stropt)
16802 {
16803 case 'a': case 'A': /* append */
16804 append = TRUE;
16805 break;
16806 case 'v': case 'c': /* character-wise selection */
16807 yank_type = MCHAR;
16808 break;
16809 case 'V': case 'l': /* line-wise selection */
16810 yank_type = MLINE;
16811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 case 'b': case Ctrl_V: /* block-wise selection */
16813 yank_type = MBLOCK;
16814 if (VIM_ISDIGIT(stropt[1]))
16815 {
16816 ++stropt;
16817 block_len = getdigits(&stropt) - 1;
16818 --stropt;
16819 }
16820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 }
16822 }
16823
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016824 if (argvars[1].v_type == VAR_LIST)
16825 {
16826 char_u **lstval;
16827 char_u **curval;
16828 int len = argvars[1].vval.v_list->lv_len;
16829 listitem_T *li;
16830
16831 lstval = (char_u **)alloc(sizeof(char_u *) * (len + 1));
16832 if (lstval == NULL)
16833 return;
16834 curval = lstval;
16835
16836 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16837 li = li->li_next)
16838 {
16839 /* TODO: this may use a static buffer several times. */
16840 strval = get_tv_string_chk(&li->li_tv);
16841 if (strval == NULL)
16842 {
16843 vim_free(lstval);
16844 return;
16845 }
16846 *curval++ = strval;
16847 }
16848 *curval++ = NULL;
16849
16850 write_reg_contents_lst(regname, lstval, -1,
16851 append, yank_type, block_len);
16852 vim_free(lstval);
16853 }
16854 else
16855 {
16856 strval = get_tv_string_chk(&argvars[1]);
16857 if (strval == NULL)
16858 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016859 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016862 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863}
16864
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016865/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016866 * "settabvar()" function
16867 */
16868 static void
16869f_settabvar(argvars, rettv)
16870 typval_T *argvars;
16871 typval_T *rettv;
16872{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016873#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016874 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016875 tabpage_T *tp;
16876#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016877 char_u *varname, *tabvarname;
16878 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016879
16880 rettv->vval.v_number = 0;
16881
16882 if (check_restricted() || check_secure())
16883 return;
16884
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016885#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016886 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016887#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016888 varname = get_tv_string_chk(&argvars[1]);
16889 varp = &argvars[2];
16890
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016891 if (varname != NULL && varp != NULL
16892#ifdef FEAT_WINDOWS
16893 && tp != NULL
16894#endif
16895 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016896 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016897#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016898 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016899 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016900#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016901
16902 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16903 if (tabvarname != NULL)
16904 {
16905 STRCPY(tabvarname, "t:");
16906 STRCPY(tabvarname + 2, varname);
16907 set_var(tabvarname, varp, TRUE);
16908 vim_free(tabvarname);
16909 }
16910
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016911#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016912 /* Restore current tabpage */
16913 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016914 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016915#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016916 }
16917}
16918
16919/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016920 * "settabwinvar()" function
16921 */
16922 static void
16923f_settabwinvar(argvars, rettv)
16924 typval_T *argvars;
16925 typval_T *rettv;
16926{
16927 setwinvar(argvars, rettv, 1);
16928}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929
16930/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016931 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016934f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 typval_T *argvars;
16936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016938 setwinvar(argvars, rettv, 0);
16939}
16940
16941/*
16942 * "setwinvar()" and "settabwinvar()" functions
16943 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016944
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016945 static void
16946setwinvar(argvars, rettv, off)
16947 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016948 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016949 int off;
16950{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 win_T *win;
16952#ifdef FEAT_WINDOWS
16953 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016954 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955#endif
16956 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016959 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960
16961 if (check_restricted() || check_secure())
16962 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016963
16964#ifdef FEAT_WINDOWS
16965 if (off == 1)
16966 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16967 else
16968 tp = curtab;
16969#endif
16970 win = find_win_by_nr(&argvars[off], tp);
16971 varname = get_tv_string_chk(&argvars[off + 1]);
16972 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973
16974 if (win != NULL && varname != NULL && varp != NULL)
16975 {
16976#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016977 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016978 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979#endif
16980
16981 if (*varname == '&')
16982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016983 long numval;
16984 char_u *strval;
16985 int error = FALSE;
16986
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 numval = get_tv_number_chk(varp, &error);
16989 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016990 if (!error && strval != NULL)
16991 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 }
16993 else
16994 {
16995 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16996 if (winvarname != NULL)
16997 {
16998 STRCPY(winvarname, "w:");
16999 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017000 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 vim_free(winvarname);
17002 }
17003 }
17004
17005#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017006 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007#endif
17008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009}
17010
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017011#ifdef FEAT_CRYPT
17012/*
17013 * "sha256({string})" function
17014 */
17015 static void
17016f_sha256(argvars, rettv)
17017 typval_T *argvars;
17018 typval_T *rettv;
17019{
17020 char_u *p;
17021
17022 p = get_tv_string(&argvars[0]);
17023 rettv->vval.v_string = vim_strsave(
17024 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17025 rettv->v_type = VAR_STRING;
17026}
17027#endif /* FEAT_CRYPT */
17028
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017030 * "shellescape({string})" function
17031 */
17032 static void
17033f_shellescape(argvars, rettv)
17034 typval_T *argvars;
17035 typval_T *rettv;
17036{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017037 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017038 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017039 rettv->v_type = VAR_STRING;
17040}
17041
17042/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017043 * shiftwidth() function
17044 */
17045 static void
17046f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017047 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017048 typval_T *rettv;
17049{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017050 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017051}
17052
17053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 */
17056 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 typval_T *argvars;
17059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 p = get_tv_string(&argvars[0]);
17064 rettv->vval.v_string = vim_strsave(p);
17065 simplify_filename(rettv->vval.v_string); /* simplify in place */
17066 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067}
17068
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017069#ifdef FEAT_FLOAT
17070/*
17071 * "sin()" function
17072 */
17073 static void
17074f_sin(argvars, rettv)
17075 typval_T *argvars;
17076 typval_T *rettv;
17077{
17078 float_T f;
17079
17080 rettv->v_type = VAR_FLOAT;
17081 if (get_float_arg(argvars, &f) == OK)
17082 rettv->vval.v_float = sin(f);
17083 else
17084 rettv->vval.v_float = 0.0;
17085}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017086
17087/*
17088 * "sinh()" function
17089 */
17090 static void
17091f_sinh(argvars, rettv)
17092 typval_T *argvars;
17093 typval_T *rettv;
17094{
17095 float_T f;
17096
17097 rettv->v_type = VAR_FLOAT;
17098 if (get_float_arg(argvars, &f) == OK)
17099 rettv->vval.v_float = sinh(f);
17100 else
17101 rettv->vval.v_float = 0.0;
17102}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017103#endif
17104
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105static int
17106#ifdef __BORLANDC__
17107 _RTLENTRYF
17108#endif
17109 item_compare __ARGS((const void *s1, const void *s2));
17110static int
17111#ifdef __BORLANDC__
17112 _RTLENTRYF
17113#endif
17114 item_compare2 __ARGS((const void *s1, const void *s2));
17115
17116static int item_compare_ic;
17117static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017118static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017120static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121#define ITEM_COMPARE_FAIL 999
17122
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017124 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 static int
17127#ifdef __BORLANDC__
17128_RTLENTRYF
17129#endif
17130item_compare(s1, s2)
17131 const void *s1;
17132 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134 char_u *p1, *p2;
17135 char_u *tofree1, *tofree2;
17136 int res;
17137 char_u numbuf1[NUMBUFLEN];
17138 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017140 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17141 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017142 if (p1 == NULL)
17143 p1 = (char_u *)"";
17144 if (p2 == NULL)
17145 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 if (item_compare_ic)
17147 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 res = STRCMP(p1, p2);
17150 vim_free(tofree1);
17151 vim_free(tofree2);
17152 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153}
17154
17155 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156#ifdef __BORLANDC__
17157_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159item_compare2(s1, s2)
17160 const void *s1;
17161 const void *s2;
17162{
17163 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017165 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 /* shortcut after failure in previous call; compare all items equal */
17169 if (item_compare_func_err)
17170 return 0;
17171
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017172 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17173 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17175 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176
17177 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017178 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017179 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17180 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 clear_tv(&argv[0]);
17182 clear_tv(&argv[1]);
17183
17184 if (res == FAIL)
17185 res = ITEM_COMPARE_FAIL;
17186 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017187 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17188 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017189 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 clear_tv(&rettv);
17191 return res;
17192}
17193
17194/*
17195 * "sort({list})" function
17196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017198do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 typval_T *argvars;
17200 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017201 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202{
Bram Moolenaar33570922005-01-25 22:26:29 +000017203 list_T *l;
17204 listitem_T *li;
17205 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 long len;
17207 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208
Bram Moolenaar0d660222005-01-07 21:51:51 +000017209 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017210 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 else
17212 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017214 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017215 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017216 return;
17217 rettv->vval.v_list = l;
17218 rettv->v_type = VAR_LIST;
17219 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 len = list_len(l);
17222 if (len <= 1)
17223 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225 item_compare_ic = FALSE;
17226 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017227 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 if (argvars[1].v_type != VAR_UNKNOWN)
17229 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017230 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233 else
17234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017235 int error = FALSE;
17236
17237 i = get_tv_number_chk(&argvars[1], &error);
17238 if (error)
17239 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017240 if (i == 1)
17241 item_compare_ic = TRUE;
17242 else
17243 item_compare_func = get_tv_string(&argvars[1]);
17244 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017245
17246 if (argvars[2].v_type != VAR_UNKNOWN)
17247 {
17248 /* optional third argument: {dict} */
17249 if (argvars[2].v_type != VAR_DICT)
17250 {
17251 EMSG(_(e_dictreq));
17252 return;
17253 }
17254 item_compare_selfdict = argvars[2].vval.v_dict;
17255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 if (ptrs == NULL)
17261 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262
Bram Moolenaar327aa022014-03-25 18:24:23 +010017263 i = 0;
17264 if (sort)
17265 {
17266 /* sort(): ptrs will be the list to sort */
17267 for (li = l->lv_first; li != NULL; li = li->li_next)
17268 ptrs[i++] = li;
17269
17270 item_compare_func_err = FALSE;
17271 /* test the compare function */
17272 if (item_compare_func != NULL
17273 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017275 EMSG(_("E702: Sort compare function failed"));
17276 else
17277 {
17278 /* Sort the array with item pointers. */
17279 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17280 item_compare_func == NULL ? item_compare : item_compare2);
17281
17282 if (!item_compare_func_err)
17283 {
17284 /* Clear the List and append the items in sorted order. */
17285 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17286 l->lv_len = 0;
17287 for (i = 0; i < len; ++i)
17288 list_append(l, ptrs[i]);
17289 }
17290 }
17291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017294 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17295
17296 /* f_uniq(): ptrs will be a stack of items to remove */
17297 item_compare_func_err = FALSE;
17298 item_compare_func_ptr = item_compare_func
17299 ? item_compare2 : item_compare;
17300
17301 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17302 li = li->li_next)
17303 {
17304 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17305 == 0)
17306 ptrs[i++] = li;
17307 if (item_compare_func_err)
17308 {
17309 EMSG(_("E882: Uniq compare function failed"));
17310 break;
17311 }
17312 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017314 if (!item_compare_func_err)
17315 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017316 while (--i >= 0)
17317 {
17318 li = ptrs[i]->li_next;
17319 ptrs[i]->li_next = li->li_next;
17320 if (li->li_next != NULL)
17321 li->li_next->li_prev = ptrs[i];
17322 else
17323 l->lv_last = ptrs[i];
17324 list_fix_watch(l, li);
17325 listitem_free(li);
17326 l->lv_len--;
17327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329 }
17330
17331 vim_free(ptrs);
17332 }
17333}
17334
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017335/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017336 * "sort({list})" function
17337 */
17338 static void
17339f_sort(argvars, rettv)
17340 typval_T *argvars;
17341 typval_T *rettv;
17342{
17343 do_sort_uniq(argvars, rettv, TRUE);
17344}
17345
17346/*
17347 * "uniq({list})" function
17348 */
17349 static void
17350f_uniq(argvars, rettv)
17351 typval_T *argvars;
17352 typval_T *rettv;
17353{
17354 do_sort_uniq(argvars, rettv, FALSE);
17355}
17356
17357/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017358 * "soundfold({word})" function
17359 */
17360 static void
17361f_soundfold(argvars, rettv)
17362 typval_T *argvars;
17363 typval_T *rettv;
17364{
17365 char_u *s;
17366
17367 rettv->v_type = VAR_STRING;
17368 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017369#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017370 rettv->vval.v_string = eval_soundfold(s);
17371#else
17372 rettv->vval.v_string = vim_strsave(s);
17373#endif
17374}
17375
17376/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017377 * "spellbadword()" function
17378 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017379 static void
17380f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017381 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017382 typval_T *rettv;
17383{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017384 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017385 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017386 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017388 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017389 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017390
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017391#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017392 if (argvars[0].v_type == VAR_UNKNOWN)
17393 {
17394 /* Find the start and length of the badly spelled word. */
17395 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17396 if (len != 0)
17397 word = ml_get_cursor();
17398 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017399 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017400 {
17401 char_u *str = get_tv_string_chk(&argvars[0]);
17402 int capcol = -1;
17403
17404 if (str != NULL)
17405 {
17406 /* Check the argument for spelling. */
17407 while (*str != NUL)
17408 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017409 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017410 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017411 {
17412 word = str;
17413 break;
17414 }
17415 str += len;
17416 }
17417 }
17418 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017419#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017421 list_append_string(rettv->vval.v_list, word, len);
17422 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017423 attr == HLF_SPB ? "bad" :
17424 attr == HLF_SPR ? "rare" :
17425 attr == HLF_SPL ? "local" :
17426 attr == HLF_SPC ? "caps" :
17427 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017428}
17429
17430/*
17431 * "spellsuggest()" function
17432 */
17433 static void
17434f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017435 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017436 typval_T *rettv;
17437{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017438#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017439 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017440 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017441 int maxcount;
17442 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017443 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017444 listitem_T *li;
17445 int need_capital = FALSE;
17446#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017449 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017450
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017451#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017452 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017453 {
17454 str = get_tv_string(&argvars[0]);
17455 if (argvars[1].v_type != VAR_UNKNOWN)
17456 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017457 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017458 if (maxcount <= 0)
17459 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017460 if (argvars[2].v_type != VAR_UNKNOWN)
17461 {
17462 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17463 if (typeerr)
17464 return;
17465 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017466 }
17467 else
17468 maxcount = 25;
17469
Bram Moolenaar4770d092006-01-12 23:22:24 +000017470 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017471
17472 for (i = 0; i < ga.ga_len; ++i)
17473 {
17474 str = ((char_u **)ga.ga_data)[i];
17475
17476 li = listitem_alloc();
17477 if (li == NULL)
17478 vim_free(str);
17479 else
17480 {
17481 li->li_tv.v_type = VAR_STRING;
17482 li->li_tv.v_lock = 0;
17483 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017484 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017485 }
17486 }
17487 ga_clear(&ga);
17488 }
17489#endif
17490}
17491
Bram Moolenaar0d660222005-01-07 21:51:51 +000017492 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017493f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017494 typval_T *argvars;
17495 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017496{
17497 char_u *str;
17498 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017499 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500 regmatch_T regmatch;
17501 char_u patbuf[NUMBUFLEN];
17502 char_u *save_cpo;
17503 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017505 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017506 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017507
17508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17509 save_cpo = p_cpo;
17510 p_cpo = (char_u *)"";
17511
17512 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017513 if (argvars[1].v_type != VAR_UNKNOWN)
17514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017515 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17516 if (pat == NULL)
17517 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017519 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017520 }
17521 if (pat == NULL || *pat == NUL)
17522 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017524 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017526 if (typeerr)
17527 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17530 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017532 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017533 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017535 if (*str == NUL)
17536 match = FALSE; /* empty item at the end */
17537 else
17538 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 if (match)
17540 end = regmatch.startp[0];
17541 else
17542 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017543 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17544 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017546 if (list_append_string(rettv->vval.v_list, str,
17547 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017549 }
17550 if (!match)
17551 break;
17552 /* Advance to just after the match. */
17553 if (regmatch.endp[0] > str)
17554 col = 0;
17555 else
17556 {
17557 /* Don't get stuck at the same match. */
17558#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017559 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560#else
17561 col = 1;
17562#endif
17563 }
17564 str = regmatch.endp[0];
17565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566
Bram Moolenaar473de612013-06-08 18:19:48 +020017567 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571}
17572
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017573#ifdef FEAT_FLOAT
17574/*
17575 * "sqrt()" function
17576 */
17577 static void
17578f_sqrt(argvars, rettv)
17579 typval_T *argvars;
17580 typval_T *rettv;
17581{
17582 float_T f;
17583
17584 rettv->v_type = VAR_FLOAT;
17585 if (get_float_arg(argvars, &f) == OK)
17586 rettv->vval.v_float = sqrt(f);
17587 else
17588 rettv->vval.v_float = 0.0;
17589}
17590
17591/*
17592 * "str2float()" function
17593 */
17594 static void
17595f_str2float(argvars, rettv)
17596 typval_T *argvars;
17597 typval_T *rettv;
17598{
17599 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17600
17601 if (*p == '+')
17602 p = skipwhite(p + 1);
17603 (void)string2float(p, &rettv->vval.v_float);
17604 rettv->v_type = VAR_FLOAT;
17605}
17606#endif
17607
Bram Moolenaar2c932302006-03-18 21:42:09 +000017608/*
17609 * "str2nr()" function
17610 */
17611 static void
17612f_str2nr(argvars, rettv)
17613 typval_T *argvars;
17614 typval_T *rettv;
17615{
17616 int base = 10;
17617 char_u *p;
17618 long n;
17619
17620 if (argvars[1].v_type != VAR_UNKNOWN)
17621 {
17622 base = get_tv_number(&argvars[1]);
17623 if (base != 8 && base != 10 && base != 16)
17624 {
17625 EMSG(_(e_invarg));
17626 return;
17627 }
17628 }
17629
17630 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017631 if (*p == '+')
17632 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017633 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17634 rettv->vval.v_number = n;
17635}
17636
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637#ifdef HAVE_STRFTIME
17638/*
17639 * "strftime({format}[, {time}])" function
17640 */
17641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017643 typval_T *argvars;
17644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645{
17646 char_u result_buf[256];
17647 struct tm *curtime;
17648 time_t seconds;
17649 char_u *p;
17650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017653 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017654 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 seconds = time(NULL);
17656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017657 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 curtime = localtime(&seconds);
17659 /* MSVC returns NULL for an invalid value of seconds. */
17660 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 else
17663 {
17664# ifdef FEAT_MBYTE
17665 vimconv_T conv;
17666 char_u *enc;
17667
17668 conv.vc_type = CONV_NONE;
17669 enc = enc_locale();
17670 convert_setup(&conv, p_enc, enc);
17671 if (conv.vc_type != CONV_NONE)
17672 p = string_convert(&conv, p, NULL);
17673# endif
17674 if (p != NULL)
17675 (void)strftime((char *)result_buf, sizeof(result_buf),
17676 (char *)p, curtime);
17677 else
17678 result_buf[0] = NUL;
17679
17680# ifdef FEAT_MBYTE
17681 if (conv.vc_type != CONV_NONE)
17682 vim_free(p);
17683 convert_setup(&conv, enc, p_enc);
17684 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 else
17687# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689
17690# ifdef FEAT_MBYTE
17691 /* Release conversion descriptors */
17692 convert_setup(&conv, NULL, NULL);
17693 vim_free(enc);
17694# endif
17695 }
17696}
17697#endif
17698
17699/*
17700 * "stridx()" function
17701 */
17702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 typval_T *argvars;
17705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706{
17707 char_u buf[NUMBUFLEN];
17708 char_u *needle;
17709 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017714 needle = get_tv_string_chk(&argvars[1]);
17715 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017716 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 if (needle == NULL || haystack == NULL)
17718 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719
Bram Moolenaar33570922005-01-25 22:26:29 +000017720 if (argvars[2].v_type != VAR_UNKNOWN)
17721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017722 int error = FALSE;
17723
17724 start_idx = get_tv_number_chk(&argvars[2], &error);
17725 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017727 if (start_idx >= 0)
17728 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 }
17730
17731 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17732 if (pos != NULL)
17733 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734}
17735
17736/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017737 * "string()" function
17738 */
17739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017740f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017741 typval_T *argvars;
17742 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017743{
17744 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017745 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017748 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017749 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017750 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752}
17753
17754/*
17755 * "strlen()" function
17756 */
17757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 rettv->vval.v_number = (varnumber_T)(STRLEN(
17763 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764}
17765
17766/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017767 * "strchars()" function
17768 */
17769 static void
17770f_strchars(argvars, rettv)
17771 typval_T *argvars;
17772 typval_T *rettv;
17773{
17774 char_u *s = get_tv_string(&argvars[0]);
17775#ifdef FEAT_MBYTE
17776 varnumber_T len = 0;
17777
17778 while (*s != NUL)
17779 {
17780 mb_cptr2char_adv(&s);
17781 ++len;
17782 }
17783 rettv->vval.v_number = len;
17784#else
17785 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17786#endif
17787}
17788
17789/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017790 * "strdisplaywidth()" function
17791 */
17792 static void
17793f_strdisplaywidth(argvars, rettv)
17794 typval_T *argvars;
17795 typval_T *rettv;
17796{
17797 char_u *s = get_tv_string(&argvars[0]);
17798 int col = 0;
17799
17800 if (argvars[1].v_type != VAR_UNKNOWN)
17801 col = get_tv_number(&argvars[1]);
17802
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017803 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017804}
17805
17806/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017807 * "strwidth()" function
17808 */
17809 static void
17810f_strwidth(argvars, rettv)
17811 typval_T *argvars;
17812 typval_T *rettv;
17813{
17814 char_u *s = get_tv_string(&argvars[0]);
17815
17816 rettv->vval.v_number = (varnumber_T)(
17817#ifdef FEAT_MBYTE
17818 mb_string2cells(s, -1)
17819#else
17820 STRLEN(s)
17821#endif
17822 );
17823}
17824
17825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 * "strpart()" function
17827 */
17828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017829f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 typval_T *argvars;
17831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832{
17833 char_u *p;
17834 int n;
17835 int len;
17836 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017837 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 slen = (int)STRLEN(p);
17841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017842 n = get_tv_number_chk(&argvars[1], &error);
17843 if (error)
17844 len = 0;
17845 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 else
17848 len = slen - n; /* default len: all bytes that are available. */
17849
17850 /*
17851 * Only return the overlap between the specified part and the actual
17852 * string.
17853 */
17854 if (n < 0)
17855 {
17856 len += n;
17857 n = 0;
17858 }
17859 else if (n > slen)
17860 n = slen;
17861 if (len < 0)
17862 len = 0;
17863 else if (n + len > slen)
17864 len = slen - n;
17865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017866 rettv->v_type = VAR_STRING;
17867 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868}
17869
17870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017871 * "strridx()" function
17872 */
17873 static void
17874f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017875 typval_T *argvars;
17876 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877{
17878 char_u buf[NUMBUFLEN];
17879 char_u *needle;
17880 char_u *haystack;
17881 char_u *rest;
17882 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017883 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 needle = get_tv_string_chk(&argvars[1]);
17886 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017887
17888 rettv->vval.v_number = -1;
17889 if (needle == NULL || haystack == NULL)
17890 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017891
17892 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017893 if (argvars[2].v_type != VAR_UNKNOWN)
17894 {
17895 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017896 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017897 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017898 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017899 }
17900 else
17901 end_idx = haystack_len;
17902
Bram Moolenaar0d660222005-01-07 21:51:51 +000017903 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017904 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017905 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017906 lastmatch = haystack + end_idx;
17907 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017909 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 for (rest = haystack; *rest != '\0'; ++rest)
17911 {
17912 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017913 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017914 break;
17915 lastmatch = rest;
17916 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017917 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017918
17919 if (lastmatch == NULL)
17920 rettv->vval.v_number = -1;
17921 else
17922 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17923}
17924
17925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 * "strtrans()" function
17927 */
17928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017929f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017930 typval_T *argvars;
17931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 rettv->v_type = VAR_STRING;
17934 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935}
17936
17937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017938 * "submatch()" function
17939 */
17940 static void
17941f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 typval_T *argvars;
17943 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017944{
Bram Moolenaar41571762014-04-02 19:00:58 +020017945 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017946 int no;
17947 int retList = 0;
17948
17949 no = (int)get_tv_number_chk(&argvars[0], &error);
17950 if (error)
17951 return;
17952 error = FALSE;
17953 if (argvars[1].v_type != VAR_UNKNOWN)
17954 retList = get_tv_number_chk(&argvars[1], &error);
17955 if (error)
17956 return;
17957
17958 if (retList == 0)
17959 {
17960 rettv->v_type = VAR_STRING;
17961 rettv->vval.v_string = reg_submatch(no);
17962 }
17963 else
17964 {
17965 rettv->v_type = VAR_LIST;
17966 rettv->vval.v_list = reg_submatch_list(no);
17967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017968}
17969
17970/*
17971 * "substitute()" function
17972 */
17973 static void
17974f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017975 typval_T *argvars;
17976 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017977{
17978 char_u patbuf[NUMBUFLEN];
17979 char_u subbuf[NUMBUFLEN];
17980 char_u flagsbuf[NUMBUFLEN];
17981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017982 char_u *str = get_tv_string_chk(&argvars[0]);
17983 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17984 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17985 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17986
Bram Moolenaar0d660222005-01-07 21:51:51 +000017987 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017988 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17989 rettv->vval.v_string = NULL;
17990 else
17991 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992}
17993
17994/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017995 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017998f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001{
18002 int id = 0;
18003#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018004 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 long col;
18006 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018007 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018009 lnum = get_tv_lnum(argvars); /* -1 on type error */
18010 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18011 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018013 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018014 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018015 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016#endif
18017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018018 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019}
18020
18021/*
18022 * "synIDattr(id, what [, mode])" function
18023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028{
18029 char_u *p = NULL;
18030#ifdef FEAT_SYN_HL
18031 int id;
18032 char_u *what;
18033 char_u *mode;
18034 char_u modebuf[NUMBUFLEN];
18035 int modec;
18036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018037 id = get_tv_number(&argvars[0]);
18038 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018041 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018043 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 modec = 0; /* replace invalid with current */
18045 }
18046 else
18047 {
18048#ifdef FEAT_GUI
18049 if (gui.in_use)
18050 modec = 'g';
18051 else
18052#endif
18053 if (t_colors > 1)
18054 modec = 'c';
18055 else
18056 modec = 't';
18057 }
18058
18059
18060 switch (TOLOWER_ASC(what[0]))
18061 {
18062 case 'b':
18063 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18064 p = highlight_color(id, what, modec);
18065 else /* bold */
18066 p = highlight_has_attr(id, HL_BOLD, modec);
18067 break;
18068
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018069 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 p = highlight_color(id, what, modec);
18071 break;
18072
18073 case 'i':
18074 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18075 p = highlight_has_attr(id, HL_INVERSE, modec);
18076 else /* italic */
18077 p = highlight_has_attr(id, HL_ITALIC, modec);
18078 break;
18079
18080 case 'n': /* name */
18081 p = get_highlight_name(NULL, id - 1);
18082 break;
18083
18084 case 'r': /* reverse */
18085 p = highlight_has_attr(id, HL_INVERSE, modec);
18086 break;
18087
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018088 case 's':
18089 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18090 p = highlight_color(id, what, modec);
18091 else /* standout */
18092 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 break;
18094
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018095 case 'u':
18096 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18097 /* underline */
18098 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18099 else
18100 /* undercurl */
18101 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 break;
18103 }
18104
18105 if (p != NULL)
18106 p = vim_strsave(p);
18107#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 rettv->v_type = VAR_STRING;
18109 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018110}
18111
18112/*
18113 * "synIDtrans(id)" function
18114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018117 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119{
18120 int id;
18121
18122#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124
18125 if (id > 0)
18126 id = syn_get_final_id(id);
18127 else
18128#endif
18129 id = 0;
18130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018131 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
18134/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018135 * "synconcealed(lnum, col)" function
18136 */
18137 static void
18138f_synconcealed(argvars, rettv)
18139 typval_T *argvars UNUSED;
18140 typval_T *rettv;
18141{
18142#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18143 long lnum;
18144 long col;
18145 int syntax_flags = 0;
18146 int cchar;
18147 int matchid = 0;
18148 char_u str[NUMBUFLEN];
18149#endif
18150
18151 rettv->v_type = VAR_LIST;
18152 rettv->vval.v_list = NULL;
18153
18154#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18155 lnum = get_tv_lnum(argvars); /* -1 on type error */
18156 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18157
18158 vim_memset(str, NUL, sizeof(str));
18159
18160 if (rettv_list_alloc(rettv) != FAIL)
18161 {
18162 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18163 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18164 && curwin->w_p_cole > 0)
18165 {
18166 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18167 syntax_flags = get_syntax_info(&matchid);
18168
18169 /* get the conceal character */
18170 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18171 {
18172 cchar = syn_get_sub_char();
18173 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18174 cchar = lcs_conceal;
18175 if (cchar != NUL)
18176 {
18177# ifdef FEAT_MBYTE
18178 if (has_mbyte)
18179 (*mb_char2bytes)(cchar, str);
18180 else
18181# endif
18182 str[0] = cchar;
18183 }
18184 }
18185 }
18186
18187 list_append_number(rettv->vval.v_list,
18188 (syntax_flags & HL_CONCEAL) != 0);
18189 /* -1 to auto-determine strlen */
18190 list_append_string(rettv->vval.v_list, str, -1);
18191 list_append_number(rettv->vval.v_list, matchid);
18192 }
18193#endif
18194}
18195
18196/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018197 * "synstack(lnum, col)" function
18198 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018199 static void
18200f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018201 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018202 typval_T *rettv;
18203{
18204#ifdef FEAT_SYN_HL
18205 long lnum;
18206 long col;
18207 int i;
18208 int id;
18209#endif
18210
18211 rettv->v_type = VAR_LIST;
18212 rettv->vval.v_list = NULL;
18213
18214#ifdef FEAT_SYN_HL
18215 lnum = get_tv_lnum(argvars); /* -1 on type error */
18216 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18217
18218 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018219 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018220 && rettv_list_alloc(rettv) != FAIL)
18221 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018222 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018223 for (i = 0; ; ++i)
18224 {
18225 id = syn_get_stack_item(i);
18226 if (id < 0)
18227 break;
18228 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18229 break;
18230 }
18231 }
18232#endif
18233}
18234
18235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 * "system()" function
18237 */
18238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018239f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018240 typval_T *argvars;
18241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018243 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018245 char_u *infile = NULL;
18246 char_u buf[NUMBUFLEN];
18247 int err = FALSE;
18248 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018250 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018251 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018253 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018254 {
18255 /*
18256 * Write the string to a temp file, to be used for input of the shell
18257 * command.
18258 */
18259 if ((infile = vim_tempname('i')) == NULL)
18260 {
18261 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018262 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018263 }
18264
18265 fd = mch_fopen((char *)infile, WRITEBIN);
18266 if (fd == NULL)
18267 {
18268 EMSG2(_(e_notopen), infile);
18269 goto done;
18270 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018271 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018272 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018273 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18274 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018275 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018276 else
18277 {
18278 p = get_tv_string_buf_chk(&argvars[1], buf);
18279 if (p == NULL)
18280 {
18281 fclose(fd);
18282 goto done; /* type error; errmsg already given */
18283 }
18284 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18285 err = TRUE;
18286 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018287 if (fclose(fd) != 0)
18288 err = TRUE;
18289 if (err)
18290 {
18291 EMSG(_("E677: Error writing temp file"));
18292 goto done;
18293 }
18294 }
18295
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018296 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18297 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018298
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299#ifdef USE_CR
18300 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018301 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 {
18303 char_u *s;
18304
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018305 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 {
18307 if (*s == CAR)
18308 *s = NL;
18309 }
18310 }
18311#else
18312# ifdef USE_CRNL
18313 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018314 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
18316 char_u *s, *d;
18317
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018318 d = res;
18319 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 {
18321 if (s[0] == CAR && s[1] == NL)
18322 ++s;
18323 *d++ = *s;
18324 }
18325 *d = NUL;
18326 }
18327# endif
18328#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018329
18330done:
18331 if (infile != NULL)
18332 {
18333 mch_remove(infile);
18334 vim_free(infile);
18335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018336 rettv->v_type = VAR_STRING;
18337 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338}
18339
18340/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018341 * "tabpagebuflist()" function
18342 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018343 static void
18344f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018345 typval_T *argvars UNUSED;
18346 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018347{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018348#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018349 tabpage_T *tp;
18350 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018351
18352 if (argvars[0].v_type == VAR_UNKNOWN)
18353 wp = firstwin;
18354 else
18355 {
18356 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18357 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018358 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018359 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018360 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018361 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018362 for (; wp != NULL; wp = wp->w_next)
18363 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018364 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018365 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018366 }
18367#endif
18368}
18369
18370
18371/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018372 * "tabpagenr()" function
18373 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018374 static void
18375f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018376 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018377 typval_T *rettv;
18378{
18379 int nr = 1;
18380#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018381 char_u *arg;
18382
18383 if (argvars[0].v_type != VAR_UNKNOWN)
18384 {
18385 arg = get_tv_string_chk(&argvars[0]);
18386 nr = 0;
18387 if (arg != NULL)
18388 {
18389 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018390 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018391 else
18392 EMSG2(_(e_invexpr2), arg);
18393 }
18394 }
18395 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018396 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018397#endif
18398 rettv->vval.v_number = nr;
18399}
18400
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018401
18402#ifdef FEAT_WINDOWS
18403static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18404
18405/*
18406 * Common code for tabpagewinnr() and winnr().
18407 */
18408 static int
18409get_winnr(tp, argvar)
18410 tabpage_T *tp;
18411 typval_T *argvar;
18412{
18413 win_T *twin;
18414 int nr = 1;
18415 win_T *wp;
18416 char_u *arg;
18417
18418 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18419 if (argvar->v_type != VAR_UNKNOWN)
18420 {
18421 arg = get_tv_string_chk(argvar);
18422 if (arg == NULL)
18423 nr = 0; /* type error; errmsg already given */
18424 else if (STRCMP(arg, "$") == 0)
18425 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18426 else if (STRCMP(arg, "#") == 0)
18427 {
18428 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18429 if (twin == NULL)
18430 nr = 0;
18431 }
18432 else
18433 {
18434 EMSG2(_(e_invexpr2), arg);
18435 nr = 0;
18436 }
18437 }
18438
18439 if (nr > 0)
18440 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18441 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018442 {
18443 if (wp == NULL)
18444 {
18445 /* didn't find it in this tabpage */
18446 nr = 0;
18447 break;
18448 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018449 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018450 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018451 return nr;
18452}
18453#endif
18454
18455/*
18456 * "tabpagewinnr()" function
18457 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018458 static void
18459f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018460 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018461 typval_T *rettv;
18462{
18463 int nr = 1;
18464#ifdef FEAT_WINDOWS
18465 tabpage_T *tp;
18466
18467 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18468 if (tp == NULL)
18469 nr = 0;
18470 else
18471 nr = get_winnr(tp, &argvars[1]);
18472#endif
18473 rettv->vval.v_number = nr;
18474}
18475
18476
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018477/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018478 * "tagfiles()" function
18479 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018480 static void
18481f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018482 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018483 typval_T *rettv;
18484{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018485 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018486 tagname_T tn;
18487 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018490 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018491 fname = alloc(MAXPATHL);
18492 if (fname == NULL)
18493 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018495 for (first = TRUE; ; first = FALSE)
18496 if (get_tagfname(&tn, first, fname) == FAIL
18497 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018498 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018499 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018500 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018501}
18502
18503/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018504 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018505 */
18506 static void
18507f_taglist(argvars, rettv)
18508 typval_T *argvars;
18509 typval_T *rettv;
18510{
18511 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018512
18513 tag_pattern = get_tv_string(&argvars[0]);
18514
18515 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018516 if (*tag_pattern == NUL)
18517 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018519 if (rettv_list_alloc(rettv) == OK)
18520 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018521}
18522
18523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 * "tempname()" function
18525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018527f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018528 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530{
18531 static int x = 'A';
18532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018533 rettv->v_type = VAR_STRING;
18534 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535
18536 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18537 * names. Skip 'I' and 'O', they are used for shell redirection. */
18538 do
18539 {
18540 if (x == 'Z')
18541 x = '0';
18542 else if (x == '9')
18543 x = 'A';
18544 else
18545 {
18546#ifdef EBCDIC
18547 if (x == 'I')
18548 x = 'J';
18549 else if (x == 'R')
18550 x = 'S';
18551 else
18552#endif
18553 ++x;
18554 }
18555 } while (x == 'I' || x == 'O');
18556}
18557
18558/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018559 * "test(list)" function: Just checking the walls...
18560 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018561 static void
18562f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018563 typval_T *argvars UNUSED;
18564 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018565{
18566 /* Used for unit testing. Change the code below to your liking. */
18567#if 0
18568 listitem_T *li;
18569 list_T *l;
18570 char_u *bad, *good;
18571
18572 if (argvars[0].v_type != VAR_LIST)
18573 return;
18574 l = argvars[0].vval.v_list;
18575 if (l == NULL)
18576 return;
18577 li = l->lv_first;
18578 if (li == NULL)
18579 return;
18580 bad = get_tv_string(&li->li_tv);
18581 li = li->li_next;
18582 if (li == NULL)
18583 return;
18584 good = get_tv_string(&li->li_tv);
18585 rettv->vval.v_number = test_edit_score(bad, good);
18586#endif
18587}
18588
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018589#ifdef FEAT_FLOAT
18590/*
18591 * "tan()" function
18592 */
18593 static void
18594f_tan(argvars, rettv)
18595 typval_T *argvars;
18596 typval_T *rettv;
18597{
18598 float_T f;
18599
18600 rettv->v_type = VAR_FLOAT;
18601 if (get_float_arg(argvars, &f) == OK)
18602 rettv->vval.v_float = tan(f);
18603 else
18604 rettv->vval.v_float = 0.0;
18605}
18606
18607/*
18608 * "tanh()" function
18609 */
18610 static void
18611f_tanh(argvars, rettv)
18612 typval_T *argvars;
18613 typval_T *rettv;
18614{
18615 float_T f;
18616
18617 rettv->v_type = VAR_FLOAT;
18618 if (get_float_arg(argvars, &f) == OK)
18619 rettv->vval.v_float = tanh(f);
18620 else
18621 rettv->vval.v_float = 0.0;
18622}
18623#endif
18624
Bram Moolenaard52d9742005-08-21 22:20:28 +000018625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 * "tolower(string)" function
18627 */
18628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018630 typval_T *argvars;
18631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632{
18633 char_u *p;
18634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 p = vim_strsave(get_tv_string(&argvars[0]));
18636 rettv->v_type = VAR_STRING;
18637 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638
18639 if (p != NULL)
18640 while (*p != NUL)
18641 {
18642#ifdef FEAT_MBYTE
18643 int l;
18644
18645 if (enc_utf8)
18646 {
18647 int c, lc;
18648
18649 c = utf_ptr2char(p);
18650 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018651 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 /* TODO: reallocate string when byte count changes. */
18653 if (utf_char2len(lc) == l)
18654 utf_char2bytes(lc, p);
18655 p += l;
18656 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018657 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 p += l; /* skip multi-byte character */
18659 else
18660#endif
18661 {
18662 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18663 ++p;
18664 }
18665 }
18666}
18667
18668/*
18669 * "toupper(string)" function
18670 */
18671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018672f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018673 typval_T *argvars;
18674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018677 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678}
18679
18680/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018681 * "tr(string, fromstr, tostr)" function
18682 */
18683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018685 typval_T *argvars;
18686 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018687{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018688 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018689 char_u *fromstr;
18690 char_u *tostr;
18691 char_u *p;
18692#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018693 int inlen;
18694 int fromlen;
18695 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018696 int idx;
18697 char_u *cpstr;
18698 int cplen;
18699 int first = TRUE;
18700#endif
18701 char_u buf[NUMBUFLEN];
18702 char_u buf2[NUMBUFLEN];
18703 garray_T ga;
18704
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018705 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018706 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18707 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018708
18709 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018710 rettv->v_type = VAR_STRING;
18711 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018712 if (fromstr == NULL || tostr == NULL)
18713 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018714 ga_init2(&ga, (int)sizeof(char), 80);
18715
18716#ifdef FEAT_MBYTE
18717 if (!has_mbyte)
18718#endif
18719 /* not multi-byte: fromstr and tostr must be the same length */
18720 if (STRLEN(fromstr) != STRLEN(tostr))
18721 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018722#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018723error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018724#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018725 EMSG2(_(e_invarg2), fromstr);
18726 ga_clear(&ga);
18727 return;
18728 }
18729
18730 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018731 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018732 {
18733#ifdef FEAT_MBYTE
18734 if (has_mbyte)
18735 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018736 inlen = (*mb_ptr2len)(in_str);
18737 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018738 cplen = inlen;
18739 idx = 0;
18740 for (p = fromstr; *p != NUL; p += fromlen)
18741 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018742 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018743 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018744 {
18745 for (p = tostr; *p != NUL; p += tolen)
18746 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018747 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018748 if (idx-- == 0)
18749 {
18750 cplen = tolen;
18751 cpstr = p;
18752 break;
18753 }
18754 }
18755 if (*p == NUL) /* tostr is shorter than fromstr */
18756 goto error;
18757 break;
18758 }
18759 ++idx;
18760 }
18761
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018762 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018763 {
18764 /* Check that fromstr and tostr have the same number of
18765 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018766 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018767 first = FALSE;
18768 for (p = tostr; *p != NUL; p += tolen)
18769 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018770 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018771 --idx;
18772 }
18773 if (idx != 0)
18774 goto error;
18775 }
18776
18777 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018778 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018779 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018780
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018781 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018782 }
18783 else
18784#endif
18785 {
18786 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018787 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018788 if (p != NULL)
18789 ga_append(&ga, tostr[p - fromstr]);
18790 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018791 ga_append(&ga, *in_str);
18792 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018793 }
18794 }
18795
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018796 /* add a terminating NUL */
18797 ga_grow(&ga, 1);
18798 ga_append(&ga, NUL);
18799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018801}
18802
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018803#ifdef FEAT_FLOAT
18804/*
18805 * "trunc({float})" function
18806 */
18807 static void
18808f_trunc(argvars, rettv)
18809 typval_T *argvars;
18810 typval_T *rettv;
18811{
18812 float_T f;
18813
18814 rettv->v_type = VAR_FLOAT;
18815 if (get_float_arg(argvars, &f) == OK)
18816 /* trunc() is not in C90, use floor() or ceil() instead. */
18817 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18818 else
18819 rettv->vval.v_float = 0.0;
18820}
18821#endif
18822
Bram Moolenaar8299df92004-07-10 09:47:34 +000018823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 * "type(expr)" function
18825 */
18826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018828 typval_T *argvars;
18829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018831 int n;
18832
18833 switch (argvars[0].v_type)
18834 {
18835 case VAR_NUMBER: n = 0; break;
18836 case VAR_STRING: n = 1; break;
18837 case VAR_FUNC: n = 2; break;
18838 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018839 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018840#ifdef FEAT_FLOAT
18841 case VAR_FLOAT: n = 5; break;
18842#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018843 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18844 }
18845 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
18848/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018849 * "undofile(name)" function
18850 */
18851 static void
18852f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018853 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018854 typval_T *rettv;
18855{
18856 rettv->v_type = VAR_STRING;
18857#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018858 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018859 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018860
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018861 if (*fname == NUL)
18862 {
18863 /* If there is no file name there will be no undo file. */
18864 rettv->vval.v_string = NULL;
18865 }
18866 else
18867 {
18868 char_u *ffname = FullName_save(fname, FALSE);
18869
18870 if (ffname != NULL)
18871 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18872 vim_free(ffname);
18873 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018874 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018875#else
18876 rettv->vval.v_string = NULL;
18877#endif
18878}
18879
18880/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018881 * "undotree()" function
18882 */
18883 static void
18884f_undotree(argvars, rettv)
18885 typval_T *argvars UNUSED;
18886 typval_T *rettv;
18887{
18888 if (rettv_dict_alloc(rettv) == OK)
18889 {
18890 dict_T *dict = rettv->vval.v_dict;
18891 list_T *list;
18892
Bram Moolenaar730cde92010-06-27 05:18:54 +020018893 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018894 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018895 dict_add_nr_str(dict, "save_last",
18896 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018897 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18898 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018899 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018900
18901 list = list_alloc();
18902 if (list != NULL)
18903 {
18904 u_eval_tree(curbuf->b_u_oldhead, list);
18905 dict_add_list(dict, "entries", list);
18906 }
18907 }
18908}
18909
18910/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018911 * "values(dict)" function
18912 */
18913 static void
18914f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018915 typval_T *argvars;
18916 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018917{
18918 dict_list(argvars, rettv, 1);
18919}
18920
18921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 * "virtcol(string)" function
18923 */
18924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925f_virtcol(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 colnr_T vcol = 0;
18930 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018931 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018933 fp = var2fpos(&argvars[0], FALSE, &fnum);
18934 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18935 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936 {
18937 getvvcol(curwin, fp, NULL, NULL, &vcol);
18938 ++vcol;
18939 }
18940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018941 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942}
18943
18944/*
18945 * "visualmode()" function
18946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018948f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018949 typval_T *argvars UNUSED;
18950 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 char_u str[2];
18953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018954 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 str[0] = curbuf->b_visual_mode_eval;
18956 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018957 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958
18959 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018960 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962}
18963
18964/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018965 * "wildmenumode()" function
18966 */
18967 static void
18968f_wildmenumode(argvars, rettv)
18969 typval_T *argvars UNUSED;
18970 typval_T *rettv UNUSED;
18971{
18972#ifdef FEAT_WILDMENU
18973 if (wild_menu_showing)
18974 rettv->vval.v_number = 1;
18975#endif
18976}
18977
18978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 * "winbufnr(nr)" function
18980 */
18981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018983 typval_T *argvars;
18984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985{
18986 win_T *wp;
18987
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018988 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
18995/*
18996 * "wincol()" function
18997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018999f_wincol(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 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019004 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005}
19006
19007/*
19008 * "winheight(nr)" function
19009 */
19010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019011f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019012 typval_T *argvars;
19013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014{
19015 win_T *wp;
19016
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019017 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019019 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019021 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022}
19023
19024/*
19025 * "winline()" function
19026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019028f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031{
19032 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019033 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034}
19035
19036/*
19037 * "winnr()" function
19038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019041 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043{
19044 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019045
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019047 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051
19052/*
19053 * "winrestcmd()" function
19054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019057 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059{
19060#ifdef FEAT_WINDOWS
19061 win_T *wp;
19062 int winnr = 1;
19063 garray_T ga;
19064 char_u buf[50];
19065
19066 ga_init2(&ga, (int)sizeof(char), 70);
19067 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19068 {
19069 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19070 ga_concat(&ga, buf);
19071# ifdef FEAT_VERTSPLIT
19072 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19073 ga_concat(&ga, buf);
19074# endif
19075 ++winnr;
19076 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019077 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019083 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084}
19085
19086/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019087 * "winrestview()" function
19088 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019089 static void
19090f_winrestview(argvars, rettv)
19091 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019092 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019093{
19094 dict_T *dict;
19095
19096 if (argvars[0].v_type != VAR_DICT
19097 || (dict = argvars[0].vval.v_dict) == NULL)
19098 EMSG(_(e_invarg));
19099 else
19100 {
19101 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19102 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19103#ifdef FEAT_VIRTUALEDIT
19104 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19105#endif
19106 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019107 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019108
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019109 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019110#ifdef FEAT_DIFF
19111 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19112#endif
19113 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19114 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19115
19116 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019117 win_new_height(curwin, curwin->w_height);
19118# ifdef FEAT_VERTSPLIT
19119 win_new_width(curwin, W_WIDTH(curwin));
19120# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019121 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019122
19123 if (curwin->w_topline == 0)
19124 curwin->w_topline = 1;
19125 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19126 curwin->w_topline = curbuf->b_ml.ml_line_count;
19127#ifdef FEAT_DIFF
19128 check_topfill(curwin, TRUE);
19129#endif
19130 }
19131}
19132
19133/*
19134 * "winsaveview()" function
19135 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019136 static void
19137f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019138 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019139 typval_T *rettv;
19140{
19141 dict_T *dict;
19142
Bram Moolenaara800b422010-06-27 01:15:55 +020019143 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019144 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019145 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019146
19147 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19148 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19149#ifdef FEAT_VIRTUALEDIT
19150 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19151#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019152 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019153 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19154
19155 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19156#ifdef FEAT_DIFF
19157 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19158#endif
19159 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19160 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19161}
19162
19163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 * "winwidth(nr)" function
19165 */
19166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 typval_T *argvars;
19169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170{
19171 win_T *wp;
19172
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019173 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 else
19177#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019180 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181#endif
19182}
19183
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019185 * Write list of strings to file
19186 */
19187 static int
19188write_list(fd, list, binary)
19189 FILE *fd;
19190 list_T *list;
19191 int binary;
19192{
19193 listitem_T *li;
19194 int c;
19195 int ret = OK;
19196 char_u *s;
19197
19198 for (li = list->lv_first; li != NULL; li = li->li_next)
19199 {
19200 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19201 {
19202 if (*s == '\n')
19203 c = putc(NUL, fd);
19204 else
19205 c = putc(*s, fd);
19206 if (c == EOF)
19207 {
19208 ret = FAIL;
19209 break;
19210 }
19211 }
19212 if (!binary || li->li_next != NULL)
19213 if (putc('\n', fd) == EOF)
19214 {
19215 ret = FAIL;
19216 break;
19217 }
19218 if (ret == FAIL)
19219 {
19220 EMSG(_(e_write));
19221 break;
19222 }
19223 }
19224 return ret;
19225}
19226
19227/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019228 * "writefile()" function
19229 */
19230 static void
19231f_writefile(argvars, rettv)
19232 typval_T *argvars;
19233 typval_T *rettv;
19234{
19235 int binary = FALSE;
19236 char_u *fname;
19237 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019238 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019239
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019240 if (check_restricted() || check_secure())
19241 return;
19242
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019243 if (argvars[0].v_type != VAR_LIST)
19244 {
19245 EMSG2(_(e_listarg), "writefile()");
19246 return;
19247 }
19248 if (argvars[0].vval.v_list == NULL)
19249 return;
19250
19251 if (argvars[2].v_type != VAR_UNKNOWN
19252 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19253 binary = TRUE;
19254
19255 /* Always open the file in binary mode, library functions have a mind of
19256 * their own about CR-LF conversion. */
19257 fname = get_tv_string(&argvars[1]);
19258 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19259 {
19260 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19261 ret = -1;
19262 }
19263 else
19264 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019265 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19266 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019267 fclose(fd);
19268 }
19269
19270 rettv->vval.v_number = ret;
19271}
19272
19273/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019274 * "xor(expr, expr)" function
19275 */
19276 static void
19277f_xor(argvars, rettv)
19278 typval_T *argvars;
19279 typval_T *rettv;
19280{
19281 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19282 ^ get_tv_number_chk(&argvars[1], NULL);
19283}
19284
19285
19286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019288 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 */
19290 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019291var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019292 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019293 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019294 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019296 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019298 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299
Bram Moolenaara5525202006-03-02 22:52:09 +000019300 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019301 if (varp->v_type == VAR_LIST)
19302 {
19303 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019304 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019305 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019306 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019307
19308 l = varp->vval.v_list;
19309 if (l == NULL)
19310 return NULL;
19311
19312 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019313 pos.lnum = list_find_nr(l, 0L, &error);
19314 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019315 return NULL; /* invalid line number */
19316
19317 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019318 pos.col = list_find_nr(l, 1L, &error);
19319 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019320 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019321 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019322
19323 /* We accept "$" for the column number: last column. */
19324 li = list_find(l, 1L);
19325 if (li != NULL && li->li_tv.v_type == VAR_STRING
19326 && li->li_tv.vval.v_string != NULL
19327 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19328 pos.col = len + 1;
19329
Bram Moolenaara5525202006-03-02 22:52:09 +000019330 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019331 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019332 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019333 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019334
Bram Moolenaara5525202006-03-02 22:52:09 +000019335#ifdef FEAT_VIRTUALEDIT
19336 /* Get the virtual offset. Defaults to zero. */
19337 pos.coladd = list_find_nr(l, 2L, &error);
19338 if (error)
19339 pos.coladd = 0;
19340#endif
19341
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019342 return &pos;
19343 }
19344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019345 name = get_tv_string_chk(varp);
19346 if (name == NULL)
19347 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019348 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019350 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19351 {
19352 if (VIsual_active)
19353 return &VIsual;
19354 return &curwin->w_cursor;
19355 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019356 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019358 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19360 return NULL;
19361 return pp;
19362 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019363
19364#ifdef FEAT_VIRTUALEDIT
19365 pos.coladd = 0;
19366#endif
19367
Bram Moolenaar477933c2007-07-17 14:32:23 +000019368 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019369 {
19370 pos.col = 0;
19371 if (name[1] == '0') /* "w0": first visible line */
19372 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019373 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019374 pos.lnum = curwin->w_topline;
19375 return &pos;
19376 }
19377 else if (name[1] == '$') /* "w$": last visible line */
19378 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019379 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019380 pos.lnum = curwin->w_botline - 1;
19381 return &pos;
19382 }
19383 }
19384 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019386 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019387 {
19388 pos.lnum = curbuf->b_ml.ml_line_count;
19389 pos.col = 0;
19390 }
19391 else
19392 {
19393 pos.lnum = curwin->w_cursor.lnum;
19394 pos.col = (colnr_T)STRLEN(ml_get_curline());
19395 }
19396 return &pos;
19397 }
19398 return NULL;
19399}
19400
19401/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019402 * Convert list in "arg" into a position and optional file number.
19403 * When "fnump" is NULL there is no file number, only 3 items.
19404 * Note that the column is passed on as-is, the caller may want to decrement
19405 * it to use 1 for the first column.
19406 * Return FAIL when conversion is not possible, doesn't check the position for
19407 * validity.
19408 */
19409 static int
19410list2fpos(arg, posp, fnump)
19411 typval_T *arg;
19412 pos_T *posp;
19413 int *fnump;
19414{
19415 list_T *l = arg->vval.v_list;
19416 long i = 0;
19417 long n;
19418
Bram Moolenaarbde35262006-07-23 20:12:24 +000019419 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19420 * when "fnump" isn't NULL and "coladd" is optional. */
19421 if (arg->v_type != VAR_LIST
19422 || l == NULL
19423 || l->lv_len < (fnump == NULL ? 2 : 3)
19424 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019425 return FAIL;
19426
19427 if (fnump != NULL)
19428 {
19429 n = list_find_nr(l, i++, NULL); /* fnum */
19430 if (n < 0)
19431 return FAIL;
19432 if (n == 0)
19433 n = curbuf->b_fnum; /* current buffer */
19434 *fnump = n;
19435 }
19436
19437 n = list_find_nr(l, i++, NULL); /* lnum */
19438 if (n < 0)
19439 return FAIL;
19440 posp->lnum = n;
19441
19442 n = list_find_nr(l, i++, NULL); /* col */
19443 if (n < 0)
19444 return FAIL;
19445 posp->col = n;
19446
19447#ifdef FEAT_VIRTUALEDIT
19448 n = list_find_nr(l, i, NULL);
19449 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019450 posp->coladd = 0;
19451 else
19452 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019453#endif
19454
19455 return OK;
19456}
19457
19458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 * Get the length of an environment variable name.
19460 * Advance "arg" to the first character after the name.
19461 * Return 0 for error.
19462 */
19463 static int
19464get_env_len(arg)
19465 char_u **arg;
19466{
19467 char_u *p;
19468 int len;
19469
19470 for (p = *arg; vim_isIDc(*p); ++p)
19471 ;
19472 if (p == *arg) /* no name found */
19473 return 0;
19474
19475 len = (int)(p - *arg);
19476 *arg = p;
19477 return len;
19478}
19479
19480/*
19481 * Get the length of the name of a function or internal variable.
19482 * "arg" is advanced to the first non-white character after the name.
19483 * Return 0 if something is wrong.
19484 */
19485 static int
19486get_id_len(arg)
19487 char_u **arg;
19488{
19489 char_u *p;
19490 int len;
19491
19492 /* Find the end of the name. */
19493 for (p = *arg; eval_isnamec(*p); ++p)
19494 ;
19495 if (p == *arg) /* no name found */
19496 return 0;
19497
19498 len = (int)(p - *arg);
19499 *arg = skipwhite(p);
19500
19501 return len;
19502}
19503
19504/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019505 * Get the length of the name of a variable or function.
19506 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019508 * Return -1 if curly braces expansion failed.
19509 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 * If the name contains 'magic' {}'s, expand them and return the
19511 * expanded name in an allocated string via 'alias' - caller must free.
19512 */
19513 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019514get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 char_u **arg;
19516 char_u **alias;
19517 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019518 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519{
19520 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 char_u *p;
19522 char_u *expr_start;
19523 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524
19525 *alias = NULL; /* default to no alias */
19526
19527 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19528 && (*arg)[2] == (int)KE_SNR)
19529 {
19530 /* hard coded <SNR>, already translated */
19531 *arg += 3;
19532 return get_id_len(arg) + 3;
19533 }
19534 len = eval_fname_script(*arg);
19535 if (len > 0)
19536 {
19537 /* literal "<SID>", "s:" or "<SNR>" */
19538 *arg += len;
19539 }
19540
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019542 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019544 p = find_name_end(*arg, &expr_start, &expr_end,
19545 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 if (expr_start != NULL)
19547 {
19548 char_u *temp_string;
19549
19550 if (!evaluate)
19551 {
19552 len += (int)(p - *arg);
19553 *arg = skipwhite(p);
19554 return len;
19555 }
19556
19557 /*
19558 * Include any <SID> etc in the expanded string:
19559 * Thus the -len here.
19560 */
19561 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19562 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019563 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 *alias = temp_string;
19565 *arg = skipwhite(p);
19566 return (int)STRLEN(temp_string);
19567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568
19569 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019570 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 EMSG2(_(e_invexpr2), *arg);
19572
19573 return len;
19574}
19575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576/*
19577 * Find the end of a variable or function name, taking care of magic braces.
19578 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19579 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019580 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 * Return a pointer to just after the name. Equal to "arg" if there is no
19582 * valid name.
19583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019585find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 char_u *arg;
19587 char_u **expr_start;
19588 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019589 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 int mb_nest = 0;
19592 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 char_u *p;
19594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 *expr_start = NULL;
19598 *expr_end = NULL;
19599 }
19600
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019601 /* Quick check for valid starting character. */
19602 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19603 return arg;
19604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 for (p = arg; *p != NUL
19606 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019607 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019608 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019610 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019612 if (*p == '\'')
19613 {
19614 /* skip over 'string' to avoid counting [ and ] inside it. */
19615 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19616 ;
19617 if (*p == NUL)
19618 break;
19619 }
19620 else if (*p == '"')
19621 {
19622 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19623 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19624 if (*p == '\\' && p[1] != NUL)
19625 ++p;
19626 if (*p == NUL)
19627 break;
19628 }
19629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 if (*p == '[')
19633 ++br_nest;
19634 else if (*p == ']')
19635 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019640 if (*p == '{')
19641 {
19642 mb_nest++;
19643 if (expr_start != NULL && *expr_start == NULL)
19644 *expr_start = p;
19645 }
19646 else if (*p == '}')
19647 {
19648 mb_nest--;
19649 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19650 *expr_end = p;
19651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 }
19654
19655 return p;
19656}
19657
19658/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019659 * Expands out the 'magic' {}'s in a variable/function name.
19660 * Note that this can call itself recursively, to deal with
19661 * constructs like foo{bar}{baz}{bam}
19662 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19663 * "in_start" ^
19664 * "expr_start" ^
19665 * "expr_end" ^
19666 * "in_end" ^
19667 *
19668 * Returns a new allocated string, which the caller must free.
19669 * Returns NULL for failure.
19670 */
19671 static char_u *
19672make_expanded_name(in_start, expr_start, expr_end, in_end)
19673 char_u *in_start;
19674 char_u *expr_start;
19675 char_u *expr_end;
19676 char_u *in_end;
19677{
19678 char_u c1;
19679 char_u *retval = NULL;
19680 char_u *temp_result;
19681 char_u *nextcmd = NULL;
19682
19683 if (expr_end == NULL || in_end == NULL)
19684 return NULL;
19685 *expr_start = NUL;
19686 *expr_end = NUL;
19687 c1 = *in_end;
19688 *in_end = NUL;
19689
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019690 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019691 if (temp_result != NULL && nextcmd == NULL)
19692 {
19693 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19694 + (in_end - expr_end) + 1));
19695 if (retval != NULL)
19696 {
19697 STRCPY(retval, in_start);
19698 STRCAT(retval, temp_result);
19699 STRCAT(retval, expr_end + 1);
19700 }
19701 }
19702 vim_free(temp_result);
19703
19704 *in_end = c1; /* put char back for error messages */
19705 *expr_start = '{';
19706 *expr_end = '}';
19707
19708 if (retval != NULL)
19709 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019710 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019711 if (expr_start != NULL)
19712 {
19713 /* Further expansion! */
19714 temp_result = make_expanded_name(retval, expr_start,
19715 expr_end, temp_result);
19716 vim_free(retval);
19717 retval = temp_result;
19718 }
19719 }
19720
19721 return retval;
19722}
19723
19724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 */
19728 static int
19729eval_isnamec(c)
19730 int c;
19731{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019732 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19733}
19734
19735/*
19736 * Return TRUE if character "c" can be used as the first character in a
19737 * variable or function name (excluding '{' and '}').
19738 */
19739 static int
19740eval_isnamec1(c)
19741 int c;
19742{
19743 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744}
19745
19746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 * Set number v: variable to "val".
19748 */
19749 void
19750set_vim_var_nr(idx, val)
19751 int idx;
19752 long val;
19753{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019754 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755}
19756
19757/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019758 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 */
19760 long
19761get_vim_var_nr(idx)
19762 int idx;
19763{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019764 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765}
19766
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019767/*
19768 * Get string v: variable value. Uses a static buffer, can only be used once.
19769 */
19770 char_u *
19771get_vim_var_str(idx)
19772 int idx;
19773{
19774 return get_tv_string(&vimvars[idx].vv_tv);
19775}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019776
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019778 * Get List v: variable value. Caller must take care of reference count when
19779 * needed.
19780 */
19781 list_T *
19782get_vim_var_list(idx)
19783 int idx;
19784{
19785 return vimvars[idx].vv_list;
19786}
19787
19788/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019789 * Set v:char to character "c".
19790 */
19791 void
19792set_vim_var_char(c)
19793 int c;
19794{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019795 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019796
19797#ifdef FEAT_MBYTE
19798 if (has_mbyte)
19799 buf[(*mb_char2bytes)(c, buf)] = NUL;
19800 else
19801#endif
19802 {
19803 buf[0] = c;
19804 buf[1] = NUL;
19805 }
19806 set_vim_var_string(VV_CHAR, buf, -1);
19807}
19808
19809/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019810 * Set v:count to "count" and v:count1 to "count1".
19811 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 */
19813 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019814set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 long count;
19816 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019817 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019819 if (set_prevcount)
19820 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019821 vimvars[VV_COUNT].vv_nr = count;
19822 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823}
19824
19825/*
19826 * Set string v: variable to a copy of "val".
19827 */
19828 void
19829set_vim_var_string(idx, val, len)
19830 int idx;
19831 char_u *val;
19832 int len; /* length of "val" to use or -1 (whole string) */
19833{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019834 /* Need to do this (at least) once, since we can't initialize a union.
19835 * Will always be invoked when "v:progname" is set. */
19836 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19837
Bram Moolenaare9a41262005-01-15 22:18:47 +000019838 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019840 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019842 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019844 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845}
19846
19847/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019848 * Set List v: variable to "val".
19849 */
19850 void
19851set_vim_var_list(idx, val)
19852 int idx;
19853 list_T *val;
19854{
19855 list_unref(vimvars[idx].vv_list);
19856 vimvars[idx].vv_list = val;
19857 if (val != NULL)
19858 ++val->lv_refcount;
19859}
19860
19861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 * Set v:register if needed.
19863 */
19864 void
19865set_reg_var(c)
19866 int c;
19867{
19868 char_u regname;
19869
19870 if (c == 0 || c == ' ')
19871 regname = '"';
19872 else
19873 regname = c;
19874 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019875 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 set_vim_var_string(VV_REG, &regname, 1);
19877}
19878
19879/*
19880 * Get or set v:exception. If "oldval" == NULL, return the current value.
19881 * Otherwise, restore the value to "oldval" and return NULL.
19882 * Must always be called in pairs to save and restore v:exception! Does not
19883 * take care of memory allocations.
19884 */
19885 char_u *
19886v_exception(oldval)
19887 char_u *oldval;
19888{
19889 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019890 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891
Bram Moolenaare9a41262005-01-15 22:18:47 +000019892 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 return NULL;
19894}
19895
19896/*
19897 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19898 * Otherwise, restore the value to "oldval" and return NULL.
19899 * Must always be called in pairs to save and restore v:throwpoint! Does not
19900 * take care of memory allocations.
19901 */
19902 char_u *
19903v_throwpoint(oldval)
19904 char_u *oldval;
19905{
19906 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019907 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908
Bram Moolenaare9a41262005-01-15 22:18:47 +000019909 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910 return NULL;
19911}
19912
19913#if defined(FEAT_AUTOCMD) || defined(PROTO)
19914/*
19915 * Set v:cmdarg.
19916 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19917 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19918 * Must always be called in pairs!
19919 */
19920 char_u *
19921set_cmdarg(eap, oldarg)
19922 exarg_T *eap;
19923 char_u *oldarg;
19924{
19925 char_u *oldval;
19926 char_u *newval;
19927 unsigned len;
19928
Bram Moolenaare9a41262005-01-15 22:18:47 +000019929 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019930 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019932 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019933 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019934 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 }
19936
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019937 if (eap->force_bin == FORCE_BIN)
19938 len = 6;
19939 else if (eap->force_bin == FORCE_NOBIN)
19940 len = 8;
19941 else
19942 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019943
19944 if (eap->read_edit)
19945 len += 7;
19946
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019947 if (eap->force_ff != 0)
19948 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19949# ifdef FEAT_MBYTE
19950 if (eap->force_enc != 0)
19951 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019952 if (eap->bad_char != 0)
19953 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019954# endif
19955
19956 newval = alloc(len + 1);
19957 if (newval == NULL)
19958 return NULL;
19959
19960 if (eap->force_bin == FORCE_BIN)
19961 sprintf((char *)newval, " ++bin");
19962 else if (eap->force_bin == FORCE_NOBIN)
19963 sprintf((char *)newval, " ++nobin");
19964 else
19965 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019966
19967 if (eap->read_edit)
19968 STRCAT(newval, " ++edit");
19969
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019970 if (eap->force_ff != 0)
19971 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19972 eap->cmd + eap->force_ff);
19973# ifdef FEAT_MBYTE
19974 if (eap->force_enc != 0)
19975 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19976 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019977 if (eap->bad_char == BAD_KEEP)
19978 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19979 else if (eap->bad_char == BAD_DROP)
19980 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19981 else if (eap->bad_char != 0)
19982 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019983# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019984 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019985 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986}
19987#endif
19988
19989/*
19990 * Get the value of internal variable "name".
19991 * Return OK or FAIL.
19992 */
19993 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019994get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 char_u *name;
19996 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019998 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019999 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000{
20001 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020002 typval_T *tv = NULL;
20003 typval_T atv;
20004 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006
20007 /* truncate the name, so that we can use strcmp() */
20008 cc = name[len];
20009 name[len] = NUL;
20010
20011 /*
20012 * Check for "b:changedtick".
20013 */
20014 if (STRCMP(name, "b:changedtick") == 0)
20015 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020016 atv.v_type = VAR_NUMBER;
20017 atv.vval.v_number = curbuf->b_changedtick;
20018 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 }
20020
20021 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 * Check for user-defined variables.
20023 */
20024 else
20025 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020026 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 }
20030
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020033 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 ret = FAIL;
20036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020038 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039
20040 name[len] = cc;
20041
20042 return ret;
20043}
20044
20045/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020046 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20047 * Also handle function call with Funcref variable: func(expr)
20048 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20049 */
20050 static int
20051handle_subscript(arg, rettv, evaluate, verbose)
20052 char_u **arg;
20053 typval_T *rettv;
20054 int evaluate; /* do more than finding the end */
20055 int verbose; /* give error messages */
20056{
20057 int ret = OK;
20058 dict_T *selfdict = NULL;
20059 char_u *s;
20060 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020061 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020062
20063 while (ret == OK
20064 && (**arg == '['
20065 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020066 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020067 && !vim_iswhite(*(*arg - 1)))
20068 {
20069 if (**arg == '(')
20070 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020071 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020072 if (evaluate)
20073 {
20074 functv = *rettv;
20075 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020076
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020077 /* Invoke the function. Recursive! */
20078 s = functv.vval.v_string;
20079 }
20080 else
20081 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020082 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020083 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20084 &len, evaluate, selfdict);
20085
20086 /* Clear the funcref afterwards, so that deleting it while
20087 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020088 if (evaluate)
20089 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020090
20091 /* Stop the expression evaluation when immediately aborting on
20092 * error, or when an interrupt occurred or an exception was thrown
20093 * but not caught. */
20094 if (aborting())
20095 {
20096 if (ret == OK)
20097 clear_tv(rettv);
20098 ret = FAIL;
20099 }
20100 dict_unref(selfdict);
20101 selfdict = NULL;
20102 }
20103 else /* **arg == '[' || **arg == '.' */
20104 {
20105 dict_unref(selfdict);
20106 if (rettv->v_type == VAR_DICT)
20107 {
20108 selfdict = rettv->vval.v_dict;
20109 if (selfdict != NULL)
20110 ++selfdict->dv_refcount;
20111 }
20112 else
20113 selfdict = NULL;
20114 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20115 {
20116 clear_tv(rettv);
20117 ret = FAIL;
20118 }
20119 }
20120 }
20121 dict_unref(selfdict);
20122 return ret;
20123}
20124
20125/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020126 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020127 * value).
20128 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020129 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020130alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020131{
Bram Moolenaar33570922005-01-25 22:26:29 +000020132 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020133}
20134
20135/*
20136 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 * The string "s" must have been allocated, it is consumed.
20138 * Return NULL for out of memory, the variable otherwise.
20139 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020140 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 char_u *s;
20143{
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv = alloc_tv();
20147 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149 rettv->v_type = VAR_STRING;
20150 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 }
20152 else
20153 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155}
20156
20157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020158 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020160 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020161free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020162 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163{
20164 if (varp != NULL)
20165 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166 switch (varp->v_type)
20167 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020168 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020169 func_unref(varp->vval.v_string);
20170 /*FALLTHROUGH*/
20171 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020172 vim_free(varp->vval.v_string);
20173 break;
20174 case VAR_LIST:
20175 list_unref(varp->vval.v_list);
20176 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020177 case VAR_DICT:
20178 dict_unref(varp->vval.v_dict);
20179 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020180 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020181#ifdef FEAT_FLOAT
20182 case VAR_FLOAT:
20183#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020184 case VAR_UNKNOWN:
20185 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020186 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020187 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020188 break;
20189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 vim_free(varp);
20191 }
20192}
20193
20194/*
20195 * Free the memory for a variable value and set the value to NULL or 0.
20196 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020197 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
20201 if (varp != NULL)
20202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020203 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020206 func_unref(varp->vval.v_string);
20207 /*FALLTHROUGH*/
20208 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020209 vim_free(varp->vval.v_string);
20210 varp->vval.v_string = NULL;
20211 break;
20212 case VAR_LIST:
20213 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020214 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020215 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020216 case VAR_DICT:
20217 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020218 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020219 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020220 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020221 varp->vval.v_number = 0;
20222 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020223#ifdef FEAT_FLOAT
20224 case VAR_FLOAT:
20225 varp->vval.v_float = 0.0;
20226 break;
20227#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 case VAR_UNKNOWN:
20229 break;
20230 default:
20231 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020233 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 }
20235}
20236
20237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020238 * Set the value of a variable to NULL without freeing items.
20239 */
20240 static void
20241init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243{
20244 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246}
20247
20248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 * Get the number value of a variable.
20250 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020251 * For incompatible types, return 0.
20252 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20253 * caller of incompatible types: it sets *denote to TRUE if "denote"
20254 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255 */
20256 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020260 int error = FALSE;
20261
20262 return get_tv_number_chk(varp, &error); /* return 0L on error */
20263}
20264
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020265 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020266get_tv_number_chk(varp, denote)
20267 typval_T *varp;
20268 int *denote;
20269{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020270 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020272 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020274 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020275 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020276#ifdef FEAT_FLOAT
20277 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020278 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020279 break;
20280#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020281 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020282 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283 break;
20284 case VAR_STRING:
20285 if (varp->vval.v_string != NULL)
20286 vim_str2nr(varp->vval.v_string, NULL, NULL,
20287 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020288 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020289 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020290 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020291 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020292 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020293 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020294 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020295 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020296 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020297 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020299 if (denote == NULL) /* useful for values that must be unsigned */
20300 n = -1;
20301 else
20302 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020303 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304}
20305
20306/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020307 * Get the lnum from the first argument.
20308 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020309 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 */
20311 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020313 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314{
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 linenr_T lnum;
20317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020318 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (lnum == 0) /* no valid number, try using line() */
20320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020321 rettv.v_type = VAR_NUMBER;
20322 f_line(argvars, &rettv);
20323 lnum = rettv.vval.v_number;
20324 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 }
20326 return lnum;
20327}
20328
20329/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020330 * Get the lnum from the first argument.
20331 * Also accepts "$", then "buf" is used.
20332 * Returns 0 on error.
20333 */
20334 static linenr_T
20335get_tv_lnum_buf(argvars, buf)
20336 typval_T *argvars;
20337 buf_T *buf;
20338{
20339 if (argvars[0].v_type == VAR_STRING
20340 && argvars[0].vval.v_string != NULL
20341 && argvars[0].vval.v_string[0] == '$'
20342 && buf != NULL)
20343 return buf->b_ml.ml_line_count;
20344 return get_tv_number_chk(&argvars[0], NULL);
20345}
20346
20347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 * Get the string value of a variable.
20349 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020350 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20351 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 * If the String variable has never been set, return an empty string.
20353 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020354 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20355 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 */
20357 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020359 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360{
20361 static char_u mybuf[NUMBUFLEN];
20362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020363 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364}
20365
20366 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020369 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020371 char_u *res = get_tv_string_buf_chk(varp, buf);
20372
20373 return res != NULL ? res : (char_u *)"";
20374}
20375
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020376 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020377get_tv_string_chk(varp)
20378 typval_T *varp;
20379{
20380 static char_u mybuf[NUMBUFLEN];
20381
20382 return get_tv_string_buf_chk(varp, mybuf);
20383}
20384
20385 static char_u *
20386get_tv_string_buf_chk(varp, buf)
20387 typval_T *varp;
20388 char_u *buf;
20389{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020390 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020392 case VAR_NUMBER:
20393 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20394 return buf;
20395 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020396 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 break;
20398 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020399 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020400 break;
20401 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020402 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020403 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020404#ifdef FEAT_FLOAT
20405 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020406 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020407 break;
20408#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020409 case VAR_STRING:
20410 if (varp->vval.v_string != NULL)
20411 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020412 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020413 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020414 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020415 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020417 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418}
20419
20420/*
20421 * Find variable "name" in the list of variables.
20422 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020423 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020424 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020425 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020428find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020430 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020431 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435
Bram Moolenaara7043832005-01-21 11:56:39 +000020436 ht = find_var_ht(name, &varname);
20437 if (htp != NULL)
20438 *htp = ht;
20439 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020441 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442}
20443
20444/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020445 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020446 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020448 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020449find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020451 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020452 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020453 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020454{
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 hashitem_T *hi;
20456
20457 if (*varname == NUL)
20458 {
20459 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020460 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020462 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020463 case 'g': return &globvars_var;
20464 case 'v': return &vimvars_var;
20465 case 'b': return &curbuf->b_bufvar;
20466 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020467#ifdef FEAT_WINDOWS
20468 case 't': return &curtab->tp_winvar;
20469#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020470 case 'l': return current_funccal == NULL
20471 ? NULL : &current_funccal->l_vars_var;
20472 case 'a': return current_funccal == NULL
20473 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020474 }
20475 return NULL;
20476 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020477
20478 hi = hash_find(ht, varname);
20479 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020480 {
20481 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020482 * worked find the variable again. Don't auto-load a script if it was
20483 * loaded already, otherwise it would be loaded every time when
20484 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020485 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020486 {
20487 /* Note: script_autoload() may make "hi" invalid. It must either
20488 * be obtained again or not used. */
20489 if (!script_autoload(varname, FALSE) || aborting())
20490 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020491 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020492 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020493 if (HASHITEM_EMPTY(hi))
20494 return NULL;
20495 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020497}
20498
20499/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020501 * Set "varname" to the start of name without ':'.
20502 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020504find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 char_u *name;
20506 char_u **varname;
20507{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020508 hashitem_T *hi;
20509
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 if (name[1] != ':')
20511 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020512 /* The name must not start with a colon or #. */
20513 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 return NULL;
20515 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020516
20517 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020518 hi = hash_find(&compat_hashtab, name);
20519 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020520 return &compat_hashtab;
20521
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020523 return &globvarht; /* global variable */
20524 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 }
20526 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020527 if (*name == 'g') /* global variable */
20528 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020529 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20530 */
20531 if (vim_strchr(name + 2, ':') != NULL
20532 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020533 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020535 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020537 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020538#ifdef FEAT_WINDOWS
20539 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020540 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020541#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 if (*name == 'v') /* v: variable */
20543 return &vimvarht;
20544 if (*name == 'a' && current_funccal != NULL) /* function argument */
20545 return &current_funccal->l_avars.dv_hashtab;
20546 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20547 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 if (*name == 's' /* script variable */
20549 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20550 return &SCRIPT_VARS(current_SID);
20551 return NULL;
20552}
20553
20554/*
20555 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020556 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 * Returns NULL when it doesn't exist.
20558 */
20559 char_u *
20560get_var_value(name)
20561 char_u *name;
20562{
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020565 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 if (v == NULL)
20567 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020572 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 * sourcing this script and when executing functions defined in the script.
20574 */
20575 void
20576new_script_vars(id)
20577 scid_T id;
20578{
Bram Moolenaara7043832005-01-21 11:56:39 +000020579 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 hashtab_T *ht;
20581 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020582
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20584 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020585 /* Re-allocating ga_data means that an ht_array pointing to
20586 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020587 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020588 for (i = 1; i <= ga_scripts.ga_len; ++i)
20589 {
20590 ht = &SCRIPT_VARS(i);
20591 if (ht->ht_mask == HT_INIT_SIZE - 1)
20592 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020593 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020594 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020595 }
20596
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 while (ga_scripts.ga_len < id)
20598 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020599 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020600 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020601 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 }
20604 }
20605}
20606
20607/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020608 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20609 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 */
20611 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020612init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020613 dict_T *dict;
20614 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020615 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616{
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020618 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020619 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020620 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020621 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020622 dict_var->di_tv.vval.v_dict = dict;
20623 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020624 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020625 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20626 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627}
20628
20629/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020630 * Unreference a dictionary initialized by init_var_dict().
20631 */
20632 void
20633unref_var_dict(dict)
20634 dict_T *dict;
20635{
20636 /* Now the dict needs to be freed if no one else is using it, go back to
20637 * normal reference counting. */
20638 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20639 dict_unref(dict);
20640}
20641
20642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020644 * Frees all allocated variables and the value they contain.
20645 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 */
20647 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020648vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 hashtab_T *ht;
20650{
20651 vars_clear_ext(ht, TRUE);
20652}
20653
20654/*
20655 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20656 */
20657 static void
20658vars_clear_ext(ht, free_val)
20659 hashtab_T *ht;
20660 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
Bram Moolenaara7043832005-01-21 11:56:39 +000020662 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 hashitem_T *hi;
20664 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665
Bram Moolenaar33570922005-01-25 22:26:29 +000020666 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020667 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020668 for (hi = ht->ht_array; todo > 0; ++hi)
20669 {
20670 if (!HASHITEM_EMPTY(hi))
20671 {
20672 --todo;
20673
Bram Moolenaar33570922005-01-25 22:26:29 +000020674 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020675 * ht_array might change then. hash_clear() takes care of it
20676 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020677 v = HI2DI(hi);
20678 if (free_val)
20679 clear_tv(&v->di_tv);
20680 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20681 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020682 }
20683 }
20684 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020685 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686}
20687
Bram Moolenaara7043832005-01-21 11:56:39 +000020688/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020689 * Delete a variable from hashtab "ht" at item "hi".
20690 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020693delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020694 hashtab_T *ht;
20695 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696{
Bram Moolenaar33570922005-01-25 22:26:29 +000020697 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020698
20699 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020700 clear_tv(&di->di_tv);
20701 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702}
20703
20704/*
20705 * List the value of one internal variable.
20706 */
20707 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020708list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020709 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020711 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020713 char_u *tofree;
20714 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020715 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020716
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020717 current_copyID += COPYID_INC;
20718 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020719 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020720 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020721 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722}
20723
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020725list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 char_u *prefix;
20727 char_u *name;
20728 int type;
20729 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020730 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731{
Bram Moolenaar31859182007-08-14 20:41:13 +000020732 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20733 msg_start();
20734 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 if (name != NULL) /* "a:" vars don't have a name stored */
20736 msg_puts(name);
20737 msg_putchar(' ');
20738 msg_advance(22);
20739 if (type == VAR_NUMBER)
20740 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020741 else if (type == VAR_FUNC)
20742 msg_putchar('*');
20743 else if (type == VAR_LIST)
20744 {
20745 msg_putchar('[');
20746 if (*string == '[')
20747 ++string;
20748 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020749 else if (type == VAR_DICT)
20750 {
20751 msg_putchar('{');
20752 if (*string == '{')
20753 ++string;
20754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 else
20756 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020757
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759
20760 if (type == VAR_FUNC)
20761 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020762 if (*first)
20763 {
20764 msg_clr_eos();
20765 *first = FALSE;
20766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767}
20768
20769/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020770 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 * If the variable already exists, the value is updated.
20772 * Otherwise the variable is created.
20773 */
20774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020775set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020777 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020778 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779{
Bram Moolenaar33570922005-01-25 22:26:29 +000020780 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020782 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020784 ht = find_var_ht(name, &varname);
20785 if (ht == NULL || *varname == NUL)
20786 {
20787 EMSG2(_(e_illvar), name);
20788 return;
20789 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020790 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020791
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020792 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20793 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020794
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020797 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020798 if (var_check_ro(v->di_flags, name)
20799 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020800 return;
20801 if (v->di_tv.v_type != tv->v_type
20802 && !((v->di_tv.v_type == VAR_STRING
20803 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020805 || tv->v_type == VAR_NUMBER))
20806#ifdef FEAT_FLOAT
20807 && !((v->di_tv.v_type == VAR_NUMBER
20808 || v->di_tv.v_type == VAR_FLOAT)
20809 && (tv->v_type == VAR_NUMBER
20810 || tv->v_type == VAR_FLOAT))
20811#endif
20812 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020813 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020814 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020815 return;
20816 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020817
20818 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020819 * Handle setting internal v: variables separately: we don't change
20820 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020821 */
20822 if (ht == &vimvarht)
20823 {
20824 if (v->di_tv.v_type == VAR_STRING)
20825 {
20826 vim_free(v->di_tv.vval.v_string);
20827 if (copy || tv->v_type != VAR_STRING)
20828 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20829 else
20830 {
20831 /* Take over the string to avoid an extra alloc/free. */
20832 v->di_tv.vval.v_string = tv->vval.v_string;
20833 tv->vval.v_string = NULL;
20834 }
20835 }
20836 else if (v->di_tv.v_type != VAR_NUMBER)
20837 EMSG2(_(e_intern2), "set_var()");
20838 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020839 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020840 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020841 if (STRCMP(varname, "searchforward") == 0)
20842 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020843#ifdef FEAT_SEARCH_EXTRA
20844 else if (STRCMP(varname, "hlsearch") == 0)
20845 {
20846 no_hlsearch = !v->di_tv.vval.v_number;
20847 redraw_all_later(SOME_VALID);
20848 }
20849#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020850 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020851 return;
20852 }
20853
20854 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 }
20856 else /* add a new variable */
20857 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020858 /* Can't add "v:" variable. */
20859 if (ht == &vimvarht)
20860 {
20861 EMSG2(_(e_illvar), name);
20862 return;
20863 }
20864
Bram Moolenaar92124a32005-06-17 22:03:40 +000020865 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020866 if (!valid_varname(varname))
20867 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020868
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020869 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20870 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020871 if (v == NULL)
20872 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020873 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020874 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020876 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 return;
20878 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020879 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020882 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020883 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020884 else
20885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020886 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020887 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020888 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890}
20891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020892/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020893 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020894 * Also give an error message.
20895 */
20896 static int
20897var_check_ro(flags, name)
20898 int flags;
20899 char_u *name;
20900{
20901 if (flags & DI_FLAGS_RO)
20902 {
20903 EMSG2(_(e_readonlyvar), name);
20904 return TRUE;
20905 }
20906 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20907 {
20908 EMSG2(_(e_readonlysbx), name);
20909 return TRUE;
20910 }
20911 return FALSE;
20912}
20913
20914/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020915 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20916 * Also give an error message.
20917 */
20918 static int
20919var_check_fixed(flags, name)
20920 int flags;
20921 char_u *name;
20922{
20923 if (flags & DI_FLAGS_FIX)
20924 {
20925 EMSG2(_("E795: Cannot delete variable %s"), name);
20926 return TRUE;
20927 }
20928 return FALSE;
20929}
20930
20931/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020932 * Check if a funcref is assigned to a valid variable name.
20933 * Return TRUE and give an error if not.
20934 */
20935 static int
20936var_check_func_name(name, new_var)
20937 char_u *name; /* points to start of variable name */
20938 int new_var; /* TRUE when creating the variable */
20939{
20940 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20941 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20942 ? name[2] : name[0]))
20943 {
20944 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20945 name);
20946 return TRUE;
20947 }
20948 /* Don't allow hiding a function. When "v" is not NULL we might be
20949 * assigning another function to the same var, the type is checked
20950 * below. */
20951 if (new_var && function_exists(name))
20952 {
20953 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20954 name);
20955 return TRUE;
20956 }
20957 return FALSE;
20958}
20959
20960/*
20961 * Check if a variable name is valid.
20962 * Return FALSE and give an error if not.
20963 */
20964 static int
20965valid_varname(varname)
20966 char_u *varname;
20967{
20968 char_u *p;
20969
20970 for (p = varname; *p != NUL; ++p)
20971 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20972 && *p != AUTOLOAD_CHAR)
20973 {
20974 EMSG2(_(e_illvar), varname);
20975 return FALSE;
20976 }
20977 return TRUE;
20978}
20979
20980/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020981 * Return TRUE if typeval "tv" is set to be locked (immutable).
20982 * Also give an error message, using "name".
20983 */
20984 static int
20985tv_check_lock(lock, name)
20986 int lock;
20987 char_u *name;
20988{
20989 if (lock & VAR_LOCKED)
20990 {
20991 EMSG2(_("E741: Value is locked: %s"),
20992 name == NULL ? (char_u *)_("Unknown") : name);
20993 return TRUE;
20994 }
20995 if (lock & VAR_FIXED)
20996 {
20997 EMSG2(_("E742: Cannot change value of %s"),
20998 name == NULL ? (char_u *)_("Unknown") : name);
20999 return TRUE;
21000 }
21001 return FALSE;
21002}
21003
21004/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021005 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021007 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021008 * It is OK for "from" and "to" to point to the same item. This is used to
21009 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021010 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021011 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021013 typval_T *from;
21014 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021016 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021017 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021018 switch (from->v_type)
21019 {
21020 case VAR_NUMBER:
21021 to->vval.v_number = from->vval.v_number;
21022 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021023#ifdef FEAT_FLOAT
21024 case VAR_FLOAT:
21025 to->vval.v_float = from->vval.v_float;
21026 break;
21027#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021028 case VAR_STRING:
21029 case VAR_FUNC:
21030 if (from->vval.v_string == NULL)
21031 to->vval.v_string = NULL;
21032 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021033 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021035 if (from->v_type == VAR_FUNC)
21036 func_ref(to->vval.v_string);
21037 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021038 break;
21039 case VAR_LIST:
21040 if (from->vval.v_list == NULL)
21041 to->vval.v_list = NULL;
21042 else
21043 {
21044 to->vval.v_list = from->vval.v_list;
21045 ++to->vval.v_list->lv_refcount;
21046 }
21047 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021048 case VAR_DICT:
21049 if (from->vval.v_dict == NULL)
21050 to->vval.v_dict = NULL;
21051 else
21052 {
21053 to->vval.v_dict = from->vval.v_dict;
21054 ++to->vval.v_dict->dv_refcount;
21055 }
21056 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021057 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021058 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021059 break;
21060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061}
21062
21063/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021064 * Make a copy of an item.
21065 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021066 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21067 * reference to an already copied list/dict can be used.
21068 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021069 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021070 static int
21071item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021072 typval_T *from;
21073 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021074 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021075 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076{
21077 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021078 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021079
Bram Moolenaar33570922005-01-25 22:26:29 +000021080 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021081 {
21082 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021083 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021084 }
21085 ++recurse;
21086
21087 switch (from->v_type)
21088 {
21089 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021090#ifdef FEAT_FLOAT
21091 case VAR_FLOAT:
21092#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021093 case VAR_STRING:
21094 case VAR_FUNC:
21095 copy_tv(from, to);
21096 break;
21097 case VAR_LIST:
21098 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021099 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021100 if (from->vval.v_list == NULL)
21101 to->vval.v_list = NULL;
21102 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21103 {
21104 /* use the copy made earlier */
21105 to->vval.v_list = from->vval.v_list->lv_copylist;
21106 ++to->vval.v_list->lv_refcount;
21107 }
21108 else
21109 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21110 if (to->vval.v_list == NULL)
21111 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 break;
21113 case VAR_DICT:
21114 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021115 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021116 if (from->vval.v_dict == NULL)
21117 to->vval.v_dict = NULL;
21118 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21119 {
21120 /* use the copy made earlier */
21121 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21122 ++to->vval.v_dict->dv_refcount;
21123 }
21124 else
21125 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21126 if (to->vval.v_dict == NULL)
21127 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128 break;
21129 default:
21130 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021131 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021132 }
21133 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021134 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021135}
21136
21137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 * ":echo expr1 ..." print each argument separated with a space, add a
21139 * newline at the end.
21140 * ":echon expr1 ..." print each argument plain.
21141 */
21142 void
21143ex_echo(eap)
21144 exarg_T *eap;
21145{
21146 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021147 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021148 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 char_u *p;
21150 int needclr = TRUE;
21151 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021152 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153
21154 if (eap->skip)
21155 ++emsg_skip;
21156 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021158 /* If eval1() causes an error message the text from the command may
21159 * still need to be cleared. E.g., "echo 22,44". */
21160 need_clr_eos = needclr;
21161
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 {
21165 /*
21166 * Report the invalid expression unless the expression evaluation
21167 * has been cancelled due to an aborting error, an interrupt, or an
21168 * exception.
21169 */
21170 if (!aborting())
21171 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021172 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 break;
21174 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021175 need_clr_eos = FALSE;
21176
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 if (!eap->skip)
21178 {
21179 if (atstart)
21180 {
21181 atstart = FALSE;
21182 /* Call msg_start() after eval1(), evaluating the expression
21183 * may cause a message to appear. */
21184 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021185 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021186 /* Mark the saved text as finishing the line, so that what
21187 * follows is displayed on a new line when scrolling back
21188 * at the more prompt. */
21189 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 }
21193 else if (eap->cmdidx == CMD_echo)
21194 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021195 current_copyID += COPYID_INC;
21196 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021197 if (p != NULL)
21198 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021200 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021202 if (*p != TAB && needclr)
21203 {
21204 /* remove any text still there from the command */
21205 msg_clr_eos();
21206 needclr = FALSE;
21207 }
21208 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 }
21210 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021211 {
21212#ifdef FEAT_MBYTE
21213 if (has_mbyte)
21214 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021215 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021216
21217 (void)msg_outtrans_len_attr(p, i, echo_attr);
21218 p += i - 1;
21219 }
21220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021222 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021225 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 arg = skipwhite(arg);
21229 }
21230 eap->nextcmd = check_nextcmd(arg);
21231
21232 if (eap->skip)
21233 --emsg_skip;
21234 else
21235 {
21236 /* remove text that may still be there from the command */
21237 if (needclr)
21238 msg_clr_eos();
21239 if (eap->cmdidx == CMD_echo)
21240 msg_end();
21241 }
21242}
21243
21244/*
21245 * ":echohl {name}".
21246 */
21247 void
21248ex_echohl(eap)
21249 exarg_T *eap;
21250{
21251 int id;
21252
21253 id = syn_name2id(eap->arg);
21254 if (id == 0)
21255 echo_attr = 0;
21256 else
21257 echo_attr = syn_id2attr(id);
21258}
21259
21260/*
21261 * ":execute expr1 ..." execute the result of an expression.
21262 * ":echomsg expr1 ..." Print a message
21263 * ":echoerr expr1 ..." Print an error
21264 * Each gets spaces around each argument and a newline at the end for
21265 * echo commands
21266 */
21267 void
21268ex_execute(eap)
21269 exarg_T *eap;
21270{
21271 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 int ret = OK;
21274 char_u *p;
21275 garray_T ga;
21276 int len;
21277 int save_did_emsg;
21278
21279 ga_init2(&ga, 1, 80);
21280
21281 if (eap->skip)
21282 ++emsg_skip;
21283 while (*arg != NUL && *arg != '|' && *arg != '\n')
21284 {
21285 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021286 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 {
21288 /*
21289 * Report the invalid expression unless the expression evaluation
21290 * has been cancelled due to an aborting error, an interrupt, or an
21291 * exception.
21292 */
21293 if (!aborting())
21294 EMSG2(_(e_invexpr2), p);
21295 ret = FAIL;
21296 break;
21297 }
21298
21299 if (!eap->skip)
21300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021301 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 len = (int)STRLEN(p);
21303 if (ga_grow(&ga, len + 2) == FAIL)
21304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021305 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 ret = FAIL;
21307 break;
21308 }
21309 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 ga.ga_len += len;
21313 }
21314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021315 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 arg = skipwhite(arg);
21317 }
21318
21319 if (ret != FAIL && ga.ga_data != NULL)
21320 {
21321 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021324 out_flush();
21325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 else if (eap->cmdidx == CMD_echoerr)
21327 {
21328 /* We don't want to abort following commands, restore did_emsg. */
21329 save_did_emsg = did_emsg;
21330 EMSG((char_u *)ga.ga_data);
21331 if (!force_abort)
21332 did_emsg = save_did_emsg;
21333 }
21334 else if (eap->cmdidx == CMD_execute)
21335 do_cmdline((char_u *)ga.ga_data,
21336 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21337 }
21338
21339 ga_clear(&ga);
21340
21341 if (eap->skip)
21342 --emsg_skip;
21343
21344 eap->nextcmd = check_nextcmd(arg);
21345}
21346
21347/*
21348 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21349 * "arg" points to the "&" or '+' when called, to "option" when returning.
21350 * Returns NULL when no option name found. Otherwise pointer to the char
21351 * after the option name.
21352 */
21353 static char_u *
21354find_option_end(arg, opt_flags)
21355 char_u **arg;
21356 int *opt_flags;
21357{
21358 char_u *p = *arg;
21359
21360 ++p;
21361 if (*p == 'g' && p[1] == ':')
21362 {
21363 *opt_flags = OPT_GLOBAL;
21364 p += 2;
21365 }
21366 else if (*p == 'l' && p[1] == ':')
21367 {
21368 *opt_flags = OPT_LOCAL;
21369 p += 2;
21370 }
21371 else
21372 *opt_flags = 0;
21373
21374 if (!ASCII_ISALPHA(*p))
21375 return NULL;
21376 *arg = p;
21377
21378 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21379 p += 4; /* termcap option */
21380 else
21381 while (ASCII_ISALPHA(*p))
21382 ++p;
21383 return p;
21384}
21385
21386/*
21387 * ":function"
21388 */
21389 void
21390ex_function(eap)
21391 exarg_T *eap;
21392{
21393 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021394 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 int j;
21396 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021398 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 char_u *name = NULL;
21400 char_u *p;
21401 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021402 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 garray_T newargs;
21404 garray_T newlines;
21405 int varargs = FALSE;
21406 int mustend = FALSE;
21407 int flags = 0;
21408 ufunc_T *fp;
21409 int indent;
21410 int nesting;
21411 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021412 dictitem_T *v;
21413 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021414 static int func_nr = 0; /* number for nameless function */
21415 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021416 hashtab_T *ht;
21417 int todo;
21418 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021419 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420
21421 /*
21422 * ":function" without argument: list functions.
21423 */
21424 if (ends_excmd(*eap->arg))
21425 {
21426 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021427 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021428 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021429 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021430 {
21431 if (!HASHITEM_EMPTY(hi))
21432 {
21433 --todo;
21434 fp = HI2UF(hi);
21435 if (!isdigit(*fp->uf_name))
21436 list_func_head(fp, FALSE);
21437 }
21438 }
21439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 eap->nextcmd = check_nextcmd(eap->arg);
21441 return;
21442 }
21443
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021444 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021445 * ":function /pat": list functions matching pattern.
21446 */
21447 if (*eap->arg == '/')
21448 {
21449 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21450 if (!eap->skip)
21451 {
21452 regmatch_T regmatch;
21453
21454 c = *p;
21455 *p = NUL;
21456 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21457 *p = c;
21458 if (regmatch.regprog != NULL)
21459 {
21460 regmatch.rm_ic = p_ic;
21461
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021462 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021463 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21464 {
21465 if (!HASHITEM_EMPTY(hi))
21466 {
21467 --todo;
21468 fp = HI2UF(hi);
21469 if (!isdigit(*fp->uf_name)
21470 && vim_regexec(&regmatch, fp->uf_name, 0))
21471 list_func_head(fp, FALSE);
21472 }
21473 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021474 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021475 }
21476 }
21477 if (*p == '/')
21478 ++p;
21479 eap->nextcmd = check_nextcmd(p);
21480 return;
21481 }
21482
21483 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484 * Get the function name. There are these situations:
21485 * func normal function name
21486 * "name" == func, "fudi.fd_dict" == NULL
21487 * dict.func new dictionary entry
21488 * "name" == NULL, "fudi.fd_dict" set,
21489 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21490 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021491 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021492 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21493 * dict.func existing dict entry that's not a Funcref
21494 * "name" == NULL, "fudi.fd_dict" set,
21495 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 name = trans_function_name(&p, eap->skip, 0, &fudi);
21499 paren = (vim_strchr(p, '(') != NULL);
21500 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 {
21502 /*
21503 * Return on an invalid expression in braces, unless the expression
21504 * evaluation has been cancelled due to an aborting error, an
21505 * interrupt, or an exception.
21506 */
21507 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021509 if (!eap->skip && fudi.fd_newkey != NULL)
21510 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 else
21515 eap->skip = TRUE;
21516 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021517
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 /* An error in a function call during evaluation of an expression in magic
21519 * braces should not cause the function not to be defined. */
21520 saved_did_emsg = did_emsg;
21521 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522
21523 /*
21524 * ":function func" with only function name: list function.
21525 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021526 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 {
21528 if (!ends_excmd(*skipwhite(p)))
21529 {
21530 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021531 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 }
21533 eap->nextcmd = check_nextcmd(p);
21534 if (eap->nextcmd != NULL)
21535 *p = NUL;
21536 if (!eap->skip && !got_int)
21537 {
21538 fp = find_func(name);
21539 if (fp != NULL)
21540 {
21541 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021542 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021543 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021544 if (FUNCLINE(fp, j) == NULL)
21545 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 msg_putchar('\n');
21547 msg_outnum((long)(j + 1));
21548 if (j < 9)
21549 msg_putchar(' ');
21550 if (j < 99)
21551 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021552 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 out_flush(); /* show a line at a time */
21554 ui_breakcheck();
21555 }
21556 if (!got_int)
21557 {
21558 msg_putchar('\n');
21559 msg_puts((char_u *)" endfunction");
21560 }
21561 }
21562 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021563 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021565 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 }
21567
21568 /*
21569 * ":function name(arg1, arg2)" Define function.
21570 */
21571 p = skipwhite(p);
21572 if (*p != '(')
21573 {
21574 if (!eap->skip)
21575 {
21576 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021577 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021578 }
21579 /* attempt to continue by skipping some text */
21580 if (vim_strchr(p, '(') != NULL)
21581 p = vim_strchr(p, '(');
21582 }
21583 p = skipwhite(p + 1);
21584
21585 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21586 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21587
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021588 if (!eap->skip)
21589 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021590 /* Check the name of the function. Unless it's a dictionary function
21591 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021592 if (name != NULL)
21593 arg = name;
21594 else
21595 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021596 if (arg != NULL && (fudi.fd_di == NULL
21597 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021598 {
21599 if (*arg == K_SPECIAL)
21600 j = 3;
21601 else
21602 j = 0;
21603 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21604 : eval_isnamec(arg[j])))
21605 ++j;
21606 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021607 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021608 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021609 /* Disallow using the g: dict. */
21610 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21611 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021612 }
21613
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 /*
21615 * Isolate the arguments: "arg1, arg2, ...)"
21616 */
21617 while (*p != ')')
21618 {
21619 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21620 {
21621 varargs = TRUE;
21622 p += 3;
21623 mustend = TRUE;
21624 }
21625 else
21626 {
21627 arg = p;
21628 while (ASCII_ISALNUM(*p) || *p == '_')
21629 ++p;
21630 if (arg == p || isdigit(*arg)
21631 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21632 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21633 {
21634 if (!eap->skip)
21635 EMSG2(_("E125: Illegal argument: %s"), arg);
21636 break;
21637 }
21638 if (ga_grow(&newargs, 1) == FAIL)
21639 goto erret;
21640 c = *p;
21641 *p = NUL;
21642 arg = vim_strsave(arg);
21643 if (arg == NULL)
21644 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021645
21646 /* Check for duplicate argument name. */
21647 for (i = 0; i < newargs.ga_len; ++i)
21648 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21649 {
21650 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021651 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021652 goto erret;
21653 }
21654
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21656 *p = c;
21657 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 if (*p == ',')
21659 ++p;
21660 else
21661 mustend = TRUE;
21662 }
21663 p = skipwhite(p);
21664 if (mustend && *p != ')')
21665 {
21666 if (!eap->skip)
21667 EMSG2(_(e_invarg2), eap->arg);
21668 break;
21669 }
21670 }
21671 ++p; /* skip the ')' */
21672
Bram Moolenaare9a41262005-01-15 22:18:47 +000021673 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 for (;;)
21675 {
21676 p = skipwhite(p);
21677 if (STRNCMP(p, "range", 5) == 0)
21678 {
21679 flags |= FC_RANGE;
21680 p += 5;
21681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021682 else if (STRNCMP(p, "dict", 4) == 0)
21683 {
21684 flags |= FC_DICT;
21685 p += 4;
21686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 else if (STRNCMP(p, "abort", 5) == 0)
21688 {
21689 flags |= FC_ABORT;
21690 p += 5;
21691 }
21692 else
21693 break;
21694 }
21695
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021696 /* When there is a line break use what follows for the function body.
21697 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21698 if (*p == '\n')
21699 line_arg = p + 1;
21700 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 EMSG(_(e_trailing));
21702
21703 /*
21704 * Read the body of the function, until ":endfunction" is found.
21705 */
21706 if (KeyTyped)
21707 {
21708 /* Check if the function already exists, don't let the user type the
21709 * whole function before telling him it doesn't work! For a script we
21710 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 if (!eap->skip && !eap->forceit)
21712 {
21713 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21714 EMSG(_(e_funcdict));
21715 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021716 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021719 if (!eap->skip && did_emsg)
21720 goto erret;
21721
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 msg_putchar('\n'); /* don't overwrite the function name */
21723 cmdline_row = msg_row;
21724 }
21725
21726 indent = 2;
21727 nesting = 0;
21728 for (;;)
21729 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021730 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021731 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021732 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021733 saved_wait_return = FALSE;
21734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021736 sourcing_lnum_off = sourcing_lnum;
21737
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021738 if (line_arg != NULL)
21739 {
21740 /* Use eap->arg, split up in parts by line breaks. */
21741 theline = line_arg;
21742 p = vim_strchr(theline, '\n');
21743 if (p == NULL)
21744 line_arg += STRLEN(line_arg);
21745 else
21746 {
21747 *p = NUL;
21748 line_arg = p + 1;
21749 }
21750 }
21751 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 theline = getcmdline(':', 0L, indent);
21753 else
21754 theline = eap->getline(':', eap->cookie, indent);
21755 if (KeyTyped)
21756 lines_left = Rows - 1;
21757 if (theline == NULL)
21758 {
21759 EMSG(_("E126: Missing :endfunction"));
21760 goto erret;
21761 }
21762
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021763 /* Detect line continuation: sourcing_lnum increased more than one. */
21764 if (sourcing_lnum > sourcing_lnum_off + 1)
21765 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21766 else
21767 sourcing_lnum_off = 0;
21768
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 if (skip_until != NULL)
21770 {
21771 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21772 * don't check for ":endfunc". */
21773 if (STRCMP(theline, skip_until) == 0)
21774 {
21775 vim_free(skip_until);
21776 skip_until = NULL;
21777 }
21778 }
21779 else
21780 {
21781 /* skip ':' and blanks*/
21782 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21783 ;
21784
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021785 /* Check for "endfunction". */
21786 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021788 if (line_arg == NULL)
21789 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 break;
21791 }
21792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021793 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 * at "end". */
21795 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21796 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021797 else if (STRNCMP(p, "if", 2) == 0
21798 || STRNCMP(p, "wh", 2) == 0
21799 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 || STRNCMP(p, "try", 3) == 0)
21801 indent += 2;
21802
21803 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021804 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021806 if (*p == '!')
21807 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 p += eval_fname_script(p);
21809 if (ASCII_ISALPHA(*p))
21810 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021811 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 if (*skipwhite(p) == '(')
21813 {
21814 ++nesting;
21815 indent += 2;
21816 }
21817 }
21818 }
21819
21820 /* Check for ":append" or ":insert". */
21821 p = skip_range(p, NULL);
21822 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21823 || (p[0] == 'i'
21824 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21825 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21826 skip_until = vim_strsave((char_u *)".");
21827
21828 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21829 arg = skipwhite(skiptowhite(p));
21830 if (arg[0] == '<' && arg[1] =='<'
21831 && ((p[0] == 'p' && p[1] == 'y'
21832 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21833 || (p[0] == 'p' && p[1] == 'e'
21834 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21835 || (p[0] == 't' && p[1] == 'c'
21836 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021837 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21838 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21840 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021841 || (p[0] == 'm' && p[1] == 'z'
21842 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843 ))
21844 {
21845 /* ":python <<" continues until a dot, like ":append" */
21846 p = skipwhite(arg + 2);
21847 if (*p == NUL)
21848 skip_until = vim_strsave((char_u *)".");
21849 else
21850 skip_until = vim_strsave(p);
21851 }
21852 }
21853
21854 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021855 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021856 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021857 if (line_arg == NULL)
21858 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021860 }
21861
21862 /* Copy the line to newly allocated memory. get_one_sourceline()
21863 * allocates 250 bytes per line, this saves 80% on average. The cost
21864 * is an extra alloc/free. */
21865 p = vim_strsave(theline);
21866 if (p != NULL)
21867 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021868 if (line_arg == NULL)
21869 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021870 theline = p;
21871 }
21872
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021873 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21874
21875 /* Add NULL lines for continuation lines, so that the line count is
21876 * equal to the index in the growarray. */
21877 while (sourcing_lnum_off-- > 0)
21878 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021879
21880 /* Check for end of eap->arg. */
21881 if (line_arg != NULL && *line_arg == NUL)
21882 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 }
21884
21885 /* Don't define the function when skipping commands or when an error was
21886 * detected. */
21887 if (eap->skip || did_emsg)
21888 goto erret;
21889
21890 /*
21891 * If there are no errors, add the function
21892 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021893 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021894 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021895 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021897 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021898 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021899 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021900 goto erret;
21901 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021902
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021903 fp = find_func(name);
21904 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021906 if (!eap->forceit)
21907 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021908 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021909 goto erret;
21910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021911 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021912 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021913 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021914 name);
21915 goto erret;
21916 }
21917 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021918 ga_clear_strings(&(fp->uf_args));
21919 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021920 vim_free(name);
21921 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 }
21924 else
21925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021926 char numbuf[20];
21927
21928 fp = NULL;
21929 if (fudi.fd_newkey == NULL && !eap->forceit)
21930 {
21931 EMSG(_(e_funcdict));
21932 goto erret;
21933 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021934 if (fudi.fd_di == NULL)
21935 {
21936 /* Can't add a function to a locked dictionary */
21937 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21938 goto erret;
21939 }
21940 /* Can't change an existing function if it is locked */
21941 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21942 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021943
21944 /* Give the function a sequential number. Can only be used with a
21945 * Funcref! */
21946 vim_free(name);
21947 sprintf(numbuf, "%d", ++func_nr);
21948 name = vim_strsave((char_u *)numbuf);
21949 if (name == NULL)
21950 goto erret;
21951 }
21952
21953 if (fp == NULL)
21954 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021955 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021956 {
21957 int slen, plen;
21958 char_u *scriptname;
21959
21960 /* Check that the autoload name matches the script name. */
21961 j = FAIL;
21962 if (sourcing_name != NULL)
21963 {
21964 scriptname = autoload_name(name);
21965 if (scriptname != NULL)
21966 {
21967 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021968 plen = (int)STRLEN(p);
21969 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021970 if (slen > plen && fnamecmp(p,
21971 sourcing_name + slen - plen) == 0)
21972 j = OK;
21973 vim_free(scriptname);
21974 }
21975 }
21976 if (j == FAIL)
21977 {
21978 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21979 goto erret;
21980 }
21981 }
21982
21983 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 if (fp == NULL)
21985 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021986
21987 if (fudi.fd_dict != NULL)
21988 {
21989 if (fudi.fd_di == NULL)
21990 {
21991 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021992 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021993 if (fudi.fd_di == NULL)
21994 {
21995 vim_free(fp);
21996 goto erret;
21997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021998 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21999 {
22000 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022001 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022002 goto erret;
22003 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022004 }
22005 else
22006 /* overwrite existing dict entry */
22007 clear_tv(&fudi.fd_di->di_tv);
22008 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022009 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022010 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022011 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022012
22013 /* behave like "dict" was used */
22014 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022015 }
22016
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022018 STRCPY(fp->uf_name, name);
22019 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022021 fp->uf_args = newargs;
22022 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022023#ifdef FEAT_PROFILE
22024 fp->uf_tml_count = NULL;
22025 fp->uf_tml_total = NULL;
22026 fp->uf_tml_self = NULL;
22027 fp->uf_profiling = FALSE;
22028 if (prof_def_func())
22029 func_do_profile(fp);
22030#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022031 fp->uf_varargs = varargs;
22032 fp->uf_flags = flags;
22033 fp->uf_calls = 0;
22034 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022035 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036
22037erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 ga_clear_strings(&newargs);
22039 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022040ret_free:
22041 vim_free(skip_until);
22042 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022045 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046}
22047
22048/*
22049 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022050 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022052 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022053 * TFN_INT: internal function name OK
22054 * TFN_QUIET: be quiet
22055 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 * Advances "pp" to just after the function name (if no error).
22057 */
22058 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022059trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 char_u **pp;
22061 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022062 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022065 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 char_u *start;
22067 char_u *end;
22068 int lead;
22069 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022071 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022072
22073 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022074 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022076
22077 /* Check for hard coded <SNR>: already translated function ID (from a user
22078 * command). */
22079 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22080 && (*pp)[2] == (int)KE_SNR)
22081 {
22082 *pp += 3;
22083 len = get_id_len(pp) + 3;
22084 return vim_strnsave(start, len);
22085 }
22086
22087 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22088 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022090 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022092
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022093 /* Note that TFN_ flags use the same values as GLV_ flags. */
22094 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022095 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022096 if (end == start)
22097 {
22098 if (!skip)
22099 EMSG(_("E129: Function name required"));
22100 goto theend;
22101 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022102 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022103 {
22104 /*
22105 * Report an invalid expression in braces, unless the expression
22106 * evaluation has been cancelled due to an aborting error, an
22107 * interrupt, or an exception.
22108 */
22109 if (!aborting())
22110 {
22111 if (end != NULL)
22112 EMSG2(_(e_invarg2), start);
22113 }
22114 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022115 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022116 goto theend;
22117 }
22118
22119 if (lv.ll_tv != NULL)
22120 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022121 if (fdp != NULL)
22122 {
22123 fdp->fd_dict = lv.ll_dict;
22124 fdp->fd_newkey = lv.ll_newkey;
22125 lv.ll_newkey = NULL;
22126 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022127 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022128 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22129 {
22130 name = vim_strsave(lv.ll_tv->vval.v_string);
22131 *pp = end;
22132 }
22133 else
22134 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022135 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22136 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022137 EMSG(_(e_funcref));
22138 else
22139 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022140 name = NULL;
22141 }
22142 goto theend;
22143 }
22144
22145 if (lv.ll_name == NULL)
22146 {
22147 /* Error found, but continue after the function name. */
22148 *pp = end;
22149 goto theend;
22150 }
22151
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022152 /* Check if the name is a Funcref. If so, use the value. */
22153 if (lv.ll_exp_name != NULL)
22154 {
22155 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022156 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022157 if (name == lv.ll_exp_name)
22158 name = NULL;
22159 }
22160 else
22161 {
22162 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022163 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022164 if (name == *pp)
22165 name = NULL;
22166 }
22167 if (name != NULL)
22168 {
22169 name = vim_strsave(name);
22170 *pp = end;
22171 goto theend;
22172 }
22173
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022174 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022175 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022176 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022177 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22178 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22179 {
22180 /* When there was "s:" already or the name expanded to get a
22181 * leading "s:" then remove it. */
22182 lv.ll_name += 2;
22183 len -= 2;
22184 lead = 2;
22185 }
22186 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022187 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022188 {
22189 if (lead == 2) /* skip over "s:" */
22190 lv.ll_name += 2;
22191 len = (int)(end - lv.ll_name);
22192 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022193
22194 /*
22195 * Copy the function name to allocated memory.
22196 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22197 * Accept <SNR>123_name() outside a script.
22198 */
22199 if (skip)
22200 lead = 0; /* do nothing */
22201 else if (lead > 0)
22202 {
22203 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022204 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22205 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022206 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022207 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022208 if (current_SID <= 0)
22209 {
22210 EMSG(_(e_usingsid));
22211 goto theend;
22212 }
22213 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22214 lead += (int)STRLEN(sid_buf);
22215 }
22216 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022217 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022218 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022219 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022220 goto theend;
22221 }
22222 name = alloc((unsigned)(len + lead + 1));
22223 if (name != NULL)
22224 {
22225 if (lead > 0)
22226 {
22227 name[0] = K_SPECIAL;
22228 name[1] = KS_EXTRA;
22229 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022230 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022231 STRCPY(name + 3, sid_buf);
22232 }
22233 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22234 name[len + lead] = NUL;
22235 }
22236 *pp = end;
22237
22238theend:
22239 clear_lval(&lv);
22240 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241}
22242
22243/*
22244 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22245 * Return 2 if "p" starts with "s:".
22246 * Return 0 otherwise.
22247 */
22248 static int
22249eval_fname_script(p)
22250 char_u *p;
22251{
22252 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22253 || STRNICMP(p + 1, "SNR>", 4) == 0))
22254 return 5;
22255 if (p[0] == 's' && p[1] == ':')
22256 return 2;
22257 return 0;
22258}
22259
22260/*
22261 * Return TRUE if "p" starts with "<SID>" or "s:".
22262 * Only works if eval_fname_script() returned non-zero for "p"!
22263 */
22264 static int
22265eval_fname_sid(p)
22266 char_u *p;
22267{
22268 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22269}
22270
22271/*
22272 * List the head of the function: "name(arg1, arg2)".
22273 */
22274 static void
22275list_func_head(fp, indent)
22276 ufunc_T *fp;
22277 int indent;
22278{
22279 int j;
22280
22281 msg_start();
22282 if (indent)
22283 MSG_PUTS(" ");
22284 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022285 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 {
22287 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022288 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022291 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 {
22295 if (j)
22296 MSG_PUTS(", ");
22297 msg_puts(FUNCARG(fp, j));
22298 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022299 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 {
22301 if (j)
22302 MSG_PUTS(", ");
22303 MSG_PUTS("...");
22304 }
22305 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022306 if (fp->uf_flags & FC_ABORT)
22307 MSG_PUTS(" abort");
22308 if (fp->uf_flags & FC_RANGE)
22309 MSG_PUTS(" range");
22310 if (fp->uf_flags & FC_DICT)
22311 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022312 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022313 if (p_verbose > 0)
22314 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315}
22316
22317/*
22318 * Find a function by name, return pointer to it in ufuncs.
22319 * Return NULL for unknown function.
22320 */
22321 static ufunc_T *
22322find_func(name)
22323 char_u *name;
22324{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022325 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022327 hi = hash_find(&func_hashtab, name);
22328 if (!HASHITEM_EMPTY(hi))
22329 return HI2UF(hi);
22330 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331}
22332
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022333#if defined(EXITFREE) || defined(PROTO)
22334 void
22335free_all_functions()
22336{
22337 hashitem_T *hi;
22338
22339 /* Need to start all over every time, because func_free() may change the
22340 * hash table. */
22341 while (func_hashtab.ht_used > 0)
22342 for (hi = func_hashtab.ht_array; ; ++hi)
22343 if (!HASHITEM_EMPTY(hi))
22344 {
22345 func_free(HI2UF(hi));
22346 break;
22347 }
22348}
22349#endif
22350
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022351 int
22352translated_function_exists(name)
22353 char_u *name;
22354{
22355 if (builtin_function(name))
22356 return find_internal_func(name) >= 0;
22357 return find_func(name) != NULL;
22358}
22359
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022360/*
22361 * Return TRUE if a function "name" exists.
22362 */
22363 static int
22364function_exists(name)
22365 char_u *name;
22366{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022367 char_u *nm = name;
22368 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 int n = FALSE;
22370
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022371 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22372 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022373 nm = skipwhite(nm);
22374
22375 /* Only accept "funcname", "funcname ", "funcname (..." and
22376 * "funcname(...", not "funcname!...". */
22377 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022378 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022379 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022380 return n;
22381}
22382
Bram Moolenaara1544c02013-05-30 12:35:52 +020022383 char_u *
22384get_expanded_name(name, check)
22385 char_u *name;
22386 int check;
22387{
22388 char_u *nm = name;
22389 char_u *p;
22390
22391 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22392
22393 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022394 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022395 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022396
Bram Moolenaara1544c02013-05-30 12:35:52 +020022397 vim_free(p);
22398 return NULL;
22399}
22400
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022401/*
22402 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022403 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022404 */
22405 static int
22406builtin_function(name)
22407 char_u *name;
22408{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022409 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22410 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022411}
22412
Bram Moolenaar05159a02005-02-26 23:04:13 +000022413#if defined(FEAT_PROFILE) || defined(PROTO)
22414/*
22415 * Start profiling function "fp".
22416 */
22417 static void
22418func_do_profile(fp)
22419 ufunc_T *fp;
22420{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022421 int len = fp->uf_lines.ga_len;
22422
22423 if (len == 0)
22424 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022425 fp->uf_tm_count = 0;
22426 profile_zero(&fp->uf_tm_self);
22427 profile_zero(&fp->uf_tm_total);
22428 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022429 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022430 if (fp->uf_tml_total == NULL)
22431 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022432 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022433 if (fp->uf_tml_self == NULL)
22434 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022435 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022436 fp->uf_tml_idx = -1;
22437 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22438 || fp->uf_tml_self == NULL)
22439 return; /* out of memory */
22440
22441 fp->uf_profiling = TRUE;
22442}
22443
22444/*
22445 * Dump the profiling results for all functions in file "fd".
22446 */
22447 void
22448func_dump_profile(fd)
22449 FILE *fd;
22450{
22451 hashitem_T *hi;
22452 int todo;
22453 ufunc_T *fp;
22454 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022455 ufunc_T **sorttab;
22456 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022458 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022459 if (todo == 0)
22460 return; /* nothing to dump */
22461
Bram Moolenaar73830342005-02-28 22:48:19 +000022462 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22463
Bram Moolenaar05159a02005-02-26 23:04:13 +000022464 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22465 {
22466 if (!HASHITEM_EMPTY(hi))
22467 {
22468 --todo;
22469 fp = HI2UF(hi);
22470 if (fp->uf_profiling)
22471 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022472 if (sorttab != NULL)
22473 sorttab[st_len++] = fp;
22474
Bram Moolenaar05159a02005-02-26 23:04:13 +000022475 if (fp->uf_name[0] == K_SPECIAL)
22476 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22477 else
22478 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22479 if (fp->uf_tm_count == 1)
22480 fprintf(fd, "Called 1 time\n");
22481 else
22482 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22483 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22484 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22485 fprintf(fd, "\n");
22486 fprintf(fd, "count total (s) self (s)\n");
22487
22488 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22489 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022490 if (FUNCLINE(fp, i) == NULL)
22491 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022492 prof_func_line(fd, fp->uf_tml_count[i],
22493 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022494 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22495 }
22496 fprintf(fd, "\n");
22497 }
22498 }
22499 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022500
22501 if (sorttab != NULL && st_len > 0)
22502 {
22503 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22504 prof_total_cmp);
22505 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22506 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22507 prof_self_cmp);
22508 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22509 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022510
22511 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022512}
Bram Moolenaar73830342005-02-28 22:48:19 +000022513
22514 static void
22515prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22516 FILE *fd;
22517 ufunc_T **sorttab;
22518 int st_len;
22519 char *title;
22520 int prefer_self; /* when equal print only self time */
22521{
22522 int i;
22523 ufunc_T *fp;
22524
22525 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22526 fprintf(fd, "count total (s) self (s) function\n");
22527 for (i = 0; i < 20 && i < st_len; ++i)
22528 {
22529 fp = sorttab[i];
22530 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22531 prefer_self);
22532 if (fp->uf_name[0] == K_SPECIAL)
22533 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22534 else
22535 fprintf(fd, " %s()\n", fp->uf_name);
22536 }
22537 fprintf(fd, "\n");
22538}
22539
22540/*
22541 * Print the count and times for one function or function line.
22542 */
22543 static void
22544prof_func_line(fd, count, total, self, prefer_self)
22545 FILE *fd;
22546 int count;
22547 proftime_T *total;
22548 proftime_T *self;
22549 int prefer_self; /* when equal print only self time */
22550{
22551 if (count > 0)
22552 {
22553 fprintf(fd, "%5d ", count);
22554 if (prefer_self && profile_equal(total, self))
22555 fprintf(fd, " ");
22556 else
22557 fprintf(fd, "%s ", profile_msg(total));
22558 if (!prefer_self && profile_equal(total, self))
22559 fprintf(fd, " ");
22560 else
22561 fprintf(fd, "%s ", profile_msg(self));
22562 }
22563 else
22564 fprintf(fd, " ");
22565}
22566
22567/*
22568 * Compare function for total time sorting.
22569 */
22570 static int
22571#ifdef __BORLANDC__
22572_RTLENTRYF
22573#endif
22574prof_total_cmp(s1, s2)
22575 const void *s1;
22576 const void *s2;
22577{
22578 ufunc_T *p1, *p2;
22579
22580 p1 = *(ufunc_T **)s1;
22581 p2 = *(ufunc_T **)s2;
22582 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22583}
22584
22585/*
22586 * Compare function for self time sorting.
22587 */
22588 static int
22589#ifdef __BORLANDC__
22590_RTLENTRYF
22591#endif
22592prof_self_cmp(s1, s2)
22593 const void *s1;
22594 const void *s2;
22595{
22596 ufunc_T *p1, *p2;
22597
22598 p1 = *(ufunc_T **)s1;
22599 p2 = *(ufunc_T **)s2;
22600 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22601}
22602
Bram Moolenaar05159a02005-02-26 23:04:13 +000022603#endif
22604
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022605/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022606 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022607 * Return TRUE if a package was loaded.
22608 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022609 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022610script_autoload(name, reload)
22611 char_u *name;
22612 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022613{
22614 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022615 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022616 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022617 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022618
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022619 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022620 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022621 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022622 return FALSE;
22623
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022624 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022625
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022626 /* Find the name in the list of previously loaded package names. Skip
22627 * "autoload/", it's always the same. */
22628 for (i = 0; i < ga_loaded.ga_len; ++i)
22629 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22630 break;
22631 if (!reload && i < ga_loaded.ga_len)
22632 ret = FALSE; /* was loaded already */
22633 else
22634 {
22635 /* Remember the name if it wasn't loaded already. */
22636 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22637 {
22638 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22639 tofree = NULL;
22640 }
22641
22642 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022643 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022644 ret = TRUE;
22645 }
22646
22647 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022648 return ret;
22649}
22650
22651/*
22652 * Return the autoload script name for a function or variable name.
22653 * Returns NULL when out of memory.
22654 */
22655 static char_u *
22656autoload_name(name)
22657 char_u *name;
22658{
22659 char_u *p;
22660 char_u *scriptname;
22661
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022662 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022663 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22664 if (scriptname == NULL)
22665 return FALSE;
22666 STRCPY(scriptname, "autoload/");
22667 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022668 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022669 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022670 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022671 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022672 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022673}
22674
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22676
22677/*
22678 * Function given to ExpandGeneric() to obtain the list of user defined
22679 * function names.
22680 */
22681 char_u *
22682get_user_func_name(xp, idx)
22683 expand_T *xp;
22684 int idx;
22685{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022686 static long_u done;
22687 static hashitem_T *hi;
22688 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689
22690 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022692 done = 0;
22693 hi = func_hashtab.ht_array;
22694 }
22695 if (done < func_hashtab.ht_used)
22696 {
22697 if (done++ > 0)
22698 ++hi;
22699 while (HASHITEM_EMPTY(hi))
22700 ++hi;
22701 fp = HI2UF(hi);
22702
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022703 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022704 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022705
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022706 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22707 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708
22709 cat_func_name(IObuff, fp);
22710 if (xp->xp_context != EXPAND_USER_FUNC)
22711 {
22712 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022713 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 STRCAT(IObuff, ")");
22715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 return IObuff;
22717 }
22718 return NULL;
22719}
22720
22721#endif /* FEAT_CMDL_COMPL */
22722
22723/*
22724 * Copy the function name of "fp" to buffer "buf".
22725 * "buf" must be able to hold the function name plus three bytes.
22726 * Takes care of script-local function names.
22727 */
22728 static void
22729cat_func_name(buf, fp)
22730 char_u *buf;
22731 ufunc_T *fp;
22732{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022733 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 {
22735 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022736 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 }
22738 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022739 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
22743 * ":delfunction {name}"
22744 */
22745 void
22746ex_delfunction(eap)
22747 exarg_T *eap;
22748{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022749 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 char_u *p;
22751 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753
22754 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022755 name = trans_function_name(&p, eap->skip, 0, &fudi);
22756 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022758 {
22759 if (fudi.fd_dict != NULL && !eap->skip)
22760 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 if (!ends_excmd(*skipwhite(p)))
22764 {
22765 vim_free(name);
22766 EMSG(_(e_trailing));
22767 return;
22768 }
22769 eap->nextcmd = check_nextcmd(p);
22770 if (eap->nextcmd != NULL)
22771 *p = NUL;
22772
22773 if (!eap->skip)
22774 fp = find_func(name);
22775 vim_free(name);
22776
22777 if (!eap->skip)
22778 {
22779 if (fp == NULL)
22780 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022781 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 return;
22783 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022784 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 {
22786 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22787 return;
22788 }
22789
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022790 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 /* Delete the dict item that refers to the function, it will
22793 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022794 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022796 else
22797 func_free(fp);
22798 }
22799}
22800
22801/*
22802 * Free a function and remove it from the list of functions.
22803 */
22804 static void
22805func_free(fp)
22806 ufunc_T *fp;
22807{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022808 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022809
22810 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 ga_clear_strings(&(fp->uf_args));
22812 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022813#ifdef FEAT_PROFILE
22814 vim_free(fp->uf_tml_count);
22815 vim_free(fp->uf_tml_total);
22816 vim_free(fp->uf_tml_self);
22817#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022818
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022819 /* remove the function from the function hashtable */
22820 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22821 if (HASHITEM_EMPTY(hi))
22822 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022823 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022824 hash_remove(&func_hashtab, hi);
22825
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022826 vim_free(fp);
22827}
22828
22829/*
22830 * Unreference a Function: decrement the reference count and free it when it
22831 * becomes zero. Only for numbered functions.
22832 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022833 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022834func_unref(name)
22835 char_u *name;
22836{
22837 ufunc_T *fp;
22838
22839 if (name != NULL && isdigit(*name))
22840 {
22841 fp = find_func(name);
22842 if (fp == NULL)
22843 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022844 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022845 {
22846 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022847 * when "uf_calls" becomes zero. */
22848 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022849 func_free(fp);
22850 }
22851 }
22852}
22853
22854/*
22855 * Count a reference to a Function.
22856 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022857 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022858func_ref(name)
22859 char_u *name;
22860{
22861 ufunc_T *fp;
22862
22863 if (name != NULL && isdigit(*name))
22864 {
22865 fp = find_func(name);
22866 if (fp == NULL)
22867 EMSG2(_(e_intern2), "func_ref()");
22868 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022869 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 }
22871}
22872
22873/*
22874 * Call a user function.
22875 */
22876 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022877call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 ufunc_T *fp; /* pointer to function */
22879 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 typval_T *argvars; /* arguments */
22881 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 linenr_T firstline; /* first line of range */
22883 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885{
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 char_u *save_sourcing_name;
22887 linenr_T save_sourcing_lnum;
22888 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022889 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 int save_did_emsg;
22891 static int depth = 0;
22892 dictitem_T *v;
22893 int fixvar_idx = 0; /* index in fixvar[] */
22894 int i;
22895 int ai;
22896 char_u numbuf[NUMBUFLEN];
22897 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022898#ifdef FEAT_PROFILE
22899 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022900 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902
22903 /* If depth of calling is getting too high, don't execute the function */
22904 if (depth >= p_mfd)
22905 {
22906 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022907 rettv->v_type = VAR_NUMBER;
22908 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 return;
22910 }
22911 ++depth;
22912
22913 line_breakcheck(); /* check for CTRL-C hit */
22914
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022915 fc = (funccall_T *)alloc(sizeof(funccall_T));
22916 fc->caller = current_funccal;
22917 current_funccal = fc;
22918 fc->func = fp;
22919 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022920 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022921 fc->linenr = 0;
22922 fc->returned = FALSE;
22923 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22926 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927
Bram Moolenaar33570922005-01-25 22:26:29 +000022928 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022929 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022930 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22931 * each argument variable and saves a lot of time.
22932 */
22933 /*
22934 * Init l: variables.
22935 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022936 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022937 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022938 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022939 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22940 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022941 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022942 name = v->di_key;
22943 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022944 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022945 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022946 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022947 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 v->di_tv.vval.v_dict = selfdict;
22949 ++selfdict->dv_refcount;
22950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022951
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 /*
22953 * Init a: variables.
22954 * Set a:0 to "argcount".
22955 * Set a:000 to a list with room for the "..." arguments.
22956 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022957 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022958 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022959 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022960 /* Use "name" to avoid a warning from some compiler that checks the
22961 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022962 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022963 name = v->di_key;
22964 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022965 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022966 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022968 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022969 v->di_tv.vval.v_list = &fc->l_varlist;
22970 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22971 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22972 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022973
22974 /*
22975 * Set a:firstline to "firstline" and a:lastline to "lastline".
22976 * Set a:name to named arguments.
22977 * Set a:N to the "..." arguments.
22978 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022979 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022980 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022981 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 (varnumber_T)lastline);
22983 for (i = 0; i < argcount; ++i)
22984 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022985 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022986 if (ai < 0)
22987 /* named argument a:name */
22988 name = FUNCARG(fp, i);
22989 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022990 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 /* "..." argument a:1, a:2, etc. */
22992 sprintf((char *)numbuf, "%d", ai + 1);
22993 name = numbuf;
22994 }
22995 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22996 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022997 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22999 }
23000 else
23001 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023002 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23003 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023004 if (v == NULL)
23005 break;
23006 v->di_flags = DI_FLAGS_RO;
23007 }
23008 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023009 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023010
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023011 /* Note: the values are copied directly to avoid alloc/free.
23012 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023014 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023015
23016 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23017 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023018 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23019 fc->l_listitems[ai].li_tv = argvars[i];
23020 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023021 }
23022 }
23023
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 /* Don't redraw while executing the function. */
23025 ++RedrawingDisabled;
23026 save_sourcing_name = sourcing_name;
23027 save_sourcing_lnum = sourcing_lnum;
23028 sourcing_lnum = 1;
23029 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023030 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 if (sourcing_name != NULL)
23032 {
23033 if (save_sourcing_name != NULL
23034 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23035 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23036 else
23037 STRCPY(sourcing_name, "function ");
23038 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23039
23040 if (p_verbose >= 12)
23041 {
23042 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023043 verbose_enter_scroll();
23044
Bram Moolenaar555b2802005-05-19 21:08:39 +000023045 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 if (p_verbose >= 14)
23047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023049 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023050 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023051 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052
23053 msg_puts((char_u *)"(");
23054 for (i = 0; i < argcount; ++i)
23055 {
23056 if (i > 0)
23057 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023058 if (argvars[i].v_type == VAR_NUMBER)
23059 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 else
23061 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023062 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23063 if (s != NULL)
23064 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023065 if (vim_strsize(s) > MSG_BUF_CLEN)
23066 {
23067 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23068 s = buf;
23069 }
23070 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023071 vim_free(tofree);
23072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 }
23074 }
23075 msg_puts((char_u *)")");
23076 }
23077 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023078
23079 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 --no_wait_return;
23081 }
23082 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023083#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023084 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023085 {
23086 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23087 func_do_profile(fp);
23088 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023089 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023090 {
23091 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023092 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023093 profile_zero(&fp->uf_tm_children);
23094 }
23095 script_prof_save(&wait_start);
23096 }
23097#endif
23098
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023100 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 save_did_emsg = did_emsg;
23102 did_emsg = FALSE;
23103
23104 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023105 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23107
23108 --RedrawingDisabled;
23109
23110 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023111 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023113 clear_tv(rettv);
23114 rettv->v_type = VAR_NUMBER;
23115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
23117
Bram Moolenaar05159a02005-02-26 23:04:13 +000023118#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023119 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023120 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023121 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023122 profile_end(&call_start);
23123 profile_sub_wait(&wait_start, &call_start);
23124 profile_add(&fp->uf_tm_total, &call_start);
23125 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023126 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023127 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023128 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23129 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023130 }
23131 }
23132#endif
23133
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 /* when being verbose, mention the return value */
23135 if (p_verbose >= 12)
23136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023138 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023141 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023142 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023143 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023144 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023147 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023148 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023149 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023150 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023151
Bram Moolenaar555b2802005-05-19 21:08:39 +000023152 /* The value may be very long. Skip the middle part, so that we
23153 * have some idea how it starts and ends. smsg() would always
23154 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023155 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023156 if (s != NULL)
23157 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023158 if (vim_strsize(s) > MSG_BUF_CLEN)
23159 {
23160 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23161 s = buf;
23162 }
23163 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023164 vim_free(tofree);
23165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 }
23167 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023168
23169 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 --no_wait_return;
23171 }
23172
23173 vim_free(sourcing_name);
23174 sourcing_name = save_sourcing_name;
23175 sourcing_lnum = save_sourcing_lnum;
23176 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023177#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023178 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023179 script_prof_restore(&wait_start);
23180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181
23182 if (p_verbose >= 12 && sourcing_name != NULL)
23183 {
23184 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023185 verbose_enter_scroll();
23186
Bram Moolenaar555b2802005-05-19 21:08:39 +000023187 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023189
23190 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 --no_wait_return;
23192 }
23193
23194 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023195 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023197
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023198 /* If the a:000 list and the l: and a: dicts are not referenced we can
23199 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023200 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23201 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23202 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23203 {
23204 free_funccal(fc, FALSE);
23205 }
23206 else
23207 {
23208 hashitem_T *hi;
23209 listitem_T *li;
23210 int todo;
23211
23212 /* "fc" is still in use. This can happen when returning "a:000" or
23213 * assigning "l:" to a global variable.
23214 * Link "fc" in the list for garbage collection later. */
23215 fc->caller = previous_funccal;
23216 previous_funccal = fc;
23217
23218 /* Make a copy of the a: variables, since we didn't do that above. */
23219 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23220 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23221 {
23222 if (!HASHITEM_EMPTY(hi))
23223 {
23224 --todo;
23225 v = HI2DI(hi);
23226 copy_tv(&v->di_tv, &v->di_tv);
23227 }
23228 }
23229
23230 /* Make a copy of the a:000 items, since we didn't do that above. */
23231 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23232 copy_tv(&li->li_tv, &li->li_tv);
23233 }
23234}
23235
23236/*
23237 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023238 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023239 */
23240 static int
23241can_free_funccal(fc, copyID)
23242 funccall_T *fc;
23243 int copyID;
23244{
23245 return (fc->l_varlist.lv_copyID != copyID
23246 && fc->l_vars.dv_copyID != copyID
23247 && fc->l_avars.dv_copyID != copyID);
23248}
23249
23250/*
23251 * Free "fc" and what it contains.
23252 */
23253 static void
23254free_funccal(fc, free_val)
23255 funccall_T *fc;
23256 int free_val; /* a: vars were allocated */
23257{
23258 listitem_T *li;
23259
23260 /* The a: variables typevals may not have been allocated, only free the
23261 * allocated variables. */
23262 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23263
23264 /* free all l: variables */
23265 vars_clear(&fc->l_vars.dv_hashtab);
23266
23267 /* Free the a:000 variables if they were allocated. */
23268 if (free_val)
23269 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23270 clear_tv(&li->li_tv);
23271
23272 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273}
23274
23275/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 * Add a number variable "name" to dict "dp" with value "nr".
23277 */
23278 static void
23279add_nr_var(dp, v, name, nr)
23280 dict_T *dp;
23281 dictitem_T *v;
23282 char *name;
23283 varnumber_T nr;
23284{
23285 STRCPY(v->di_key, name);
23286 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23287 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23288 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023289 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023290 v->di_tv.vval.v_number = nr;
23291}
23292
23293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 * ":return [expr]"
23295 */
23296 void
23297ex_return(eap)
23298 exarg_T *eap;
23299{
23300 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 int returning = FALSE;
23303
23304 if (current_funccal == NULL)
23305 {
23306 EMSG(_("E133: :return not inside a function"));
23307 return;
23308 }
23309
23310 if (eap->skip)
23311 ++emsg_skip;
23312
23313 eap->nextcmd = NULL;
23314 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023315 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 {
23317 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023318 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023320 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 }
23322 /* It's safer to return also on error. */
23323 else if (!eap->skip)
23324 {
23325 /*
23326 * Return unless the expression evaluation has been cancelled due to an
23327 * aborting error, an interrupt, or an exception.
23328 */
23329 if (!aborting())
23330 returning = do_return(eap, FALSE, TRUE, NULL);
23331 }
23332
23333 /* When skipping or the return gets pending, advance to the next command
23334 * in this line (!returning). Otherwise, ignore the rest of the line.
23335 * Following lines will be ignored by get_func_line(). */
23336 if (returning)
23337 eap->nextcmd = NULL;
23338 else if (eap->nextcmd == NULL) /* no argument */
23339 eap->nextcmd = check_nextcmd(arg);
23340
23341 if (eap->skip)
23342 --emsg_skip;
23343}
23344
23345/*
23346 * Return from a function. Possibly makes the return pending. Also called
23347 * for a pending return at the ":endtry" or after returning from an extra
23348 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023349 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023350 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 * FALSE when the return gets pending.
23352 */
23353 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023354do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 exarg_T *eap;
23356 int reanimate;
23357 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023358 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359{
23360 int idx;
23361 struct condstack *cstack = eap->cstack;
23362
23363 if (reanimate)
23364 /* Undo the return. */
23365 current_funccal->returned = FALSE;
23366
23367 /*
23368 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23369 * not in its finally clause (which then is to be executed next) is found.
23370 * In this case, make the ":return" pending for execution at the ":endtry".
23371 * Otherwise, return normally.
23372 */
23373 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23374 if (idx >= 0)
23375 {
23376 cstack->cs_pending[idx] = CSTP_RETURN;
23377
23378 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023379 /* A pending return again gets pending. "rettv" points to an
23380 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023382 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 else
23384 {
23385 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023386 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023388 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023390 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 {
23392 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023393 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 else
23396 EMSG(_(e_outofmem));
23397 }
23398 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023399 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400
23401 if (reanimate)
23402 {
23403 /* The pending return value could be overwritten by a ":return"
23404 * without argument in a finally clause; reset the default
23405 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023406 current_funccal->rettv->v_type = VAR_NUMBER;
23407 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 }
23409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023410 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 }
23412 else
23413 {
23414 current_funccal->returned = TRUE;
23415
23416 /* If the return is carried out now, store the return value. For
23417 * a return immediately after reanimation, the value is already
23418 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023419 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023421 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023422 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023424 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 }
23426 }
23427
23428 return idx < 0;
23429}
23430
23431/*
23432 * Free the variable with a pending return value.
23433 */
23434 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023435discard_pending_return(rettv)
23436 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437{
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439}
23440
23441/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023442 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 * is an allocated string. Used by report_pending() for verbose messages.
23444 */
23445 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023446get_return_cmd(rettv)
23447 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023449 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023450 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023451 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023453 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023454 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023455 if (s == NULL)
23456 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023457
23458 STRCPY(IObuff, ":return ");
23459 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23460 if (STRLEN(s) + 8 >= IOSIZE)
23461 STRCPY(IObuff + IOSIZE - 4, "...");
23462 vim_free(tofree);
23463 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464}
23465
23466/*
23467 * Get next function line.
23468 * Called by do_cmdline() to get the next line.
23469 * Returns allocated string, or NULL for end of function.
23470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 char_u *
23472get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023473 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023475 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476{
Bram Moolenaar33570922005-01-25 22:26:29 +000023477 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023478 ufunc_T *fp = fcp->func;
23479 char_u *retval;
23480 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481
23482 /* If breakpoints have been added/deleted need to check for it. */
23483 if (fcp->dbg_tick != debug_tick)
23484 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023485 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 sourcing_lnum);
23487 fcp->dbg_tick = debug_tick;
23488 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023489#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023490 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023491 func_line_end(cookie);
23492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493
Bram Moolenaar05159a02005-02-26 23:04:13 +000023494 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023495 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23496 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 retval = NULL;
23498 else
23499 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023500 /* Skip NULL lines (continuation lines). */
23501 while (fcp->linenr < gap->ga_len
23502 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23503 ++fcp->linenr;
23504 if (fcp->linenr >= gap->ga_len)
23505 retval = NULL;
23506 else
23507 {
23508 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23509 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023510#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023511 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023512 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023513#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 }
23516
23517 /* Did we encounter a breakpoint? */
23518 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23519 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023520 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023522 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 sourcing_lnum);
23524 fcp->dbg_tick = debug_tick;
23525 }
23526
23527 return retval;
23528}
23529
Bram Moolenaar05159a02005-02-26 23:04:13 +000023530#if defined(FEAT_PROFILE) || defined(PROTO)
23531/*
23532 * Called when starting to read a function line.
23533 * "sourcing_lnum" must be correct!
23534 * When skipping lines it may not actually be executed, but we won't find out
23535 * until later and we need to store the time now.
23536 */
23537 void
23538func_line_start(cookie)
23539 void *cookie;
23540{
23541 funccall_T *fcp = (funccall_T *)cookie;
23542 ufunc_T *fp = fcp->func;
23543
23544 if (fp->uf_profiling && sourcing_lnum >= 1
23545 && sourcing_lnum <= fp->uf_lines.ga_len)
23546 {
23547 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023548 /* Skip continuation lines. */
23549 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23550 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023551 fp->uf_tml_execed = FALSE;
23552 profile_start(&fp->uf_tml_start);
23553 profile_zero(&fp->uf_tml_children);
23554 profile_get_wait(&fp->uf_tml_wait);
23555 }
23556}
23557
23558/*
23559 * Called when actually executing a function line.
23560 */
23561 void
23562func_line_exec(cookie)
23563 void *cookie;
23564{
23565 funccall_T *fcp = (funccall_T *)cookie;
23566 ufunc_T *fp = fcp->func;
23567
23568 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23569 fp->uf_tml_execed = TRUE;
23570}
23571
23572/*
23573 * Called when done with a function line.
23574 */
23575 void
23576func_line_end(cookie)
23577 void *cookie;
23578{
23579 funccall_T *fcp = (funccall_T *)cookie;
23580 ufunc_T *fp = fcp->func;
23581
23582 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23583 {
23584 if (fp->uf_tml_execed)
23585 {
23586 ++fp->uf_tml_count[fp->uf_tml_idx];
23587 profile_end(&fp->uf_tml_start);
23588 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023589 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023590 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23591 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023592 }
23593 fp->uf_tml_idx = -1;
23594 }
23595}
23596#endif
23597
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598/*
23599 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023600 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 */
23602 int
23603func_has_ended(cookie)
23604 void *cookie;
23605{
Bram Moolenaar33570922005-01-25 22:26:29 +000023606 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607
23608 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23609 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023610 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 || fcp->returned);
23612}
23613
23614/*
23615 * return TRUE if cookie indicates a function which "abort"s on errors.
23616 */
23617 int
23618func_has_abort(cookie)
23619 void *cookie;
23620{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023621 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622}
23623
23624#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23625typedef enum
23626{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023627 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23628 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23629 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630} var_flavour_T;
23631
23632static var_flavour_T var_flavour __ARGS((char_u *varname));
23633
23634 static var_flavour_T
23635var_flavour(varname)
23636 char_u *varname;
23637{
23638 char_u *p = varname;
23639
23640 if (ASCII_ISUPPER(*p))
23641 {
23642 while (*(++p))
23643 if (ASCII_ISLOWER(*p))
23644 return VAR_FLAVOUR_SESSION;
23645 return VAR_FLAVOUR_VIMINFO;
23646 }
23647 else
23648 return VAR_FLAVOUR_DEFAULT;
23649}
23650#endif
23651
23652#if defined(FEAT_VIMINFO) || defined(PROTO)
23653/*
23654 * Restore global vars that start with a capital from the viminfo file
23655 */
23656 int
23657read_viminfo_varlist(virp, writing)
23658 vir_T *virp;
23659 int writing;
23660{
23661 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023662 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023663 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664
23665 if (!writing && (find_viminfo_parameter('!') != NULL))
23666 {
23667 tab = vim_strchr(virp->vir_line + 1, '\t');
23668 if (tab != NULL)
23669 {
23670 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023671 switch (*tab)
23672 {
23673 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023674#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023675 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023676#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023677 case 'D': type = VAR_DICT; break;
23678 case 'L': type = VAR_LIST; break;
23679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680
23681 tab = vim_strchr(tab, '\t');
23682 if (tab != NULL)
23683 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023684 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023685 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023686 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023688#ifdef FEAT_FLOAT
23689 else if (type == VAR_FLOAT)
23690 (void)string2float(tab + 1, &tv.vval.v_float);
23691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023693 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023694 if (type == VAR_DICT || type == VAR_LIST)
23695 {
23696 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23697
23698 if (etv == NULL)
23699 /* Failed to parse back the dict or list, use it as a
23700 * string. */
23701 tv.v_type = VAR_STRING;
23702 else
23703 {
23704 vim_free(tv.vval.v_string);
23705 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023706 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023707 }
23708 }
23709
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023710 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023711
23712 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023713 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023714 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23715 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 }
23717 }
23718 }
23719
23720 return viminfo_readline(virp);
23721}
23722
23723/*
23724 * Write global vars that start with a capital to the viminfo file
23725 */
23726 void
23727write_viminfo_varlist(fp)
23728 FILE *fp;
23729{
Bram Moolenaar33570922005-01-25 22:26:29 +000023730 hashitem_T *hi;
23731 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023732 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023733 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023734 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023735 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023736 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737
23738 if (find_viminfo_parameter('!') == NULL)
23739 return;
23740
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023741 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023742
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023743 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023744 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023746 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023748 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023749 this_var = HI2DI(hi);
23750 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023751 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023752 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023753 {
23754 case VAR_STRING: s = "STR"; break;
23755 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023756#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023757 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023758#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023759 case VAR_DICT: s = "DIC"; break;
23760 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023761 default: continue;
23762 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023763 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023764 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023765 if (p != NULL)
23766 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023767 vim_free(tofree);
23768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 }
23770 }
23771}
23772#endif
23773
23774#if defined(FEAT_SESSION) || defined(PROTO)
23775 int
23776store_session_globals(fd)
23777 FILE *fd;
23778{
Bram Moolenaar33570922005-01-25 22:26:29 +000023779 hashitem_T *hi;
23780 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023781 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 char_u *p, *t;
23783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023784 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023785 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023787 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023789 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023790 this_var = HI2DI(hi);
23791 if ((this_var->di_tv.v_type == VAR_NUMBER
23792 || this_var->di_tv.v_type == VAR_STRING)
23793 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023794 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023795 /* Escape special characters with a backslash. Turn a LF and
23796 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023797 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023798 (char_u *)"\\\"\n\r");
23799 if (p == NULL) /* out of memory */
23800 break;
23801 for (t = p; *t != NUL; ++t)
23802 if (*t == '\n')
23803 *t = 'n';
23804 else if (*t == '\r')
23805 *t = 'r';
23806 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023807 this_var->di_key,
23808 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23809 : ' ',
23810 p,
23811 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23812 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023813 || put_eol(fd) == FAIL)
23814 {
23815 vim_free(p);
23816 return FAIL;
23817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 vim_free(p);
23819 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023820#ifdef FEAT_FLOAT
23821 else if (this_var->di_tv.v_type == VAR_FLOAT
23822 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23823 {
23824 float_T f = this_var->di_tv.vval.v_float;
23825 int sign = ' ';
23826
23827 if (f < 0)
23828 {
23829 f = -f;
23830 sign = '-';
23831 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023832 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023833 this_var->di_key, sign, f) < 0)
23834 || put_eol(fd) == FAIL)
23835 return FAIL;
23836 }
23837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 }
23839 }
23840 return OK;
23841}
23842#endif
23843
Bram Moolenaar661b1822005-07-28 22:36:45 +000023844/*
23845 * Display script name where an item was last set.
23846 * Should only be invoked when 'verbose' is non-zero.
23847 */
23848 void
23849last_set_msg(scriptID)
23850 scid_T scriptID;
23851{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023852 char_u *p;
23853
Bram Moolenaar661b1822005-07-28 22:36:45 +000023854 if (scriptID != 0)
23855 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023856 p = home_replace_save(NULL, get_scriptname(scriptID));
23857 if (p != NULL)
23858 {
23859 verbose_enter();
23860 MSG_PUTS(_("\n\tLast set from "));
23861 MSG_PUTS(p);
23862 vim_free(p);
23863 verbose_leave();
23864 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023865 }
23866}
23867
Bram Moolenaard812df62008-11-09 12:46:09 +000023868/*
23869 * List v:oldfiles in a nice way.
23870 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023871 void
23872ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023873 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023874{
23875 list_T *l = vimvars[VV_OLDFILES].vv_list;
23876 listitem_T *li;
23877 int nr = 0;
23878
23879 if (l == NULL)
23880 msg((char_u *)_("No old files"));
23881 else
23882 {
23883 msg_start();
23884 msg_scroll = TRUE;
23885 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23886 {
23887 msg_outnum((long)++nr);
23888 MSG_PUTS(": ");
23889 msg_outtrans(get_tv_string(&li->li_tv));
23890 msg_putchar('\n');
23891 out_flush(); /* output one line at a time */
23892 ui_breakcheck();
23893 }
23894 /* Assume "got_int" was set to truncate the listing. */
23895 got_int = FALSE;
23896
23897#ifdef FEAT_BROWSE_CMD
23898 if (cmdmod.browse)
23899 {
23900 quit_more = FALSE;
23901 nr = prompt_for_number(FALSE);
23902 msg_starthere();
23903 if (nr > 0)
23904 {
23905 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23906 (long)nr);
23907
23908 if (p != NULL)
23909 {
23910 p = expand_env_save(p);
23911 eap->arg = p;
23912 eap->cmdidx = CMD_edit;
23913 cmdmod.browse = FALSE;
23914 do_exedit(eap, NULL);
23915 vim_free(p);
23916 }
23917 }
23918 }
23919#endif
23920 }
23921}
23922
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923#endif /* FEAT_EVAL */
23924
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023926#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927
23928#ifdef WIN3264
23929/*
23930 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23931 */
23932static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23933static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23934static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23935
23936/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023937 * Get the short path (8.3) for the filename in "fnamep".
23938 * Only works for a valid file name.
23939 * When the path gets longer "fnamep" is changed and the allocated buffer
23940 * is put in "bufp".
23941 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23942 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 */
23944 static int
23945get_short_pathname(fnamep, bufp, fnamelen)
23946 char_u **fnamep;
23947 char_u **bufp;
23948 int *fnamelen;
23949{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023950 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 char_u *newbuf;
23952
23953 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 l = GetShortPathName(*fnamep, *fnamep, len);
23955 if (l > len - 1)
23956 {
23957 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023958 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 newbuf = vim_strnsave(*fnamep, l);
23960 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023961 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962
23963 vim_free(*bufp);
23964 *fnamep = *bufp = newbuf;
23965
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023966 /* Really should always succeed, as the buffer is big enough. */
23967 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 }
23969
23970 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023971 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972}
23973
23974/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023975 * Get the short path (8.3) for the filename in "fname". The converted
23976 * path is returned in "bufp".
23977 *
23978 * Some of the directories specified in "fname" may not exist. This function
23979 * will shorten the existing directories at the beginning of the path and then
23980 * append the remaining non-existing path.
23981 *
23982 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023983 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023984 * bufp - Pointer to an allocated buffer for the filename.
23985 * fnamelen - Length of the filename pointed to by fname
23986 *
23987 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 */
23989 static int
23990shortpath_for_invalid_fname(fname, bufp, fnamelen)
23991 char_u **fname;
23992 char_u **bufp;
23993 int *fnamelen;
23994{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023995 char_u *short_fname, *save_fname, *pbuf_unused;
23996 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023998 int old_len, len;
23999 int new_len, sfx_len;
24000 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001
24002 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024003 old_len = *fnamelen;
24004 save_fname = vim_strnsave(*fname, old_len);
24005 pbuf_unused = NULL;
24006 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024008 endp = save_fname + old_len - 1; /* Find the end of the copy */
24009 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024011 /*
24012 * Try shortening the supplied path till it succeeds by removing one
24013 * directory at a time from the tail of the path.
24014 */
24015 len = 0;
24016 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024018 /* go back one path-separator */
24019 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24020 --endp;
24021 if (endp <= save_fname)
24022 break; /* processed the complete path */
24023
24024 /*
24025 * Replace the path separator with a NUL and try to shorten the
24026 * resulting path.
24027 */
24028 ch = *endp;
24029 *endp = 0;
24030 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024031 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024032 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24033 {
24034 retval = FAIL;
24035 goto theend;
24036 }
24037 *endp = ch; /* preserve the string */
24038
24039 if (len > 0)
24040 break; /* successfully shortened the path */
24041
24042 /* failed to shorten the path. Skip the path separator */
24043 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 }
24045
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024046 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024048 /*
24049 * Succeeded in shortening the path. Now concatenate the shortened
24050 * path with the remaining path at the tail.
24051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024053 /* Compute the length of the new path. */
24054 sfx_len = (int)(save_endp - endp) + 1;
24055 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024057 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024059 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024061 /* There is not enough space in the currently allocated string,
24062 * copy it to a buffer big enough. */
24063 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024065 {
24066 retval = FAIL;
24067 goto theend;
24068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 }
24070 else
24071 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024072 /* Transfer short_fname to the main buffer (it's big enough),
24073 * unless get_short_pathname() did its work in-place. */
24074 *fname = *bufp = save_fname;
24075 if (short_fname != save_fname)
24076 vim_strncpy(save_fname, short_fname, len);
24077 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024079
24080 /* concat the not-shortened part of the path */
24081 vim_strncpy(*fname + len, endp, sfx_len);
24082 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024084
24085theend:
24086 vim_free(pbuf_unused);
24087 vim_free(save_fname);
24088
24089 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090}
24091
24092/*
24093 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024094 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 */
24096 static int
24097shortpath_for_partial(fnamep, bufp, fnamelen)
24098 char_u **fnamep;
24099 char_u **bufp;
24100 int *fnamelen;
24101{
24102 int sepcount, len, tflen;
24103 char_u *p;
24104 char_u *pbuf, *tfname;
24105 int hasTilde;
24106
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024107 /* Count up the path separators from the RHS.. so we know which part
24108 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024110 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 if (vim_ispathsep(*p))
24112 ++sepcount;
24113
24114 /* Need full path first (use expand_env() to remove a "~/") */
24115 hasTilde = (**fnamep == '~');
24116 if (hasTilde)
24117 pbuf = tfname = expand_env_save(*fnamep);
24118 else
24119 pbuf = tfname = FullName_save(*fnamep, FALSE);
24120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024121 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024123 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24124 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125
24126 if (len == 0)
24127 {
24128 /* Don't have a valid filename, so shorten the rest of the
24129 * path if we can. This CAN give us invalid 8.3 filenames, but
24130 * there's not a lot of point in guessing what it might be.
24131 */
24132 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024133 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 }
24136
24137 /* Count the paths backward to find the beginning of the desired string. */
24138 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024139 {
24140#ifdef FEAT_MBYTE
24141 if (has_mbyte)
24142 p -= mb_head_off(tfname, p);
24143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 if (vim_ispathsep(*p))
24145 {
24146 if (sepcount == 0 || (hasTilde && sepcount == 1))
24147 break;
24148 else
24149 sepcount --;
24150 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 if (hasTilde)
24153 {
24154 --p;
24155 if (p >= tfname)
24156 *p = '~';
24157 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024158 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 }
24160 else
24161 ++p;
24162
24163 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24164 vim_free(*bufp);
24165 *fnamelen = (int)STRLEN(p);
24166 *bufp = pbuf;
24167 *fnamep = p;
24168
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024169 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170}
24171#endif /* WIN3264 */
24172
24173/*
24174 * Adjust a filename, according to a string of modifiers.
24175 * *fnamep must be NUL terminated when called. When returning, the length is
24176 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024177 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 * When there is an error, *fnamep is set to NULL.
24179 */
24180 int
24181modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24182 char_u *src; /* string with modifiers */
24183 int *usedlen; /* characters after src that are used */
24184 char_u **fnamep; /* file name so far */
24185 char_u **bufp; /* buffer for allocated file name or NULL */
24186 int *fnamelen; /* length of fnamep */
24187{
24188 int valid = 0;
24189 char_u *tail;
24190 char_u *s, *p, *pbuf;
24191 char_u dirname[MAXPATHL];
24192 int c;
24193 int has_fullname = 0;
24194#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024195 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 int has_shortname = 0;
24197#endif
24198
24199repeat:
24200 /* ":p" - full path/file_name */
24201 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24202 {
24203 has_fullname = 1;
24204
24205 valid |= VALID_PATH;
24206 *usedlen += 2;
24207
24208 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24209 if ((*fnamep)[0] == '~'
24210#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24211 && ((*fnamep)[1] == '/'
24212# ifdef BACKSLASH_IN_FILENAME
24213 || (*fnamep)[1] == '\\'
24214# endif
24215 || (*fnamep)[1] == NUL)
24216
24217#endif
24218 )
24219 {
24220 *fnamep = expand_env_save(*fnamep);
24221 vim_free(*bufp); /* free any allocated file name */
24222 *bufp = *fnamep;
24223 if (*fnamep == NULL)
24224 return -1;
24225 }
24226
24227 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024228 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 {
24230 if (vim_ispathsep(*p)
24231 && p[1] == '.'
24232 && (p[2] == NUL
24233 || vim_ispathsep(p[2])
24234 || (p[2] == '.'
24235 && (p[3] == NUL || vim_ispathsep(p[3])))))
24236 break;
24237 }
24238
24239 /* FullName_save() is slow, don't use it when not needed. */
24240 if (*p != NUL || !vim_isAbsName(*fnamep))
24241 {
24242 *fnamep = FullName_save(*fnamep, *p != NUL);
24243 vim_free(*bufp); /* free any allocated file name */
24244 *bufp = *fnamep;
24245 if (*fnamep == NULL)
24246 return -1;
24247 }
24248
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024249#ifdef WIN3264
24250# if _WIN32_WINNT >= 0x0500
24251 if (vim_strchr(*fnamep, '~') != NULL)
24252 {
24253 /* Expand 8.3 filename to full path. Needed to make sure the same
24254 * file does not have two different names.
24255 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24256 p = alloc(_MAX_PATH + 1);
24257 if (p != NULL)
24258 {
24259 if (GetLongPathName(*fnamep, p, MAXPATHL))
24260 {
24261 vim_free(*bufp);
24262 *bufp = *fnamep = p;
24263 }
24264 else
24265 vim_free(p);
24266 }
24267 }
24268# endif
24269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 /* Append a path separator to a directory. */
24271 if (mch_isdir(*fnamep))
24272 {
24273 /* Make room for one or two extra characters. */
24274 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24275 vim_free(*bufp); /* free any allocated file name */
24276 *bufp = *fnamep;
24277 if (*fnamep == NULL)
24278 return -1;
24279 add_pathsep(*fnamep);
24280 }
24281 }
24282
24283 /* ":." - path relative to the current directory */
24284 /* ":~" - path relative to the home directory */
24285 /* ":8" - shortname path - postponed till after */
24286 while (src[*usedlen] == ':'
24287 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24288 {
24289 *usedlen += 2;
24290 if (c == '8')
24291 {
24292#ifdef WIN3264
24293 has_shortname = 1; /* Postpone this. */
24294#endif
24295 continue;
24296 }
24297 pbuf = NULL;
24298 /* Need full path first (use expand_env() to remove a "~/") */
24299 if (!has_fullname)
24300 {
24301 if (c == '.' && **fnamep == '~')
24302 p = pbuf = expand_env_save(*fnamep);
24303 else
24304 p = pbuf = FullName_save(*fnamep, FALSE);
24305 }
24306 else
24307 p = *fnamep;
24308
24309 has_fullname = 0;
24310
24311 if (p != NULL)
24312 {
24313 if (c == '.')
24314 {
24315 mch_dirname(dirname, MAXPATHL);
24316 s = shorten_fname(p, dirname);
24317 if (s != NULL)
24318 {
24319 *fnamep = s;
24320 if (pbuf != NULL)
24321 {
24322 vim_free(*bufp); /* free any allocated file name */
24323 *bufp = pbuf;
24324 pbuf = NULL;
24325 }
24326 }
24327 }
24328 else
24329 {
24330 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24331 /* Only replace it when it starts with '~' */
24332 if (*dirname == '~')
24333 {
24334 s = vim_strsave(dirname);
24335 if (s != NULL)
24336 {
24337 *fnamep = s;
24338 vim_free(*bufp);
24339 *bufp = s;
24340 }
24341 }
24342 }
24343 vim_free(pbuf);
24344 }
24345 }
24346
24347 tail = gettail(*fnamep);
24348 *fnamelen = (int)STRLEN(*fnamep);
24349
24350 /* ":h" - head, remove "/file_name", can be repeated */
24351 /* Don't remove the first "/" or "c:\" */
24352 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24353 {
24354 valid |= VALID_HEAD;
24355 *usedlen += 2;
24356 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024357 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024358 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 *fnamelen = (int)(tail - *fnamep);
24360#ifdef VMS
24361 if (*fnamelen > 0)
24362 *fnamelen += 1; /* the path separator is part of the path */
24363#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024364 if (*fnamelen == 0)
24365 {
24366 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24367 p = vim_strsave((char_u *)".");
24368 if (p == NULL)
24369 return -1;
24370 vim_free(*bufp);
24371 *bufp = *fnamep = tail = p;
24372 *fnamelen = 1;
24373 }
24374 else
24375 {
24376 while (tail > s && !after_pathsep(s, tail))
24377 mb_ptr_back(*fnamep, tail);
24378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 }
24380
24381 /* ":8" - shortname */
24382 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24383 {
24384 *usedlen += 2;
24385#ifdef WIN3264
24386 has_shortname = 1;
24387#endif
24388 }
24389
24390#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024391 /*
24392 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 */
24394 if (has_shortname)
24395 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024396 /* Copy the string if it is shortened by :h and when it wasn't copied
24397 * yet, because we are going to change it in place. Avoids changing
24398 * the buffer name for "%:8". */
24399 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 {
24401 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024402 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 return -1;
24404 vim_free(*bufp);
24405 *bufp = *fnamep = p;
24406 }
24407
24408 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024409 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 if (!has_fullname && !vim_isAbsName(*fnamep))
24411 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024412 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 return -1;
24414 }
24415 else
24416 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024417 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418
Bram Moolenaardc935552011-08-17 15:23:23 +020024419 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024421 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422 return -1;
24423
24424 if (l == 0)
24425 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024426 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024428 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 return -1;
24430 }
24431 *fnamelen = l;
24432 }
24433 }
24434#endif /* WIN3264 */
24435
24436 /* ":t" - tail, just the basename */
24437 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24438 {
24439 *usedlen += 2;
24440 *fnamelen -= (int)(tail - *fnamep);
24441 *fnamep = tail;
24442 }
24443
24444 /* ":e" - extension, can be repeated */
24445 /* ":r" - root, without extension, can be repeated */
24446 while (src[*usedlen] == ':'
24447 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24448 {
24449 /* find a '.' in the tail:
24450 * - for second :e: before the current fname
24451 * - otherwise: The last '.'
24452 */
24453 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24454 s = *fnamep - 2;
24455 else
24456 s = *fnamep + *fnamelen - 1;
24457 for ( ; s > tail; --s)
24458 if (s[0] == '.')
24459 break;
24460 if (src[*usedlen + 1] == 'e') /* :e */
24461 {
24462 if (s > tail)
24463 {
24464 *fnamelen += (int)(*fnamep - (s + 1));
24465 *fnamep = s + 1;
24466#ifdef VMS
24467 /* cut version from the extension */
24468 s = *fnamep + *fnamelen - 1;
24469 for ( ; s > *fnamep; --s)
24470 if (s[0] == ';')
24471 break;
24472 if (s > *fnamep)
24473 *fnamelen = s - *fnamep;
24474#endif
24475 }
24476 else if (*fnamep <= tail)
24477 *fnamelen = 0;
24478 }
24479 else /* :r */
24480 {
24481 if (s > tail) /* remove one extension */
24482 *fnamelen = (int)(s - *fnamep);
24483 }
24484 *usedlen += 2;
24485 }
24486
24487 /* ":s?pat?foo?" - substitute */
24488 /* ":gs?pat?foo?" - global substitute */
24489 if (src[*usedlen] == ':'
24490 && (src[*usedlen + 1] == 's'
24491 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24492 {
24493 char_u *str;
24494 char_u *pat;
24495 char_u *sub;
24496 int sep;
24497 char_u *flags;
24498 int didit = FALSE;
24499
24500 flags = (char_u *)"";
24501 s = src + *usedlen + 2;
24502 if (src[*usedlen + 1] == 'g')
24503 {
24504 flags = (char_u *)"g";
24505 ++s;
24506 }
24507
24508 sep = *s++;
24509 if (sep)
24510 {
24511 /* find end of pattern */
24512 p = vim_strchr(s, sep);
24513 if (p != NULL)
24514 {
24515 pat = vim_strnsave(s, (int)(p - s));
24516 if (pat != NULL)
24517 {
24518 s = p + 1;
24519 /* find end of substitution */
24520 p = vim_strchr(s, sep);
24521 if (p != NULL)
24522 {
24523 sub = vim_strnsave(s, (int)(p - s));
24524 str = vim_strnsave(*fnamep, *fnamelen);
24525 if (sub != NULL && str != NULL)
24526 {
24527 *usedlen = (int)(p + 1 - src);
24528 s = do_string_sub(str, pat, sub, flags);
24529 if (s != NULL)
24530 {
24531 *fnamep = s;
24532 *fnamelen = (int)STRLEN(s);
24533 vim_free(*bufp);
24534 *bufp = s;
24535 didit = TRUE;
24536 }
24537 }
24538 vim_free(sub);
24539 vim_free(str);
24540 }
24541 vim_free(pat);
24542 }
24543 }
24544 /* after using ":s", repeat all the modifiers */
24545 if (didit)
24546 goto repeat;
24547 }
24548 }
24549
Bram Moolenaar26df0922014-02-23 23:39:13 +010024550 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24551 {
24552 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24553 if (p == NULL)
24554 return -1;
24555 vim_free(*bufp);
24556 *bufp = *fnamep = p;
24557 *fnamelen = (int)STRLEN(p);
24558 *usedlen += 2;
24559 }
24560
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 return valid;
24562}
24563
24564/*
24565 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24566 * "flags" can be "g" to do a global substitute.
24567 * Returns an allocated string, NULL for error.
24568 */
24569 char_u *
24570do_string_sub(str, pat, sub, flags)
24571 char_u *str;
24572 char_u *pat;
24573 char_u *sub;
24574 char_u *flags;
24575{
24576 int sublen;
24577 regmatch_T regmatch;
24578 int i;
24579 int do_all;
24580 char_u *tail;
24581 garray_T ga;
24582 char_u *ret;
24583 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024584 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585
24586 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24587 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024588 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589
24590 ga_init2(&ga, 1, 200);
24591
24592 do_all = (flags[0] == 'g');
24593
24594 regmatch.rm_ic = p_ic;
24595 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24596 if (regmatch.regprog != NULL)
24597 {
24598 tail = str;
24599 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24600 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024601 /* Skip empty match except for first match. */
24602 if (regmatch.startp[0] == regmatch.endp[0])
24603 {
24604 if (zero_width == regmatch.startp[0])
24605 {
24606 /* avoid getting stuck on a match with an empty string */
24607 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24608 ++ga.ga_len;
24609 continue;
24610 }
24611 zero_width = regmatch.startp[0];
24612 }
24613
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 /*
24615 * Get some space for a temporary buffer to do the substitution
24616 * into. It will contain:
24617 * - The text up to where the match is.
24618 * - The substituted text.
24619 * - The text after the match.
24620 */
24621 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24622 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24623 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24624 {
24625 ga_clear(&ga);
24626 break;
24627 }
24628
24629 /* copy the text up to where the match is */
24630 i = (int)(regmatch.startp[0] - tail);
24631 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24632 /* add the substituted text */
24633 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24634 + ga.ga_len + i, TRUE, TRUE, FALSE);
24635 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024636 tail = regmatch.endp[0];
24637 if (*tail == NUL)
24638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 if (!do_all)
24640 break;
24641 }
24642
24643 if (ga.ga_data != NULL)
24644 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24645
Bram Moolenaar473de612013-06-08 18:19:48 +020024646 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 }
24648
24649 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24650 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024651 if (p_cpo == empty_option)
24652 p_cpo = save_cpo;
24653 else
24654 /* Darn, evaluating {sub} expression changed the value. */
24655 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656
24657 return ret;
24658}
24659
24660#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */