blob: 0b2c32316673ab5536b1a11b2b58d38f4499f38b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000730static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000732static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000733static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000735static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200736#ifdef FEAT_FLOAT
737static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#ifdef FEAT_FLOAT
744static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
745#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200747static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200748static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100749static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100753static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000760static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100764static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000766static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000767static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int get_env_len __ARGS((char_u **arg));
769static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
772#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
773#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
774 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000775static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000777static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100778static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000779static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static typval_T *alloc_tv __ARGS((void));
781static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static void init_tv __ARGS((typval_T *varp));
783static long get_tv_number __ARGS((typval_T *varp));
784static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000785static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static char_u *get_tv_string __ARGS((typval_T *varp));
787static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000788static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100789static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
790static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
792static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
793static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000794static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
795static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
797static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000798static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200799static int var_check_func_name __ARGS((char_u *name, int new_var));
800static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000801static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000802static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
804static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
805static int eval_fname_script __ARGS((char_u *p));
806static int eval_fname_sid __ARGS((char_u *p));
807static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static ufunc_T *find_func __ARGS((char_u *name));
809static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000810static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
812static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000813static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
814static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_total_cmp __ARGS((const void *s1, const void *s2));
820static int
821# ifdef __BORLANDC__
822 _RTLENTRYF
823# endif
824 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200826static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000827static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000828static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000831static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
832static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
835static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000836static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000838static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200840
841#ifdef EBCDIC
842static int compare_func_name __ARGS((const void *s1, const void *s2));
843static void sortFunctions __ARGS(());
844#endif
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
856 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200857 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000858 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
861 for (i = 0; i < VV_LEN; ++i)
862 {
863 p = &vimvars[i];
864 STRCPY(p->vv_di.di_key, p->vv_name);
865 if (p->vv_flags & VV_RO)
866 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
867 else if (p->vv_flags & VV_RO_SBX)
868 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
869 else
870 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000871
872 /* add to v: scope dict, unless the value is not always available */
873 if (p->vv_type != VAR_UNKNOWN)
874 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000876 /* add to compat scope dict */
877 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000879 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100880 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200881 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
884 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100885 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886 */
887 sortFunctions();
888#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000889}
890
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000891#if defined(EXITFREE) || defined(PROTO)
892 void
893eval_clear()
894{
895 int i;
896 struct vimvar *p;
897
898 for (i = 0; i < VV_LEN; ++i)
899 {
900 p = &vimvars[i];
901 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000903 vim_free(p->vv_str);
904 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000905 }
906 else if (p->vv_di.di_tv.v_type == VAR_LIST)
907 {
908 list_unref(p->vv_list);
909 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 }
912 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000913 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 hash_clear(&compat_hashtab);
915
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100917# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200918 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100919# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920
921 /* global variables */
922 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000923
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000924 /* autoloaded script names */
925 ga_clear_strings(&ga_loaded);
926
Bram Moolenaarcca74132013-09-25 21:00:28 +0200927 /* Script-local variables. First clear all the variables and in a second
928 * loop free the scriptvar_T, because a variable in one script might hold
929 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200932 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 ga_clear(&ga_scripts);
935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000936 /* unreferenced lists and dicts */
937 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000938
939 /* functions */
940 free_all_functions();
941 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942}
943#endif
944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Return the name of the executed function.
947 */
948 char_u *
949func_name(cookie)
950 void *cookie;
951{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the next breakpoint line for a funccall cookie.
957 */
958 linenr_T *
959func_breakpoint(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the debug tick for a funccall cookie.
967 */
968 int *
969func_dbg_tick(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the nesting level for a funccall cookie.
977 */
978 int
979func_level(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000986funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000988/* pointer to list of previously used funccal, still around because some
989 * item in it is still being used. */
990funccall_T *previous_funccal = NULL;
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Return TRUE when a function was ended by a ":return" command.
994 */
995 int
996current_func_returned()
997{
998 return current_funccal->returned;
999}
1000
1001
1002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * Set an internal variable to a string value. Creates the variable if it does
1004 * not already exist.
1005 */
1006 void
1007set_internal_string_var(name, value)
1008 char_u *name;
1009 char_u *value;
1010{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 val = vim_strsave(value);
1015 if (val != NULL)
1016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001018 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 }
1024}
1025
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028static char_u *redir_endp = NULL;
1029static char_u *redir_varname = NULL;
1030
1031/*
1032 * Start recording command output to a variable
1033 * Returns OK if successfully completed the setup. FAIL otherwise.
1034 */
1035 int
1036var_redir_start(name, append)
1037 char_u *name;
1038 int append; /* append to an existing variable */
1039{
1040 int save_emsg;
1041 int err;
1042 typval_T tv;
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001045 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 {
1047 EMSG(_(e_invarg));
1048 return FAIL;
1049 }
1050
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001051 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 redir_varname = vim_strsave(name);
1053 if (redir_varname == NULL)
1054 return FAIL;
1055
1056 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1057 if (redir_lval == NULL)
1058 {
1059 var_redir_stop();
1060 return FAIL;
1061 }
1062
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001063 /* The output is stored in growarray "redir_ga" until redirection ends. */
1064 ga_init2(&redir_ga, (int)sizeof(char), 500);
1065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001067 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001068 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1070 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001071 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp != NULL && *redir_endp != NUL)
1073 /* Trailing characters are present after the variable name */
1074 EMSG(_(e_trailing));
1075 else
1076 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 var_redir_stop();
1079 return FAIL;
1080 }
1081
1082 /* check if we can write to the variable: set it to or append an empty
1083 * string */
1084 save_emsg = did_emsg;
1085 did_emsg = FALSE;
1086 tv.v_type = VAR_STRING;
1087 tv.vval.v_string = (char_u *)"";
1088 if (append)
1089 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1090 else
1091 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001092 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001094 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (err)
1096 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001097 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 var_redir_stop();
1099 return FAIL;
1100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106 * Append "value[value_len]" to the variable set by var_redir_start().
1107 * The actual appending is postponed until redirection ends, because the value
1108 * appended may in fact be the string we write to, changing it may cause freed
1109 * memory to be used:
1110 * :redir => foo
1111 * :let foo
1112 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 */
1114 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
1121 if (redir_lval == NULL)
1122 return;
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 {
1131 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 }
1134 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136}
1137
1138/*
1139 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
1143var_redir_stop()
1144{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145 typval_T tv;
1146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_lval != NULL)
1148 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* If there was no error: assign the text to the variable. */
1150 if (redir_endp != NULL)
1151 {
1152 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 /* Call get_lval() again, if it's inside a Dict or List it may
1156 * have changed. */
1157 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001158 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1160 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1161 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 /* free the collected output */
1165 vim_free(redir_ga.ga_data);
1166 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 vim_free(redir_lval);
1169 redir_lval = NULL;
1170 }
1171 vim_free(redir_varname);
1172 redir_varname = NULL;
1173}
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# if defined(FEAT_MBYTE) || defined(PROTO)
1176 int
1177eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1178 char_u *enc_from;
1179 char_u *enc_to;
1180 char_u *fname_from;
1181 char_u *fname_to;
1182{
1183 int err = FALSE;
1184
1185 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1186 set_vim_var_string(VV_CC_TO, enc_to, -1);
1187 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1188 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1189 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1190 err = TRUE;
1191 set_vim_var_string(VV_CC_FROM, NULL, -1);
1192 set_vim_var_string(VV_CC_TO, NULL, -1);
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1195
1196 if (err)
1197 return FAIL;
1198 return OK;
1199}
1200# endif
1201
1202# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1203 int
1204eval_printexpr(fname, args)
1205 char_u *fname;
1206 char_u *args;
1207{
1208 int err = FALSE;
1209
1210 set_vim_var_string(VV_FNAME_IN, fname, -1);
1211 set_vim_var_string(VV_CMDARG, args, -1);
1212 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1215 set_vim_var_string(VV_CMDARG, NULL, -1);
1216
1217 if (err)
1218 {
1219 mch_remove(fname);
1220 return FAIL;
1221 }
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_DIFF) || defined(PROTO)
1227 void
1228eval_diff(origfile, newfile, outfile)
1229 char_u *origfile;
1230 char_u *newfile;
1231 char_u *outfile;
1232{
1233 int err = FALSE;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243
1244 void
1245eval_patch(origfile, difffile, outfile)
1246 char_u *origfile;
1247 char_u *difffile;
1248 char_u *outfile;
1249{
1250 int err;
1251
1252 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1253 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1254 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1255 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259}
1260# endif
1261
1262/*
1263 * Top level evaluation function, returning a boolean.
1264 * Sets "error" to TRUE if there was an error.
1265 * Return TRUE or FALSE.
1266 */
1267 int
1268eval_to_bool(arg, error, nextcmd, skip)
1269 char_u *arg;
1270 int *error;
1271 char_u **nextcmd;
1272 int skip; /* only parse, don't execute */
1273{
Bram Moolenaar33570922005-01-25 22:26:29 +00001274 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int retval = FALSE;
1276
1277 if (skip)
1278 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else
1282 {
1283 *error = FALSE;
1284 if (!skip)
1285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 }
1290 if (skip)
1291 --emsg_skip;
1292
1293 return retval;
1294}
1295
1296/*
1297 * Top level evaluation function, returning a string. If "skip" is TRUE,
1298 * only parsing to "nextcmd" is done, without reporting errors. Return
1299 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1300 */
1301 char_u *
1302eval_to_string_skip(arg, nextcmd, skip)
1303 char_u *arg;
1304 char_u **nextcmd;
1305 int skip; /* only parse, don't execute */
1306{
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *retval;
1309
1310 if (skip)
1311 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 retval = NULL;
1314 else
1315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 retval = vim_strsave(get_tv_string(&tv));
1317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 if (skip)
1320 --emsg_skip;
1321
1322 return retval;
1323}
1324
1325/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326 * Skip over an expression at "*pp".
1327 * Return FAIL for an error, OK otherwise.
1328 */
1329 int
1330skip_expr(pp)
1331 char_u **pp;
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334
1335 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337}
1338
1339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 * When "convert" is TRUE convert a List into a sequence of lines and convert
1342 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Return pointer to allocated memory, or NULL for failure.
1344 */
1345 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *arg;
1348 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001354#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 retval = NULL;
1360 else
1361 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 {
1364 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001365 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001366 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001367 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001368 if (tv.vval.v_list->lv_len > 0)
1369 ga_append(&ga, NL);
1370 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 ga_append(&ga, NUL);
1372 retval = (char_u *)ga.ga_data;
1373 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374#ifdef FEAT_FLOAT
1375 else if (convert && tv.v_type == VAR_FLOAT)
1376 {
1377 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1378 retval = vim_strsave(numbuf);
1379 }
1380#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 else
1382 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 * Call eval_to_string() without using current local variables and using
1391 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 */
1393 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *arg;
1396 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 char_u *retval;
1400 void *save_funccalp;
1401
1402 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403 if (use_sandbox)
1404 ++sandbox;
1405 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 if (use_sandbox)
1408 --sandbox;
1409 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 restore_funccal(save_funccalp);
1411 return retval;
1412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Top level evaluation function, returning a number.
1416 * Evaluates "expr" silently.
1417 * Returns -1 for an error.
1418 */
1419 int
1420eval_to_number(expr)
1421 char_u *expr;
1422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001425 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427 ++emsg_off;
1428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 retval = -1;
1431 else
1432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001433 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436 --emsg_off;
1437
1438 return retval;
1439}
1440
Bram Moolenaara40058a2005-07-11 22:42:07 +00001441/*
1442 * Prepare v: variable "idx" to be used.
1443 * Save the current typeval in "save_tv".
1444 * When not used yet add the variable to the v: hashtable.
1445 */
1446 static void
1447prepare_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 *save_tv = vimvars[idx].vv_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1454}
1455
1456/*
1457 * Restore v: variable "idx" to typeval "save_tv".
1458 * When no longer defined, remove the variable from the v: hashtable.
1459 */
1460 static void
1461restore_vimvar(idx, save_tv)
1462 int idx;
1463 typval_T *save_tv;
1464{
1465 hashitem_T *hi;
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467 vimvars[idx].vv_tv = *save_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 {
1470 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1471 if (HASHITEM_EMPTY(hi))
1472 EMSG2(_(e_intern2), "restore_vimvar()");
1473 else
1474 hash_remove(&vimvarht, hi);
1475 }
1476}
1477
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001478#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001479/*
1480 * Evaluate an expression to a list with suggestions.
1481 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001482 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001483 */
1484 list_T *
1485eval_spell_expr(badword, expr)
1486 char_u *badword;
1487 char_u *expr;
1488{
1489 typval_T save_val;
1490 typval_T rettv;
1491 list_T *list = NULL;
1492 char_u *p = skipwhite(expr);
1493
1494 /* Set "v:val" to the bad word. */
1495 prepare_vimvar(VV_VAL, &save_val);
1496 vimvars[VV_VAL].vv_type = VAR_STRING;
1497 vimvars[VV_VAL].vv_str = badword;
1498 if (p_verbose == 0)
1499 ++emsg_off;
1500
1501 if (eval1(&p, &rettv, TRUE) == OK)
1502 {
1503 if (rettv.v_type != VAR_LIST)
1504 clear_tv(&rettv);
1505 else
1506 list = rettv.vval.v_list;
1507 }
1508
1509 if (p_verbose == 0)
1510 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001511 restore_vimvar(VV_VAL, &save_val);
1512
1513 return list;
1514}
1515
1516/*
1517 * "list" is supposed to contain two items: a word and a number. Return the
1518 * word in "pp" and the number as the return value.
1519 * Return -1 if anything isn't right.
1520 * Used to get the good word and score from the eval_spell_expr() result.
1521 */
1522 int
1523get_spellword(list, pp)
1524 list_T *list;
1525 char_u **pp;
1526{
1527 listitem_T *li;
1528
1529 li = list->lv_first;
1530 if (li == NULL)
1531 return -1;
1532 *pp = get_tv_string(&li->li_tv);
1533
1534 li = li->li_next;
1535 if (li == NULL)
1536 return -1;
1537 return get_tv_number(&li->li_tv);
1538}
1539#endif
1540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 * Top level evaluation function.
1543 * Returns an allocated typval_T with the result.
1544 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 */
1546 typval_T *
1547eval_expr(arg, nextcmd)
1548 char_u *arg;
1549 char_u **nextcmd;
1550{
1551 typval_T *tv;
1552
1553 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 {
1556 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 }
1559
1560 return tv;
1561}
1562
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 * Uses argv[argc] for the function arguments. Only Number and String
1567 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001570 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001571call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 char_u *func;
1573 int argc;
1574 char_u **argv;
1575 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001576 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar33570922005-01-25 22:26:29 +00001579 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 long n;
1581 int len;
1582 int i;
1583 int doesrange;
1584 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001587 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 for (i = 0; i < argc; i++)
1592 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 /* Pass a NULL or empty argument as an empty string */
1594 if (argv[i] == NULL || *argv[i] == NUL)
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_STRING;
1597 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001598 continue;
1599 }
1600
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001601 if (str_arg_only)
1602 len = 0;
1603 else
1604 /* Recognize a number argument, the others must be strings. */
1605 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (len != 0 && len == (int)STRLEN(argv[i]))
1607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001608 argvars[i].v_type = VAR_NUMBER;
1609 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001613 argvars[i].v_type = VAR_STRING;
1614 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 }
1617
1618 if (safe)
1619 {
1620 save_funccalp = save_funccal();
1621 ++sandbox;
1622 }
1623
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1625 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (safe)
1629 {
1630 --sandbox;
1631 restore_funccal(save_funccalp);
1632 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 vim_free(argvars);
1634
1635 if (ret == FAIL)
1636 clear_tv(rettv);
1637
1638 return ret;
1639}
1640
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001641/*
1642 * Call vimL function "func" and return the result as a number.
1643 * Returns -1 when calling the function fails.
1644 * Uses argv[argc] for the function arguments.
1645 */
1646 long
1647call_func_retnr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
1654 long retval;
1655
1656 /* All arguments are passed as strings, no conversion to number. */
1657 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1658 return -1;
1659
1660 retval = get_tv_number_chk(&rettv, NULL);
1661 clear_tv(&rettv);
1662 return retval;
1663}
1664
1665#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1666 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1667
Bram Moolenaar4f688582007-07-24 12:34:30 +00001668# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670 * Call vimL function "func" and return the result as a string.
1671 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Uses argv[argc] for the function arguments.
1673 */
1674 void *
1675call_func_retstr(func, argc, argv, safe)
1676 char_u *func;
1677 int argc;
1678 char_u **argv;
1679 int safe; /* use the sandbox */
1680{
1681 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001684 /* All arguments are passed as strings, no conversion to number. */
1685 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 return NULL;
1687
1688 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 return retval;
1691}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001695 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001697 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 */
1699 void *
1700call_func_retlist(func, argc, argv, safe)
1701 char_u *func;
1702 int argc;
1703 char_u **argv;
1704 int safe; /* use the sandbox */
1705{
1706 typval_T rettv;
1707
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 return NULL;
1711
1712 if (rettv.v_type != VAR_LIST)
1713 {
1714 clear_tv(&rettv);
1715 return NULL;
1716 }
1717
1718 return rettv.vval.v_list;
1719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721
1722/*
1723 * Save the current function call pointer, and set it to NULL.
1724 * Used when executing autocommands and for ":source".
1725 */
1726 void *
1727save_funccal()
1728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 current_funccal = NULL;
1732 return (void *)fc;
1733}
1734
1735 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736restore_funccal(vfc)
1737 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = (funccall_T *)vfc;
1740
1741 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742}
1743
Bram Moolenaar05159a02005-02-26 23:04:13 +00001744#if defined(FEAT_PROFILE) || defined(PROTO)
1745/*
1746 * Prepare profiling for entering a child or something else that is not
1747 * counted for the script/function itself.
1748 * Should always be called in pair with prof_child_exit().
1749 */
1750 void
1751prof_child_enter(tm)
1752 proftime_T *tm; /* place to store waittime */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 profile_start(&fc->prof_child);
1758 script_prof_save(tm);
1759}
1760
1761/*
1762 * Take care of time spent in a child.
1763 * Should always be called after prof_child_enter().
1764 */
1765 void
1766prof_child_exit(tm)
1767 proftime_T *tm; /* where waittime was stored */
1768{
1769 funccall_T *fc = current_funccal;
1770
1771 if (fc != NULL && fc->func->uf_profiling)
1772 {
1773 profile_end(&fc->prof_child);
1774 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1775 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1776 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1777 }
1778 script_prof_restore(tm);
1779}
1780#endif
1781
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#ifdef FEAT_FOLDING
1784/*
1785 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1786 * it in "*cp". Doesn't give error messages.
1787 */
1788 int
1789eval_foldexpr(arg, cp)
1790 char_u *arg;
1791 int *cp;
1792{
Bram Moolenaar33570922005-01-25 22:26:29 +00001793 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 int retval;
1795 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001796 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1797 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001800 if (use_sandbox)
1801 ++sandbox;
1802 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 if (tv.v_type == VAR_NUMBER)
1810 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001811 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 retval = 0;
1813 else
1814 {
1815 /* If the result is a string, check if there is a non-digit before
1816 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (!VIM_ISDIGIT(*s) && *s != '-')
1819 *cp = *s++;
1820 retval = atol((char *)s);
1821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 --sandbox;
1827 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 return retval;
1830}
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 * ":let" list all variable values
1835 * ":let var1 var2" list variable values
1836 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 * ":let var += expr" assignment command.
1838 * ":let var -= expr" assignment command.
1839 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 void
1843ex_let(eap)
1844 exarg_T *eap;
1845{
1846 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 int var_count = 0;
1851 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001852 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 argend = skip_var_list(arg, &var_count, &semicolon);
1857 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001859 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1860 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001861 expr = skipwhite(argend);
1862 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1863 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001865 /*
1866 * ":let" without "=": list variables
1867 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 if (*arg == '[')
1869 EMSG(_(e_invarg));
1870 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_glob_vars(&first);
1877 list_buf_vars(&first);
1878 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001879#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_script_vars(&first);
1883 list_func_vars(&first);
1884 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 eap->nextcmd = check_nextcmd(arg);
1887 }
1888 else
1889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 op[0] = '=';
1891 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001892 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001894 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1895 op[0] = *expr; /* +=, -= or .= */
1896 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001898 else
1899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002250 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002530 * flags:
2531 * GLV_QUIET: do not give error messages
2532 * GLV_NO_AUTOLOAD: do not use script autoloading
2533 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002535 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 */
2538 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 typval_T *rettv;
2542 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 int unlet;
2544 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002545 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002546 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 char_u *expr_start, *expr_end;
2550 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 dictitem_T *v;
2552 typval_T var1;
2553 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563
2564 if (skip)
2565 {
2566 /* When skipping just find the end of the name. */
2567 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002568 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 }
2570
2571 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (expr_start != NULL)
2574 {
2575 /* Don't expand the name when we already know there is an error. */
2576 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2577 && *p != '[' && *p != '.')
2578 {
2579 EMSG(_(e_trailing));
2580 return NULL;
2581 }
2582
2583 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2584 if (lp->ll_exp_name == NULL)
2585 {
2586 /* Report an invalid expression in braces, unless the
2587 * expression evaluation has been cancelled due to an
2588 * aborting error, an interrupt, or an exception. */
2589 if (!aborting() && !quiet)
2590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002591 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 EMSG2(_(e_invarg2), name);
2593 return NULL;
2594 }
2595 }
2596 lp->ll_name = lp->ll_exp_name;
2597 }
2598 else
2599 lp->ll_name = name;
2600
2601 /* Without [idx] or .key we are done. */
2602 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2603 return p;
2604
2605 cc = *p;
2606 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002607 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (v == NULL && !quiet)
2609 EMSG2(_(e_undefvar), lp->ll_name);
2610 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 if (v == NULL)
2612 return NULL;
2613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 /*
2615 * Loop until no more [idx] or .key is following.
2616 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2621 && !(lp->ll_tv->v_type == VAR_DICT
2622 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E689: Can only index a List or Dictionary"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!quiet)
2631 EMSG(_("E708: [:] must come last"));
2632 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 len = -1;
2636 if (*p == '.')
2637 {
2638 key = p + 1;
2639 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2640 ;
2641 if (len == 0)
2642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_(e_emptykey));
2645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647 p = key + len;
2648 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (*p == ':')
2654 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 else
2656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 empty1 = FALSE;
2658 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002660 if (get_tv_string_chk(&var1) == NULL)
2661 {
2662 /* not a number or string */
2663 clear_tv(&var1);
2664 return NULL;
2665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 }
2667
2668 /* Optionally get the second index [ :expr]. */
2669 if (*p == ':')
2670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (rettv != NULL && (rettv->v_type != VAR_LIST
2680 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 p = skipwhite(p + 1);
2689 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 else
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (!empty1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (get_tv_string_chk(&var2) == NULL)
2701 {
2702 /* not a number or string */
2703 if (!empty1)
2704 clear_tv(&var1);
2705 clear_tv(&var2);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724
2725 /* Skip to past ']'. */
2726 ++p;
2727 }
2728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 {
2731 if (len == -1)
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (*key == NUL)
2736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (!quiet)
2738 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 lp->ll_list = NULL;
2744 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 /* When assigning to a scope dictionary check that a function and
2748 * variable name is valid (only variable name unless it is l: or
2749 * g: dictionary). Disallow overwriting a builtin function. */
2750 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002752 int prevval;
2753 int wrong;
2754
2755 if (len != -1)
2756 {
2757 prevval = key[len];
2758 key[len] = NUL;
2759 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002760 else
2761 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2763 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 || !valid_varname(key);
2766 if (len != -1)
2767 key[len] = prevval;
2768 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 return NULL;
2770 }
2771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 /* Can't add "v:" variable. */
2775 if (lp->ll_dict == &vimvardict)
2776 {
2777 EMSG2(_(e_illvar), name);
2778 return NULL;
2779 }
2780
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (len == -1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 p = NULL;
2798 break;
2799 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* existing variable, need to check if it can be changed */
2801 else if (var_check_ro(lp->ll_di->di_flags, name))
2802 return NULL;
2803
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 else
2809 {
2810 /*
2811 * Get the number and item for the only or first index of the List.
2812 */
2813 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
2816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 clear_tv(&var1);
2819 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_list = lp->ll_tv->vval.v_list;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 if (lp->ll_n1 < 0)
2826 {
2827 lp->ll_n1 = 0;
2828 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2829 }
2830 }
2831 if (lp->ll_li == NULL)
2832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839
2840 /*
2841 * May need to find the item or absolute index for the second
2842 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 * When no index given: "lp->ll_empty2" is TRUE.
2844 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002854 {
2855 if (!quiet)
2856 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2863 if (lp->ll_n1 < 0)
2864 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2865 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 {
2867 if (!quiet)
2868 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 }
2872
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 }
2876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return p;
2878}
2879
2880/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 */
2883 static void
2884clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 vim_free(lp->ll_exp_name);
2888 vim_free(lp->ll_newkey);
2889}
2890
2891/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 */
2896 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903{
2904 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 listitem_T *ri;
2906 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907
2908 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 cc = *endp;
2913 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 if (op != NULL && *op != '=')
2915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002919 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002920 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 {
2922 if (tv_op(&tv, rettv, op) == OK)
2923 set_var(lp->ll_name, &tv, FALSE);
2924 clear_tv(&tv);
2925 }
2926 }
2927 else
2928 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 else if (tv_check_lock(lp->ll_newkey == NULL
2933 ? lp->ll_tv->v_lock
2934 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2935 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 else if (lp->ll_range)
2937 {
2938 /*
2939 * Assign the List values to the list items.
2940 */
2941 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 if (op != NULL && *op != '=')
2944 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2945 else
2946 {
2947 clear_tv(&lp->ll_li->li_tv);
2948 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 ri = ri->li_next;
2951 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2952 break;
2953 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002956 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 ri = NULL;
2959 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 lp->ll_li = lp->ll_li->li_next;
2963 ++lp->ll_n1;
2964 }
2965 if (ri != NULL)
2966 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 else if (lp->ll_empty2
2968 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 : lp->ll_n1 != lp->ll_n2)
2970 EMSG(_("E711: List value has not enough items"));
2971 }
2972 else
2973 {
2974 /*
2975 * Assign to a List or Dictionary item.
2976 */
2977 if (lp->ll_newkey != NULL)
2978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 if (op != NULL && *op != '=')
2980 {
2981 EMSG2(_(e_letwrong), op);
2982 return;
2983 }
2984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002986 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 if (di == NULL)
2988 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2990 {
2991 vim_free(di);
2992 return;
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else if (op != NULL && *op != '=')
2997 {
2998 tv_op(lp->ll_tv, rettv, op);
2999 return;
3000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003003
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 /*
3005 * Assign the value to the variable or list item.
3006 */
3007 if (copy)
3008 copy_tv(rettv, lp->ll_tv);
3009 else
3010 {
3011 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003012 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 }
3015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003016}
3017
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3020 * Returns OK or FAIL.
3021 */
3022 static int
3023tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 typval_T *tv1;
3025 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 char_u *op;
3027{
3028 long n;
3029 char_u numbuf[NUMBUFLEN];
3030 char_u *s;
3031
3032 /* Can't do anything with a Funcref or a Dict on the right. */
3033 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3034 {
3035 switch (tv1->v_type)
3036 {
3037 case VAR_DICT:
3038 case VAR_FUNC:
3039 break;
3040
3041 case VAR_LIST:
3042 if (*op != '+' || tv2->v_type != VAR_LIST)
3043 break;
3044 /* List += List */
3045 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3046 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3047 return OK;
3048
3049 case VAR_NUMBER:
3050 case VAR_STRING:
3051 if (tv2->v_type == VAR_LIST)
3052 break;
3053 if (*op == '+' || *op == '-')
3054 {
3055 /* nr += nr or nr -= nr*/
3056 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057#ifdef FEAT_FLOAT
3058 if (tv2->v_type == VAR_FLOAT)
3059 {
3060 float_T f = n;
3061
3062 if (*op == '+')
3063 f += tv2->vval.v_float;
3064 else
3065 f -= tv2->vval.v_float;
3066 clear_tv(tv1);
3067 tv1->v_type = VAR_FLOAT;
3068 tv1->vval.v_float = f;
3069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071#endif
3072 {
3073 if (*op == '+')
3074 n += get_tv_number(tv2);
3075 else
3076 n -= get_tv_number(tv2);
3077 clear_tv(tv1);
3078 tv1->v_type = VAR_NUMBER;
3079 tv1->vval.v_number = n;
3080 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 }
3082 else
3083 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084 if (tv2->v_type == VAR_FLOAT)
3085 break;
3086
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 /* str .= str */
3088 s = get_tv_string(tv1);
3089 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3090 clear_tv(tv1);
3091 tv1->v_type = VAR_STRING;
3092 tv1->vval.v_string = s;
3093 }
3094 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095
3096#ifdef FEAT_FLOAT
3097 case VAR_FLOAT:
3098 {
3099 float_T f;
3100
3101 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3102 && tv2->v_type != VAR_NUMBER
3103 && tv2->v_type != VAR_STRING))
3104 break;
3105 if (tv2->v_type == VAR_FLOAT)
3106 f = tv2->vval.v_float;
3107 else
3108 f = get_tv_number(tv2);
3109 if (*op == '+')
3110 tv1->vval.v_float += f;
3111 else
3112 tv1->vval.v_float -= f;
3113 }
3114 return OK;
3115#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 }
3117 }
3118
3119 EMSG2(_(e_letwrong), op);
3120 return FAIL;
3121}
3122
3123/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 * Add a watcher to a list.
3125 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003126 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 list_T *l;
3129 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130{
3131 lw->lw_next = l->lv_watch;
3132 l->lv_watch = lw;
3133}
3134
3135/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003136 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 * No warning when it isn't found...
3138 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003139 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 list_T *l;
3142 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143{
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145
3146 lwp = &l->lv_watch;
3147 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3148 {
3149 if (lw == lwrem)
3150 {
3151 *lwp = lw->lw_next;
3152 break;
3153 }
3154 lwp = &lw->lw_next;
3155 }
3156}
3157
3158/*
3159 * Just before removing an item from a list: advance watchers to the next
3160 * item.
3161 */
3162 static void
3163list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 list_T *l;
3165 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166{
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3170 if (lw->lw_item == item)
3171 lw->lw_item = item->li_next;
3172}
3173
3174/*
3175 * Evaluate the expression used in a ":for var in expr" command.
3176 * "arg" points to "var".
3177 * Set "*errp" to TRUE for an error, FALSE otherwise;
3178 * Return a pointer that holds the info. Null when there is an error.
3179 */
3180 void *
3181eval_for_line(arg, errp, nextcmdp, skip)
3182 char_u *arg;
3183 int *errp;
3184 char_u **nextcmdp;
3185 int skip;
3186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 typval_T tv;
3190 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 *errp = TRUE; /* default: there is an error */
3193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 if (fi == NULL)
3196 return NULL;
3197
3198 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3199 if (expr == NULL)
3200 return fi;
3201
3202 expr = skipwhite(expr);
3203 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003205 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 return fi;
3207 }
3208
3209 if (skip)
3210 ++emsg_skip;
3211 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3212 {
3213 *errp = FALSE;
3214 if (!skip)
3215 {
3216 l = tv.vval.v_list;
3217 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003220 clear_tv(&tv);
3221 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 else
3223 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003224 /* No need to increment the refcount, it's already set for the
3225 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 fi->fi_list = l;
3227 list_add_watch(l, &fi->fi_lw);
3228 fi->fi_lw.lw_item = l->lv_first;
3229 }
3230 }
3231 }
3232 if (skip)
3233 --emsg_skip;
3234
3235 return fi;
3236}
3237
3238/*
3239 * Use the first item in a ":for" list. Advance to the next.
3240 * Assign the values to the variable (list). "arg" points to the first one.
3241 * Return TRUE when a valid item was found, FALSE when at end of list or
3242 * something wrong.
3243 */
3244 int
3245next_for_item(fi_void, arg)
3246 void *fi_void;
3247 char_u *arg;
3248{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 item = fi->fi_lw.lw_item;
3254 if (item == NULL)
3255 result = FALSE;
3256 else
3257 {
3258 fi->fi_lw.lw_item = item->li_next;
3259 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3260 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3261 }
3262 return result;
3263}
3264
3265/*
3266 * Free the structure used to store info used by ":for".
3267 */
3268 void
3269free_for_info(fi_void)
3270 void *fi_void;
3271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003274 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 list_unref(fi->fi_list);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 vim_free(fi);
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3283
3284 void
3285set_context_for_expression(xp, arg, cmdidx)
3286 expand_T *xp;
3287 char_u *arg;
3288 cmdidx_T cmdidx;
3289{
3290 int got_eq = FALSE;
3291 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 if (cmdidx == CMD_let)
3295 {
3296 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003297 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 {
3299 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 if (vim_iswhite(*p))
3305 break;
3306 }
3307 return;
3308 }
3309 }
3310 else
3311 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3312 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 while ((xp->xp_pattern = vim_strpbrk(arg,
3314 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3315 {
3316 c = *xp->xp_pattern;
3317 if (c == '&')
3318 {
3319 c = xp->xp_pattern[1];
3320 if (c == '&')
3321 {
3322 ++xp->xp_pattern;
3323 xp->xp_context = cmdidx != CMD_let || got_eq
3324 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3325 }
3326 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003329 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3330 xp->xp_pattern += 2;
3331
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else if (c == '$')
3335 {
3336 /* environment variable */
3337 xp->xp_context = EXPAND_ENV_VARS;
3338 }
3339 else if (c == '=')
3340 {
3341 got_eq = TRUE;
3342 xp->xp_context = EXPAND_EXPRESSION;
3343 }
3344 else if (c == '<'
3345 && xp->xp_context == EXPAND_FUNCTIONS
3346 && vim_strchr(xp->xp_pattern, '(') == NULL)
3347 {
3348 /* Function name can start with "<SNR>" */
3349 break;
3350 }
3351 else if (cmdidx != CMD_let || got_eq)
3352 {
3353 if (c == '"') /* string */
3354 {
3355 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3356 if (c == '\\' && xp->xp_pattern[1] != NUL)
3357 ++xp->xp_pattern;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '\'') /* literal string */
3361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3364 /* skip */ ;
3365 xp->xp_context = EXPAND_NOTHING;
3366 }
3367 else if (c == '|')
3368 {
3369 if (xp->xp_pattern[1] == '|')
3370 {
3371 ++xp->xp_pattern;
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
3375 xp->xp_context = EXPAND_COMMANDS;
3376 }
3377 else
3378 xp->xp_context = EXPAND_EXPRESSION;
3379 }
3380 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 /* Doesn't look like something valid, expand as an expression
3382 * anyway. */
3383 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 arg = xp->xp_pattern;
3385 if (*arg != NUL)
3386 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3387 /* skip */ ;
3388 }
3389 xp->xp_pattern = arg;
3390}
3391
3392#endif /* FEAT_CMDL_COMPL */
3393
3394/*
3395 * ":1,25call func(arg1, arg2)" function call.
3396 */
3397 void
3398ex_call(eap)
3399 exarg_T *eap;
3400{
3401 char_u *arg = eap->arg;
3402 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003404 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 linenr_T lnum;
3408 int doesrange;
3409 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003412 if (eap->skip)
3413 {
3414 /* trans_function_name() doesn't work well when skipping, use eval0()
3415 * instead to skip to any following command, e.g. for:
3416 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003417 ++emsg_skip;
3418 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3419 clear_tv(&rettv);
3420 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003421 return;
3422 }
3423
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003425 if (fudi.fd_newkey != NULL)
3426 {
3427 /* Still need to give an error message for missing key. */
3428 EMSG2(_(e_dictkey), fudi.fd_newkey);
3429 vim_free(fudi.fd_newkey);
3430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 if (tofree == NULL)
3432 return;
3433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 /* Increase refcount on dictionary, it could get deleted when evaluating
3435 * the arguments. */
3436 if (fudi.fd_dict != NULL)
3437 ++fudi.fd_dict->dv_refcount;
3438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003440 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003441 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar532c7802005-01-27 14:44:31 +00003443 /* Skip white space to allow ":call func ()". Not good, but required for
3444 * backward compatibility. */
3445 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (*startarg != '(')
3449 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003450 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 goto end;
3452 }
3453
3454 /*
3455 * When skipping, evaluate the function once, to find the end of the
3456 * arguments.
3457 * When the function takes a range, this is discovered after the first
3458 * call, and the loop is broken.
3459 */
3460 if (eap->skip)
3461 {
3462 ++emsg_skip;
3463 lnum = eap->line2; /* do it once, also with an invalid range */
3464 }
3465 else
3466 lnum = eap->line1;
3467 for ( ; lnum <= eap->line2; ++lnum)
3468 {
3469 if (!eap->skip && eap->addr_count > 0)
3470 {
3471 curwin->w_cursor.lnum = lnum;
3472 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003473#ifdef FEAT_VIRTUALEDIT
3474 curwin->w_cursor.coladd = 0;
3475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 eap->line1, eap->line2, &doesrange,
3480 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 failed = TRUE;
3483 break;
3484 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003485
3486 /* Handle a function returning a Funcref, Dictionary or List. */
3487 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3488 {
3489 failed = TRUE;
3490 break;
3491 }
3492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (doesrange || eap->skip)
3495 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003498 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003500 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (aborting())
3502 break;
3503 }
3504 if (eap->skip)
3505 --emsg_skip;
3506
3507 if (!failed)
3508 {
3509 /* Check for trailing illegal characters and a following command. */
3510 if (!ends_excmd(*arg))
3511 {
3512 emsg_severe = TRUE;
3513 EMSG(_(e_trailing));
3514 }
3515 else
3516 eap->nextcmd = check_nextcmd(arg);
3517 }
3518
3519end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003521 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524/*
3525 * ":unlet[!] var1 ... " command.
3526 */
3527 void
3528ex_unlet(eap)
3529 exarg_T *eap;
3530{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 ex_unletlock(eap, eap->arg, 0);
3532}
3533
3534/*
3535 * ":lockvar" and ":unlockvar" commands
3536 */
3537 void
3538ex_lockvar(eap)
3539 exarg_T *eap;
3540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 int deep = 2;
3543
3544 if (eap->forceit)
3545 deep = -1;
3546 else if (vim_isdigit(*arg))
3547 {
3548 deep = getdigits(&arg);
3549 arg = skipwhite(arg);
3550 }
3551
3552 ex_unletlock(eap, arg, deep);
3553}
3554
3555/*
3556 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3557 */
3558 static void
3559ex_unletlock(eap, argstart, deep)
3560 exarg_T *eap;
3561 char_u *argstart;
3562 int deep;
3563{
3564 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 do
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003572 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003573 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 if (lv.ll_name == NULL)
3575 error = TRUE; /* error but continue parsing */
3576 if (name_end == NULL || (!vim_iswhite(*name_end)
3577 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 if (name_end != NULL)
3580 {
3581 emsg_severe = TRUE;
3582 EMSG(_(e_trailing));
3583 }
3584 if (!(eap->skip || error))
3585 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587 }
3588
3589 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 {
3591 if (eap->cmdidx == CMD_unlet)
3592 {
3593 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3594 error = TRUE;
3595 }
3596 else
3597 {
3598 if (do_lock_var(&lv, name_end, deep,
3599 eap->cmdidx == CMD_lockvar) == FAIL)
3600 error = TRUE;
3601 }
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 if (!eap->skip)
3605 clear_lval(&lv);
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 arg = skipwhite(name_end);
3608 } while (!ends_excmd(*arg));
3609
3610 eap->nextcmd = check_nextcmd(arg);
3611}
3612
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 int forceit;
3618{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 int ret = OK;
3620 int cc;
3621
3622 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 cc = *name_end;
3625 *name_end = NUL;
3626
3627 /* Normal name or expanded name. */
3628 if (check_changedtick(lp->ll_name))
3629 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3635 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else if (lp->ll_range)
3637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639
3640 /* Delete a range of List items. */
3641 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3642 {
3643 li = lp->ll_li->li_next;
3644 listitem_remove(lp->ll_list, lp->ll_li);
3645 lp->ll_li = li;
3646 ++lp->ll_n1;
3647 }
3648 }
3649 else
3650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 /* unlet a List item. */
3653 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003656 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 }
3658
3659 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660}
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 hashtab_T *ht;
3672 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003674 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 ht = find_var_ht(name, &varname);
3677 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hi = hash_find(ht, varname);
3680 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003682 di = HI2DI(hi);
3683 if (var_check_fixed(di->di_flags, name)
3684 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
3686 delete_var(ht, hi);
3687 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690 if (forceit)
3691 return OK;
3692 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694}
3695
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696/*
3697 * Lock or unlock variable indicated by "lp".
3698 * "deep" is the levels to go (-1 for unlimited);
3699 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3700 */
3701 static int
3702do_lock_var(lp, name_end, deep, lock)
3703 lval_T *lp;
3704 char_u *name_end;
3705 int deep;
3706 int lock;
3707{
3708 int ret = OK;
3709 int cc;
3710 dictitem_T *di;
3711
3712 if (deep == 0) /* nothing to do */
3713 return OK;
3714
3715 if (lp->ll_tv == NULL)
3716 {
3717 cc = *name_end;
3718 *name_end = NUL;
3719
3720 /* Normal name or expanded name. */
3721 if (check_changedtick(lp->ll_name))
3722 ret = FAIL;
3723 else
3724 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003725 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 if (di == NULL)
3727 ret = FAIL;
3728 else
3729 {
3730 if (lock)
3731 di->di_flags |= DI_FLAGS_LOCK;
3732 else
3733 di->di_flags &= ~DI_FLAGS_LOCK;
3734 item_lock(&di->di_tv, deep, lock);
3735 }
3736 }
3737 *name_end = cc;
3738 }
3739 else if (lp->ll_range)
3740 {
3741 listitem_T *li = lp->ll_li;
3742
3743 /* (un)lock a range of List items. */
3744 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3745 {
3746 item_lock(&li->li_tv, deep, lock);
3747 li = li->li_next;
3748 ++lp->ll_n1;
3749 }
3750 }
3751 else if (lp->ll_list != NULL)
3752 /* (un)lock a List item. */
3753 item_lock(&lp->ll_li->li_tv, deep, lock);
3754 else
3755 /* un(lock) a Dictionary item. */
3756 item_lock(&lp->ll_di->di_tv, deep, lock);
3757
3758 return ret;
3759}
3760
3761/*
3762 * Lock or unlock an item. "deep" is nr of levels to go.
3763 */
3764 static void
3765item_lock(tv, deep, lock)
3766 typval_T *tv;
3767 int deep;
3768 int lock;
3769{
3770 static int recurse = 0;
3771 list_T *l;
3772 listitem_T *li;
3773 dict_T *d;
3774 hashitem_T *hi;
3775 int todo;
3776
3777 if (recurse >= DICT_MAXNEST)
3778 {
3779 EMSG(_("E743: variable nested too deep for (un)lock"));
3780 return;
3781 }
3782 if (deep == 0)
3783 return;
3784 ++recurse;
3785
3786 /* lock/unlock the item itself */
3787 if (lock)
3788 tv->v_lock |= VAR_LOCKED;
3789 else
3790 tv->v_lock &= ~VAR_LOCKED;
3791
3792 switch (tv->v_type)
3793 {
3794 case VAR_LIST:
3795 if ((l = tv->vval.v_list) != NULL)
3796 {
3797 if (lock)
3798 l->lv_lock |= VAR_LOCKED;
3799 else
3800 l->lv_lock &= ~VAR_LOCKED;
3801 if (deep < 0 || deep > 1)
3802 /* recursive: lock/unlock the items the List contains */
3803 for (li = l->lv_first; li != NULL; li = li->li_next)
3804 item_lock(&li->li_tv, deep - 1, lock);
3805 }
3806 break;
3807 case VAR_DICT:
3808 if ((d = tv->vval.v_dict) != NULL)
3809 {
3810 if (lock)
3811 d->dv_lock |= VAR_LOCKED;
3812 else
3813 d->dv_lock &= ~VAR_LOCKED;
3814 if (deep < 0 || deep > 1)
3815 {
3816 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003817 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3819 {
3820 if (!HASHITEM_EMPTY(hi))
3821 {
3822 --todo;
3823 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3824 }
3825 }
3826 }
3827 }
3828 }
3829 --recurse;
3830}
3831
Bram Moolenaara40058a2005-07-11 22:42:07 +00003832/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003833 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3834 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835 */
3836 static int
3837tv_islocked(tv)
3838 typval_T *tv;
3839{
3840 return (tv->v_lock & VAR_LOCKED)
3841 || (tv->v_type == VAR_LIST
3842 && tv->vval.v_list != NULL
3843 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3844 || (tv->v_type == VAR_DICT
3845 && tv->vval.v_dict != NULL
3846 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3847}
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3850/*
3851 * Delete all "menutrans_" variables.
3852 */
3853 void
3854del_menutrans_vars()
3855{
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003860 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 {
3863 if (!HASHITEM_EMPTY(hi))
3864 {
3865 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3867 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 }
3869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871}
3872#endif
3873
3874#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3875
3876/*
3877 * Local string buffer for the next two functions to store a variable name
3878 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3879 * get_user_var_name().
3880 */
3881
3882static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3883
3884static char_u *varnamebuf = NULL;
3885static int varnamebuflen = 0;
3886
3887/*
3888 * Function to concatenate a prefix and a variable name.
3889 */
3890 static char_u *
3891cat_prefix_varname(prefix, name)
3892 int prefix;
3893 char_u *name;
3894{
3895 int len;
3896
3897 len = (int)STRLEN(name) + 3;
3898 if (len > varnamebuflen)
3899 {
3900 vim_free(varnamebuf);
3901 len += 10; /* some additional space */
3902 varnamebuf = alloc(len);
3903 if (varnamebuf == NULL)
3904 {
3905 varnamebuflen = 0;
3906 return NULL;
3907 }
3908 varnamebuflen = len;
3909 }
3910 *varnamebuf = prefix;
3911 varnamebuf[1] = ':';
3912 STRCPY(varnamebuf + 2, name);
3913 return varnamebuf;
3914}
3915
3916/*
3917 * Function given to ExpandGeneric() to obtain the list of user defined
3918 * (global/buffer/window/built-in) variable names.
3919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u *
3921get_user_var_name(xp, idx)
3922 expand_T *xp;
3923 int idx;
3924{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static long_u gdone;
3926 static long_u bdone;
3927 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928#ifdef FEAT_WINDOWS
3929 static long_u tdone;
3930#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 static int vidx;
3932 static hashitem_T *hi;
3933 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003936 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003938#ifdef FEAT_WINDOWS
3939 tdone = 0;
3940#endif
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* Global variables */
3944 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3953 return cat_prefix_varname('g', hi->hi_key);
3954 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003971 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return (char_u *)"b:changedtick";
3973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974
3975 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003976 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003979 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003981 else
3982 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 while (HASHITEM_EMPTY(hi))
3984 ++hi;
3985 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988#ifdef FEAT_WINDOWS
3989 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003990 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991 if (tdone < ht->ht_used)
3992 {
3993 if (tdone++ == 0)
3994 hi = ht->ht_array;
3995 else
3996 ++hi;
3997 while (HASHITEM_EMPTY(hi))
3998 ++hi;
3999 return cat_prefix_varname('t', hi->hi_key);
4000 }
4001#endif
4002
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 /* v: variables */
4004 if (vidx < VV_LEN)
4005 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 vim_free(varnamebuf);
4008 varnamebuf = NULL;
4009 varnamebuflen = 0;
4010 return NULL;
4011}
4012
4013#endif /* FEAT_CMDL_COMPL */
4014
4015/*
4016 * types for expressions.
4017 */
4018typedef enum
4019{
4020 TYPE_UNKNOWN = 0
4021 , TYPE_EQUAL /* == */
4022 , TYPE_NEQUAL /* != */
4023 , TYPE_GREATER /* > */
4024 , TYPE_GEQUAL /* >= */
4025 , TYPE_SMALLER /* < */
4026 , TYPE_SEQUAL /* <= */
4027 , TYPE_MATCH /* =~ */
4028 , TYPE_NOMATCH /* !~ */
4029} exptype_T;
4030
4031/*
4032 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4035 */
4036
4037/*
4038 * Handle zero level expression.
4039 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004041 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * Return OK or FAIL.
4043 */
4044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **nextcmd;
4049 int evaluate;
4050{
4051 int ret;
4052 char_u *p;
4053
4054 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (ret == FAIL || !ends_excmd(*p))
4057 {
4058 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
4061 * Report the invalid expression unless the expression evaluation has
4062 * been cancelled due to an aborting error, an interrupt, or an
4063 * exception.
4064 */
4065 if (!aborting())
4066 EMSG2(_(e_invexpr2), arg);
4067 ret = FAIL;
4068 }
4069 if (nextcmd != NULL)
4070 *nextcmd = check_nextcmd(p);
4071
4072 return ret;
4073}
4074
4075/*
4076 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004077 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 *
4079 * "arg" must point to the first non-white of the expression.
4080 * "arg" is advanced to the next non-white after the recognized expression.
4081 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
4083 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * Return OK or FAIL.
4085 */
4086 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 int evaluate;
4091{
4092 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 /*
4096 * Get the first variable.
4097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 if ((*arg)[0] == '?')
4102 {
4103 result = FALSE;
4104 if (evaluate)
4105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int error = FALSE;
4107
4108 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004111 if (error)
4112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /*
4116 * Get the second variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121
4122 /*
4123 * Check for the ":".
4124 */
4125 if ((*arg)[0] != ':')
4126 {
4127 EMSG(_("E109: Missing ':' after '?'"));
4128 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131 }
4132
4133 /*
4134 * Get the third variable.
4135 */
4136 *arg = skipwhite(*arg + 1);
4137 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4138 {
4139 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142 }
4143 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Handle first level expression:
4152 * expr2 || expr2 || expr2 logical OR
4153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
4157 * Return OK or FAIL.
4158 */
4159 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int evaluate;
4164{
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 long result;
4167 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 /*
4177 * Repeat until there is no following "||".
4178 */
4179 first = TRUE;
4180 result = FALSE;
4181 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4182 {
4183 if (evaluate && first)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 first = FALSE;
4191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 2);
4197 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4198 return FAIL;
4199
4200 /*
4201 * Compute the result.
4202 */
4203 if (evaluate && !result)
4204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (error)
4209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 if (evaluate)
4212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 rettv->v_type = VAR_NUMBER;
4214 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle second level expression:
4223 * expr3 && expr3 && expr3 logical AND
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 long result;
4238 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * Get the first variable.
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
4246
4247 /*
4248 * Repeat until there is no following "&&".
4249 */
4250 first = TRUE;
4251 result = TRUE;
4252 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4253 {
4254 if (evaluate && first)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 first = FALSE;
4262 }
4263
4264 /*
4265 * Get the second variable.
4266 */
4267 *arg = skipwhite(*arg + 2);
4268 if (eval4(arg, &var2, evaluate && result) == FAIL)
4269 return FAIL;
4270
4271 /*
4272 * Compute the result.
4273 */
4274 if (evaluate && result)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (evaluate)
4283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle third level expression:
4294 * var1 == var2
4295 * var1 =~ var2
4296 * var1 != var2
4297 * var1 !~ var2
4298 * var1 > var2
4299 * var1 >= var2
4300 * var1 < var2
4301 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 * var1 is var2
4303 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 *
4305 * "arg" must point to the first non-white of the expression.
4306 * "arg" is advanced to the next non-white after the recognized expression.
4307 *
4308 * Return OK or FAIL.
4309 */
4310 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int evaluate;
4315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 char_u *p;
4318 int i;
4319 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004320 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int len = 2;
4322 long n1, n2;
4323 char_u *s1, *s2;
4324 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4325 regmatch_T regmatch;
4326 int ic;
4327 char_u *save_cpo;
4328
4329 /*
4330 * Get the first variable.
4331 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return FAIL;
4334
4335 p = *arg;
4336 switch (p[0])
4337 {
4338 case '=': if (p[1] == '=')
4339 type = TYPE_EQUAL;
4340 else if (p[1] == '~')
4341 type = TYPE_MATCH;
4342 break;
4343 case '!': if (p[1] == '=')
4344 type = TYPE_NEQUAL;
4345 else if (p[1] == '~')
4346 type = TYPE_NOMATCH;
4347 break;
4348 case '>': if (p[1] != '=')
4349 {
4350 type = TYPE_GREATER;
4351 len = 1;
4352 }
4353 else
4354 type = TYPE_GEQUAL;
4355 break;
4356 case '<': if (p[1] != '=')
4357 {
4358 type = TYPE_SMALLER;
4359 len = 1;
4360 }
4361 else
4362 type = TYPE_SEQUAL;
4363 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364 case 'i': if (p[1] == 's')
4365 {
4366 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4367 len = 5;
4368 if (!vim_isIDc(p[len]))
4369 {
4370 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4371 type_is = TRUE;
4372 }
4373 }
4374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376
4377 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 */
4380 if (type != TYPE_UNKNOWN)
4381 {
4382 /* extra question mark appended: ignore case */
4383 if (p[len] == '?')
4384 {
4385 ic = TRUE;
4386 ++len;
4387 }
4388 /* extra '#' appended: match case */
4389 else if (p[len] == '#')
4390 {
4391 ic = FALSE;
4392 ++len;
4393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 else
4396 ic = p_ic;
4397
4398 /*
4399 * Get the second variable.
4400 */
4401 *arg = skipwhite(p + len);
4402 if (eval5(arg, &var2, evaluate) == FAIL)
4403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
4406 }
4407
4408 if (evaluate)
4409 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004410 if (type_is && rettv->v_type != var2.v_type)
4411 {
4412 /* For "is" a different type always means FALSE, for "notis"
4413 * it means TRUE. */
4414 n1 = (type == TYPE_NEQUAL);
4415 }
4416 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4417 {
4418 if (type_is)
4419 {
4420 n1 = (rettv->v_type == var2.v_type
4421 && rettv->vval.v_list == var2.vval.v_list);
4422 if (type == TYPE_NEQUAL)
4423 n1 = !n1;
4424 }
4425 else if (rettv->v_type != var2.v_type
4426 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4427 {
4428 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004429 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004431 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 clear_tv(rettv);
4433 clear_tv(&var2);
4434 return FAIL;
4435 }
4436 else
4437 {
4438 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004439 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4440 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 if (type == TYPE_NEQUAL)
4442 n1 = !n1;
4443 }
4444 }
4445
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004446 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_dict == var2.vval.v_dict);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
4459 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4460 else
4461 EMSG(_("E736: Invalid operation for Dictionary"));
4462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4470 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4477 {
4478 if (rettv->v_type != var2.v_type
4479 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4480 {
4481 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004482 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004484 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 clear_tv(rettv);
4486 clear_tv(&var2);
4487 return FAIL;
4488 }
4489 else
4490 {
4491 /* Compare two Funcrefs for being equal or unequal. */
4492 if (rettv->vval.v_string == NULL
4493 || var2.vval.v_string == NULL)
4494 n1 = FALSE;
4495 else
4496 n1 = STRCMP(rettv->vval.v_string,
4497 var2.vval.v_string) == 0;
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 }
4502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004503#ifdef FEAT_FLOAT
4504 /*
4505 * If one of the two variables is a float, compare as a float.
4506 * When using "=~" or "!~", always compare as string.
4507 */
4508 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4509 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 {
4511 float_T f1, f2;
4512
4513 if (rettv->v_type == VAR_FLOAT)
4514 f1 = rettv->vval.v_float;
4515 else
4516 f1 = get_tv_number(rettv);
4517 if (var2.v_type == VAR_FLOAT)
4518 f2 = var2.vval.v_float;
4519 else
4520 f2 = get_tv_number(&var2);
4521 n1 = FALSE;
4522 switch (type)
4523 {
4524 case TYPE_EQUAL: n1 = (f1 == f2); break;
4525 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4526 case TYPE_GREATER: n1 = (f1 > f2); break;
4527 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4528 case TYPE_SMALLER: n1 = (f1 < f2); break;
4529 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4530 case TYPE_UNKNOWN:
4531 case TYPE_MATCH:
4532 case TYPE_NOMATCH: break; /* avoid gcc warning */
4533 }
4534 }
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /*
4538 * If one of the two variables is a number, compare as a number.
4539 * When using "=~" or "!~", always compare as string.
4540 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 n1 = get_tv_number(rettv);
4545 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 switch (type)
4547 {
4548 case TYPE_EQUAL: n1 = (n1 == n2); break;
4549 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4550 case TYPE_GREATER: n1 = (n1 > n2); break;
4551 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4552 case TYPE_SMALLER: n1 = (n1 < n2); break;
4553 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4554 case TYPE_UNKNOWN:
4555 case TYPE_MATCH:
4556 case TYPE_NOMATCH: break; /* avoid gcc warning */
4557 }
4558 }
4559 else
4560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 s1 = get_tv_string_buf(rettv, buf1);
4562 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4564 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4565 else
4566 i = 0;
4567 n1 = FALSE;
4568 switch (type)
4569 {
4570 case TYPE_EQUAL: n1 = (i == 0); break;
4571 case TYPE_NEQUAL: n1 = (i != 0); break;
4572 case TYPE_GREATER: n1 = (i > 0); break;
4573 case TYPE_GEQUAL: n1 = (i >= 0); break;
4574 case TYPE_SMALLER: n1 = (i < 0); break;
4575 case TYPE_SEQUAL: n1 = (i <= 0); break;
4576
4577 case TYPE_MATCH:
4578 case TYPE_NOMATCH:
4579 /* avoid 'l' flag in 'cpoptions' */
4580 save_cpo = p_cpo;
4581 p_cpo = (char_u *)"";
4582 regmatch.regprog = vim_regcomp(s2,
4583 RE_MAGIC + RE_STRING);
4584 regmatch.rm_ic = ic;
4585 if (regmatch.regprog != NULL)
4586 {
4587 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004588 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (type == TYPE_NOMATCH)
4590 n1 = !n1;
4591 }
4592 p_cpo = save_cpo;
4593 break;
4594
4595 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4596 }
4597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 clear_tv(rettv);
4599 clear_tv(&var2);
4600 rettv->v_type = VAR_NUMBER;
4601 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604
4605 return OK;
4606}
4607
4608/*
4609 * Handle fourth level expression:
4610 * + number addition
4611 * - number subtraction
4612 * . string concatenation
4613 *
4614 * "arg" must point to the first non-white of the expression.
4615 * "arg" is advanced to the next non-white after the recognized expression.
4616 *
4617 * Return OK or FAIL.
4618 */
4619 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 int evaluate;
4624{
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T var2;
4626 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int op;
4628 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629#ifdef FEAT_FLOAT
4630 float_T f1 = 0, f2 = 0;
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *s1, *s2;
4633 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4634 char_u *p;
4635
4636 /*
4637 * Get the first variable.
4638 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004639 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 return FAIL;
4641
4642 /*
4643 * Repeat computing, until no '+', '-' or '.' is following.
4644 */
4645 for (;;)
4646 {
4647 op = **arg;
4648 if (op != '+' && op != '-' && op != '.')
4649 break;
4650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004651 if ((op != '+' || rettv->v_type != VAR_LIST)
4652#ifdef FEAT_FLOAT
4653 && (op == '.' || rettv->v_type != VAR_FLOAT)
4654#endif
4655 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 {
4657 /* For "list + ...", an illegal use of the first operand as
4658 * a number cannot be determined before evaluating the 2nd
4659 * operand: if this is also a list, all is ok.
4660 * For "something . ...", "something - ..." or "non-list + ...",
4661 * we know that the first operand needs to be a string or number
4662 * without evaluating the 2nd operand. So check before to avoid
4663 * side effects after an error. */
4664 if (evaluate && get_tv_string_chk(rettv) == NULL)
4665 {
4666 clear_tv(rettv);
4667 return FAIL;
4668 }
4669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 /*
4672 * Get the second variable.
4673 */
4674 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004675 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679 }
4680
4681 if (evaluate)
4682 {
4683 /*
4684 * Compute the result.
4685 */
4686 if (op == '.')
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4689 s2 = get_tv_string_buf_chk(&var2, buf2);
4690 if (s2 == NULL) /* type error ? */
4691 {
4692 clear_tv(rettv);
4693 clear_tv(&var2);
4694 return FAIL;
4695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004696 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 clear_tv(rettv);
4698 rettv->v_type = VAR_STRING;
4699 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 else if (op == '+' && rettv->v_type == VAR_LIST
4702 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004703 {
4704 /* concatenate Lists */
4705 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4706 &var3) == FAIL)
4707 {
4708 clear_tv(rettv);
4709 clear_tv(&var2);
4710 return FAIL;
4711 }
4712 clear_tv(rettv);
4713 *rettv = var3;
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 else
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int error = FALSE;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719#ifdef FEAT_FLOAT
4720 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 f1 = rettv->vval.v_float;
4723 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 else
4726#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 n1 = get_tv_number_chk(rettv, &error);
4729 if (error)
4730 {
4731 /* This can only happen for "list + non-list". For
4732 * "non-list + ..." or "something - ...", we returned
4733 * before evaluating the 2nd operand. */
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (var2.v_type == VAR_FLOAT)
4739 f1 = n1;
4740#endif
4741 }
4742#ifdef FEAT_FLOAT
4743 if (var2.v_type == VAR_FLOAT)
4744 {
4745 f2 = var2.vval.v_float;
4746 n2 = 0;
4747 }
4748 else
4749#endif
4750 {
4751 n2 = get_tv_number_chk(&var2, &error);
4752 if (error)
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
4758#ifdef FEAT_FLOAT
4759 if (rettv->v_type == VAR_FLOAT)
4760 f2 = n2;
4761#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764
4765#ifdef FEAT_FLOAT
4766 /* If there is a float on either side the result is a float. */
4767 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4768 {
4769 if (op == '+')
4770 f1 = f1 + f2;
4771 else
4772 f1 = f1 - f2;
4773 rettv->v_type = VAR_FLOAT;
4774 rettv->vval.v_float = f1;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#endif
4778 {
4779 if (op == '+')
4780 n1 = n1 + n2;
4781 else
4782 n1 = n1 - n2;
4783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = n1;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 }
4790 return OK;
4791}
4792
4793/*
4794 * Handle fifth level expression:
4795 * * number multiplication
4796 * / number division
4797 * % number modulo
4798 *
4799 * "arg" must point to the first non-white of the expression.
4800 * "arg" is advanced to the next non-white after the recognized expression.
4801 *
4802 * Return OK or FAIL.
4803 */
4804 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004809 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 int op;
4813 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#ifdef FEAT_FLOAT
4815 int use_float = FALSE;
4816 float_T f1 = 0, f2;
4817#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 /*
4821 * Get the first variable.
4822 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004823 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return FAIL;
4825
4826 /*
4827 * Repeat computing, until no '*', '/' or '%' is following.
4828 */
4829 for (;;)
4830 {
4831 op = **arg;
4832 if (op != '*' && op != '/' && op != '%')
4833 break;
4834
4835 if (evaluate)
4836 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
4839 {
4840 f1 = rettv->vval.v_float;
4841 use_float = TRUE;
4842 n1 = 0;
4843 }
4844 else
4845#endif
4846 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 if (error)
4849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 else
4852 n1 = 0;
4853
4854 /*
4855 * Get the second variable.
4856 */
4857 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004858 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return FAIL;
4860
4861 if (evaluate)
4862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#ifdef FEAT_FLOAT
4864 if (var2.v_type == VAR_FLOAT)
4865 {
4866 if (!use_float)
4867 {
4868 f1 = n1;
4869 use_float = TRUE;
4870 }
4871 f2 = var2.vval.v_float;
4872 n2 = 0;
4873 }
4874 else
4875#endif
4876 {
4877 n2 = get_tv_number_chk(&var2, &error);
4878 clear_tv(&var2);
4879 if (error)
4880 return FAIL;
4881#ifdef FEAT_FLOAT
4882 if (use_float)
4883 f2 = n2;
4884#endif
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 if (op == '*')
4895 f1 = f1 * f2;
4896 else if (op == '/')
4897 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# ifdef VMS
4899 /* VMS crashes on divide by zero, work around it */
4900 if (f2 == 0.0)
4901 {
4902 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004903 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004904 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004905 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004906 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004907 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908 }
4909 else
4910 f1 = f1 / f2;
4911# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 /* We rely on the floating point library to handle divide
4913 * by zero to result in "inf" and not a crash. */
4914 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004915# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004919 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 return FAIL;
4921 }
4922 rettv->v_type = VAR_FLOAT;
4923 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 if (op == '*')
4929 n1 = n1 * n2;
4930 else if (op == '/')
4931 {
4932 if (n2 == 0) /* give an error message? */
4933 {
4934 if (n1 == 0)
4935 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4936 else if (n1 < 0)
4937 n1 = -0x7fffffffL;
4938 else
4939 n1 = 0x7fffffffL;
4940 }
4941 else
4942 n1 = n1 / n2;
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
4946 if (n2 == 0) /* give an error message? */
4947 n1 = 0;
4948 else
4949 n1 = n1 % n2;
4950 }
4951 rettv->v_type = VAR_NUMBER;
4952 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 }
4956
4957 return OK;
4958}
4959
4960/*
4961 * Handle sixth level expression:
4962 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004963 * "string" string constant
4964 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * &option-name option value
4966 * @r register contents
4967 * identifier variable value
4968 * function() function call
4969 * $VAR environment variable
4970 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004971 * [expr, expr] List
4972 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * Also handle:
4975 * ! in front logical NOT
4976 * - in front unary minus
4977 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * trailing [] subscript in String or List
4979 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 *
4981 * "arg" must point to the first non-white of the expression.
4982 * "arg" is advanced to the next non-white after the recognized expression.
4983 *
4984 * Return OK or FAIL.
4985 */
4986 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004987eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004991 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 long n;
4994 int len;
4995 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u *start_leader, *end_leader;
4997 int ret = OK;
4998 char_u *alias;
4999
5000 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * Skip '!' and '-' characters. They are handled later.
5008 */
5009 start_leader = *arg;
5010 while (**arg == '!' || **arg == '-' || **arg == '+')
5011 *arg = skipwhite(*arg + 1);
5012 end_leader = *arg;
5013
5014 switch (**arg)
5015 {
5016 /*
5017 * Number constant.
5018 */
5019 case '0':
5020 case '1':
5021 case '2':
5022 case '3':
5023 case '4':
5024 case '5':
5025 case '6':
5026 case '7':
5027 case '8':
5028 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 {
5030#ifdef FEAT_FLOAT
5031 char_u *p = skipdigits(*arg + 1);
5032 int get_float = FALSE;
5033
5034 /* We accept a float when the format matches
5035 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005036 * strict to avoid backwards compatibility problems.
5037 * Don't look for a float after the "." operator, so that
5038 * ":let vers = 1.2.3" doesn't fail. */
5039 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005041 get_float = TRUE;
5042 p = skipdigits(p + 2);
5043 if (*p == 'e' || *p == 'E')
5044 {
5045 ++p;
5046 if (*p == '-' || *p == '+')
5047 ++p;
5048 if (!vim_isdigit(*p))
5049 get_float = FALSE;
5050 else
5051 p = skipdigits(p + 1);
5052 }
5053 if (ASCII_ISALPHA(*p) || *p == '.')
5054 get_float = FALSE;
5055 }
5056 if (get_float)
5057 {
5058 float_T f;
5059
5060 *arg += string2float(*arg, &f);
5061 if (evaluate)
5062 {
5063 rettv->v_type = VAR_FLOAT;
5064 rettv->vval.v_float = f;
5065 }
5066 }
5067 else
5068#endif
5069 {
5070 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5071 *arg += len;
5072 if (evaluate)
5073 {
5074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = n;
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * String constant: "string".
5083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 break;
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 break;
5092
5093 /*
5094 * List: [expr, expr]
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 * Dictionary: {key: val, key: val}
5101 */
5102 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5103 break;
5104
5105 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Environment variable: $VAR.
5113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /*
5118 * Register contents: @r.
5119 */
5120 case '@': ++*arg;
5121 if (evaluate)
5122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005124 rettv->vval.v_string = get_reg_contents(**arg,
5125 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 if (**arg != NUL)
5128 ++*arg;
5129 break;
5130
5131 /*
5132 * nested expression: (expression).
5133 */
5134 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (**arg == ')')
5137 ++*arg;
5138 else if (ret == OK)
5139 {
5140 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 ret = FAIL;
5143 }
5144 break;
5145
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149
5150 if (ret == NOTDONE)
5151 {
5152 /*
5153 * Must be a variable or function name.
5154 * Can also be a curly-braces kind of name: {expr}.
5155 */
5156 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005157 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005158 if (alias != NULL)
5159 s = alias;
5160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 ret = FAIL;
5163 else
5164 {
5165 if (**arg == '(') /* recursive! */
5166 {
5167 /* If "s" is the name of a variable of type VAR_FUNC
5168 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005169 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005170
5171 /* Invoke the function. */
5172 ret = get_func_tv(s, len, rettv, arg,
5173 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175
5176 /* If evaluate is FALSE rettv->v_type was not set in
5177 * get_func_tv, but it's needed in handle_subscript() to parse
5178 * what follows. So set it here. */
5179 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5180 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005181 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005182 rettv->v_type = VAR_FUNC;
5183 }
5184
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 /* Stop the expression evaluation when immediately
5186 * aborting on error, or when an interrupt occurred or
5187 * an exception was thrown but not caught. */
5188 if (aborting())
5189 {
5190 if (ret == OK)
5191 clear_tv(rettv);
5192 ret = FAIL;
5193 }
5194 }
5195 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005196 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005197 else
5198 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005199 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005200 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201 }
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 *arg = skipwhite(*arg);
5204
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005205 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5206 * expr(expr). */
5207 if (ret == OK)
5208 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 /*
5211 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5212 */
5213 if (ret == OK && evaluate && end_leader > start_leader)
5214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 int val = 0;
5217#ifdef FEAT_FLOAT
5218 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 if (rettv->v_type == VAR_FLOAT)
5221 f = rettv->vval.v_float;
5222 else
5223#endif
5224 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 clear_tv(rettv);
5228 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 else
5231 {
5232 while (end_leader > start_leader)
5233 {
5234 --end_leader;
5235 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236 {
5237#ifdef FEAT_FLOAT
5238 if (rettv->v_type == VAR_FLOAT)
5239 f = !f;
5240 else
5241#endif
5242 val = !val;
5243 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005245 {
5246#ifdef FEAT_FLOAT
5247 if (rettv->v_type == VAR_FLOAT)
5248 f = -f;
5249 else
5250#endif
5251 val = -val;
5252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254#ifdef FEAT_FLOAT
5255 if (rettv->v_type == VAR_FLOAT)
5256 {
5257 clear_tv(rettv);
5258 rettv->vval.v_float = f;
5259 }
5260 else
5261#endif
5262 {
5263 clear_tv(rettv);
5264 rettv->v_type = VAR_NUMBER;
5265 rettv->vval.v_number = val;
5266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269
5270 return ret;
5271}
5272
5273/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005274 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5275 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5277 */
5278 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005281 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284{
5285 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005286 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 long len = -1;
5289 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005293 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005295 if (verbose)
5296 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 return FAIL;
5298 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005299#ifdef FEAT_FLOAT
5300 else if (rettv->v_type == VAR_FLOAT)
5301 {
5302 if (verbose)
5303 EMSG(_(e_float_as_string));
5304 return FAIL;
5305 }
5306#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 /*
5311 * dict.name
5312 */
5313 key = *arg + 1;
5314 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5315 ;
5316 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005317 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005319 }
5320 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 /*
5323 * something[idx]
5324 *
5325 * Get the (first) variable from inside the [].
5326 */
5327 *arg = skipwhite(*arg + 1);
5328 if (**arg == ':')
5329 empty1 = TRUE;
5330 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5331 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5333 {
5334 /* not a number or string */
5335 clear_tv(&var1);
5336 return FAIL;
5337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338
5339 /*
5340 * Get the second variable from inside the [:].
5341 */
5342 if (**arg == ':')
5343 {
5344 range = TRUE;
5345 *arg = skipwhite(*arg + 1);
5346 if (**arg == ']')
5347 empty2 = TRUE;
5348 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 if (!empty1)
5351 clear_tv(&var1);
5352 return FAIL;
5353 }
5354 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5355 {
5356 /* not a number or string */
5357 if (!empty1)
5358 clear_tv(&var1);
5359 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 return FAIL;
5361 }
5362 }
5363
5364 /* Check for the ']'. */
5365 if (**arg != ']')
5366 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 if (verbose)
5368 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005369 clear_tv(&var1);
5370 if (range)
5371 clear_tv(&var2);
5372 return FAIL;
5373 }
5374 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 }
5376
5377 if (evaluate)
5378 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 n1 = 0;
5380 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 n1 = get_tv_number(&var1);
5383 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 }
5385 if (range)
5386 {
5387 if (empty2)
5388 n2 = -1;
5389 else
5390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 n2 = get_tv_number(&var2);
5392 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 }
5394 }
5395
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 {
5398 case VAR_NUMBER:
5399 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 len = (long)STRLEN(s);
5402 if (range)
5403 {
5404 /* The resulting variable is a substring. If the indexes
5405 * are out of range the result is empty. */
5406 if (n1 < 0)
5407 {
5408 n1 = len + n1;
5409 if (n1 < 0)
5410 n1 = 0;
5411 }
5412 if (n2 < 0)
5413 n2 = len + n2;
5414 else if (n2 >= len)
5415 n2 = len;
5416 if (n1 >= len || n2 < 0 || n1 > n2)
5417 s = NULL;
5418 else
5419 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5420 }
5421 else
5422 {
5423 /* The resulting variable is a string of a single
5424 * character. If the index is too big or negative the
5425 * result is empty. */
5426 if (n1 >= len || n1 < 0)
5427 s = NULL;
5428 else
5429 s = vim_strnsave(s + n1, 1);
5430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005431 clear_tv(rettv);
5432 rettv->v_type = VAR_STRING;
5433 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 break;
5435
5436 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 if (n1 < 0)
5439 n1 = len + n1;
5440 if (!empty1 && (n1 < 0 || n1 >= len))
5441 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005442 /* For a range we allow invalid values and return an empty
5443 * list. A list index out of range is an error. */
5444 if (!range)
5445 {
5446 if (verbose)
5447 EMSGN(_(e_listidx), n1);
5448 return FAIL;
5449 }
5450 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 }
5452 if (range)
5453 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005454 list_T *l;
5455 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456
5457 if (n2 < 0)
5458 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005459 else if (n2 >= len)
5460 n2 = len - 1;
5461 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005462 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 l = list_alloc();
5464 if (l == NULL)
5465 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 n1 <= n2; ++n1)
5468 {
5469 if (list_append_tv(l, &item->li_tv) == FAIL)
5470 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005471 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 return FAIL;
5473 }
5474 item = item->li_next;
5475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 clear_tv(rettv);
5477 rettv->v_type = VAR_LIST;
5478 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005479 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 else
5482 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005483 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 clear_tv(rettv);
5485 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 }
5487 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488
5489 case VAR_DICT:
5490 if (range)
5491 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005492 if (verbose)
5493 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 if (len == -1)
5495 clear_tv(&var1);
5496 return FAIL;
5497 }
5498 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005499 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500
5501 if (len == -1)
5502 {
5503 key = get_tv_string(&var1);
5504 if (*key == NUL)
5505 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005506 if (verbose)
5507 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508 clear_tv(&var1);
5509 return FAIL;
5510 }
5511 }
5512
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005513 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005514
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005515 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005516 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 if (len == -1)
5518 clear_tv(&var1);
5519 if (item == NULL)
5520 return FAIL;
5521
5522 copy_tv(&item->di_tv, &var1);
5523 clear_tv(rettv);
5524 *rettv = var1;
5525 }
5526 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 }
5528 }
5529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return OK;
5531}
5532
5533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 * Get an option value.
5535 * "arg" points to the '&' or '+' before the option name.
5536 * "arg" is advanced to character after the option name.
5537 * Return OK or FAIL.
5538 */
5539 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005542 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 int evaluate;
5544{
5545 char_u *option_end;
5546 long numval;
5547 char_u *stringval;
5548 int opt_type;
5549 int c;
5550 int working = (**arg == '+'); /* has("+option") */
5551 int ret = OK;
5552 int opt_flags;
5553
5554 /*
5555 * Isolate the option name and find its value.
5556 */
5557 option_end = find_option_end(arg, &opt_flags);
5558 if (option_end == NULL)
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 EMSG2(_("E112: Option name missing: %s"), *arg);
5562 return FAIL;
5563 }
5564
5565 if (!evaluate)
5566 {
5567 *arg = option_end;
5568 return OK;
5569 }
5570
5571 c = *option_end;
5572 *option_end = NUL;
5573 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576 if (opt_type == -3) /* invalid name */
5577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 EMSG2(_("E113: Unknown option: %s"), *arg);
5580 ret = FAIL;
5581 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 if (opt_type == -2) /* hidden string option */
5585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 rettv->v_type = VAR_STRING;
5587 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589 else if (opt_type == -1) /* hidden number option */
5590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 rettv->v_type = VAR_NUMBER;
5592 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594 else if (opt_type == 1) /* number option */
5595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 rettv->v_type = VAR_NUMBER;
5597 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else /* string option */
5600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 rettv->v_type = VAR_STRING;
5602 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604 }
5605 else if (working && (opt_type == -2 || opt_type == -1))
5606 ret = FAIL;
5607
5608 *option_end = c; /* put back for error messages */
5609 *arg = option_end;
5610
5611 return ret;
5612}
5613
5614/*
5615 * Allocate a variable for a string constant.
5616 * Return OK or FAIL.
5617 */
5618 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 int evaluate;
5623{
5624 char_u *p;
5625 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 int extra = 0;
5627
5628 /*
5629 * Find the end of the string, skipping backslashed characters.
5630 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005631 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
5633 if (*p == '\\' && p[1] != NUL)
5634 {
5635 ++p;
5636 /* A "\<x>" form occupies at least 4 characters, and produces up
5637 * to 6 characters: reserve space for 2 extra */
5638 if (*p == '<')
5639 extra += 2;
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642
5643 if (*p != '"')
5644 {
5645 EMSG2(_("E114: Missing quote: %s"), *arg);
5646 return FAIL;
5647 }
5648
5649 /* If only parsing, set *arg and return here */
5650 if (!evaluate)
5651 {
5652 *arg = p + 1;
5653 return OK;
5654 }
5655
5656 /*
5657 * Copy the string into allocated memory, handling backslashed
5658 * characters.
5659 */
5660 name = alloc((unsigned)(p - *arg + extra));
5661 if (name == NULL)
5662 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 rettv->v_type = VAR_STRING;
5664 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
5668 if (*p == '\\')
5669 {
5670 switch (*++p)
5671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 case 'b': *name++ = BS; ++p; break;
5673 case 'e': *name++ = ESC; ++p; break;
5674 case 'f': *name++ = FF; ++p; break;
5675 case 'n': *name++ = NL; ++p; break;
5676 case 'r': *name++ = CAR; ++p; break;
5677 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
5679 case 'X': /* hex: "\x1", "\x12" */
5680 case 'x':
5681 case 'u': /* Unicode: "\u0023" */
5682 case 'U':
5683 if (vim_isxdigit(p[1]))
5684 {
5685 int n, nr;
5686 int c = toupper(*p);
5687
5688 if (c == 'X')
5689 n = 2;
5690 else
5691 n = 4;
5692 nr = 0;
5693 while (--n >= 0 && vim_isxdigit(p[1]))
5694 {
5695 ++p;
5696 nr = (nr << 4) + hex2nr(*p);
5697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699#ifdef FEAT_MBYTE
5700 /* For "\u" store the number according to
5701 * 'encoding'. */
5702 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
5705#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 break;
5709
5710 /* octal: "\1", "\12", "\123" */
5711 case '0':
5712 case '1':
5713 case '2':
5714 case '3':
5715 case '4':
5716 case '5':
5717 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 case '7': *name = *p++ - '0';
5719 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 *name = (*name << 3) + *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
5723 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727
5728 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (extra != 0)
5731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 break;
5734 }
5735 /* FALLTHROUGH */
5736
Bram Moolenaar8c711452005-01-14 21:53:12 +00005737 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 break;
5739 }
5740 }
5741 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 *arg = p + 1;
5747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 return OK;
5749}
5750
5751/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 * Return OK or FAIL.
5754 */
5755 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 int evaluate;
5760{
5761 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 int reduce = 0;
5764
5765 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 */
5768 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 break;
5774 ++reduce;
5775 ++p;
5776 }
5777 }
5778
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 return FAIL;
5783 }
5784
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 if (!evaluate)
5787 {
5788 *arg = p + 1;
5789 return OK;
5790 }
5791
5792 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 */
5795 str = alloc((unsigned)((p - *arg) - reduce));
5796 if (str == NULL)
5797 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 rettv->v_type = VAR_STRING;
5799 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005802 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005806 break;
5807 ++p;
5808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005812 *arg = p + 1;
5813
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 return OK;
5815}
5816
5817/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 * Allocate a variable for a List and fill it from "*arg".
5819 * Return OK or FAIL.
5820 */
5821 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005822get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005824 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005825 int evaluate;
5826{
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 list_T *l = NULL;
5828 typval_T tv;
5829 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830
5831 if (evaluate)
5832 {
5833 l = list_alloc();
5834 if (l == NULL)
5835 return FAIL;
5836 }
5837
5838 *arg = skipwhite(*arg + 1);
5839 while (**arg != ']' && **arg != NUL)
5840 {
5841 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5842 goto failret;
5843 if (evaluate)
5844 {
5845 item = listitem_alloc();
5846 if (item != NULL)
5847 {
5848 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005849 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 list_append(l, item);
5851 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005852 else
5853 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 }
5855
5856 if (**arg == ']')
5857 break;
5858 if (**arg != ',')
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 goto failret;
5862 }
5863 *arg = skipwhite(*arg + 1);
5864 }
5865
5866 if (**arg != ']')
5867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869failret:
5870 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005871 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 return FAIL;
5873 }
5874
5875 *arg = skipwhite(*arg + 1);
5876 if (evaluate)
5877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005878 rettv->v_type = VAR_LIST;
5879 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 ++l->lv_refcount;
5881 }
5882
5883 return OK;
5884}
5885
5886/*
5887 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005888 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005889 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005890 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891list_alloc()
5892{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893 list_T *l;
5894
5895 l = (list_T *)alloc_clear(sizeof(list_T));
5896 if (l != NULL)
5897 {
5898 /* Prepend the list to the list of lists for garbage collection. */
5899 if (first_list != NULL)
5900 first_list->lv_used_prev = l;
5901 l->lv_used_prev = NULL;
5902 l->lv_used_next = first_list;
5903 first_list = l;
5904 }
5905 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906}
5907
5908/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005909 * Allocate an empty list for a return value.
5910 * Returns OK or FAIL.
5911 */
5912 static int
5913rettv_list_alloc(rettv)
5914 typval_T *rettv;
5915{
5916 list_T *l = list_alloc();
5917
5918 if (l == NULL)
5919 return FAIL;
5920
5921 rettv->vval.v_list = l;
5922 rettv->v_type = VAR_LIST;
5923 ++l->lv_refcount;
5924 return OK;
5925}
5926
5927/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 * Unreference a list: decrement the reference count and free it when it
5929 * becomes zero.
5930 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005931 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005933 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005935 if (l != NULL && --l->lv_refcount <= 0)
5936 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937}
5938
5939/*
5940 * Free a list, including all items it points to.
5941 * Ignores the reference count.
5942 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005943 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005944list_free(l, recurse)
5945 list_T *l;
5946 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947{
Bram Moolenaar33570922005-01-25 22:26:29 +00005948 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005950 /* Remove the list from the list of lists for garbage collection. */
5951 if (l->lv_used_prev == NULL)
5952 first_list = l->lv_used_next;
5953 else
5954 l->lv_used_prev->lv_used_next = l->lv_used_next;
5955 if (l->lv_used_next != NULL)
5956 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5957
Bram Moolenaard9fba312005-06-26 22:34:35 +00005958 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005960 /* Remove the item before deleting it. */
5961 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005962 if (recurse || (item->li_tv.v_type != VAR_LIST
5963 && item->li_tv.v_type != VAR_DICT))
5964 clear_tv(&item->li_tv);
5965 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 }
5967 vim_free(l);
5968}
5969
5970/*
5971 * Allocate a list item.
5972 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005973 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974listitem_alloc()
5975{
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977}
5978
5979/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005980 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005982 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005986 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 vim_free(item);
5988}
5989
5990/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005991 * Remove a list item from a List and free it. Also clears the value.
5992 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005993 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l;
5996 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997{
5998 list_remove(l, item, item);
5999 listitem_free(item);
6000}
6001
6002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Get the number of items in a list.
6004 */
6005 static long
6006list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006007 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 if (l == NULL)
6010 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006011 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012}
6013
6014/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 * Return TRUE when two lists have exactly the same values.
6016 */
6017 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006018list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006019 list_T *l1;
6020 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006022 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006023{
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006026 if (l1 == NULL || l2 == NULL)
6027 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006028 if (l1 == l2)
6029 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006030 if (list_len(l1) != list_len(l2))
6031 return FALSE;
6032
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006033 for (item1 = l1->lv_first, item2 = l2->lv_first;
6034 item1 != NULL && item2 != NULL;
6035 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006037 return FALSE;
6038 return item1 == NULL && item2 == NULL;
6039}
6040
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006041#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6042 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006043/*
6044 * Return the dictitem that an entry in a hashtable points to.
6045 */
6046 dictitem_T *
6047dict_lookup(hi)
6048 hashitem_T *hi;
6049{
6050 return HI2DI(hi);
6051}
6052#endif
6053
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055 * Return TRUE when two dictionaries have exactly the same key/values.
6056 */
6057 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006058dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 dict_T *d1;
6060 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 hashitem_T *hi;
6065 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006068 if (d1 == NULL || d2 == NULL)
6069 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006070 if (d1 == d2)
6071 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006072 if (dict_len(d1) != dict_len(d2))
6073 return FALSE;
6074
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006075 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006077 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006078 if (!HASHITEM_EMPTY(hi))
6079 {
6080 item2 = dict_find(d2, hi->hi_key, -1);
6081 if (item2 == NULL)
6082 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006084 return FALSE;
6085 --todo;
6086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006087 }
6088 return TRUE;
6089}
6090
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091static int tv_equal_recurse_limit;
6092
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006093/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006094 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006095 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006096 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 */
6098 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006099tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 typval_T *tv1;
6101 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102 int ic; /* ignore case */
6103 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
6105 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006106 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006108 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006110 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 * recursiveness to a limit. We guess they are equal then.
6115 * A fixed limit has the problem of still taking an awful long time.
6116 * Reduce the limit every time running into it. That should work fine for
6117 * deeply linked structures that are not recursively linked and catch
6118 * recursiveness quickly. */
6119 if (!recursive)
6120 tv_equal_recurse_limit = 1000;
6121 if (recursive_cnt >= tv_equal_recurse_limit)
6122 {
6123 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006124 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006125 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006126
6127 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130 ++recursive_cnt;
6131 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6132 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006133 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006134
6135 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006136 ++recursive_cnt;
6137 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6138 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006139 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140
6141 case VAR_FUNC:
6142 return (tv1->vval.v_string != NULL
6143 && tv2->vval.v_string != NULL
6144 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6145
6146 case VAR_NUMBER:
6147 return tv1->vval.v_number == tv2->vval.v_number;
6148
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006149#ifdef FEAT_FLOAT
6150 case VAR_FLOAT:
6151 return tv1->vval.v_float == tv2->vval.v_float;
6152#endif
6153
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006154 case VAR_STRING:
6155 s1 = get_tv_string_buf(tv1, buf1);
6156 s2 = get_tv_string_buf(tv2, buf2);
6157 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006159
6160 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 return TRUE;
6162}
6163
6164/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 * Locate item with index "n" in list "l" and return it.
6166 * A negative index is counted from the end; -1 is the last item.
6167 * Returns NULL when "n" is out of range.
6168 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006169 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 long n;
6173{
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long idx;
6176
6177 if (l == NULL)
6178 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006179
6180 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182 n = l->lv_len + n;
6183
6184 /* Check for index out of range. */
6185 if (n < 0 || n >= l->lv_len)
6186 return NULL;
6187
6188 /* When there is a cached index may start search from there. */
6189 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006191 if (n < l->lv_idx / 2)
6192 {
6193 /* closest to the start of the list */
6194 item = l->lv_first;
6195 idx = 0;
6196 }
6197 else if (n > (l->lv_idx + l->lv_len) / 2)
6198 {
6199 /* closest to the end of the list */
6200 item = l->lv_last;
6201 idx = l->lv_len - 1;
6202 }
6203 else
6204 {
6205 /* closest to the cached index */
6206 item = l->lv_idx_item;
6207 idx = l->lv_idx;
6208 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209 }
6210 else
6211 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 if (n < l->lv_len / 2)
6213 {
6214 /* closest to the start of the list */
6215 item = l->lv_first;
6216 idx = 0;
6217 }
6218 else
6219 {
6220 /* closest to the end of the list */
6221 item = l->lv_last;
6222 idx = l->lv_len - 1;
6223 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006225
6226 while (n > idx)
6227 {
6228 /* search forward */
6229 item = item->li_next;
6230 ++idx;
6231 }
6232 while (n < idx)
6233 {
6234 /* search backward */
6235 item = item->li_prev;
6236 --idx;
6237 }
6238
6239 /* cache the used index */
6240 l->lv_idx = idx;
6241 l->lv_idx_item = item;
6242
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 return item;
6244}
6245
6246/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006247 * Get list item "l[idx]" as a number.
6248 */
6249 static long
6250list_find_nr(l, idx, errorp)
6251 list_T *l;
6252 long idx;
6253 int *errorp; /* set to TRUE when something wrong */
6254{
6255 listitem_T *li;
6256
6257 li = list_find(l, idx);
6258 if (li == NULL)
6259 {
6260 if (errorp != NULL)
6261 *errorp = TRUE;
6262 return -1L;
6263 }
6264 return get_tv_number_chk(&li->li_tv, errorp);
6265}
6266
6267/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006268 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6269 */
6270 char_u *
6271list_find_str(l, idx)
6272 list_T *l;
6273 long idx;
6274{
6275 listitem_T *li;
6276
6277 li = list_find(l, idx - 1);
6278 if (li == NULL)
6279 {
6280 EMSGN(_(e_listidx), idx);
6281 return NULL;
6282 }
6283 return get_tv_string(&li->li_tv);
6284}
6285
6286/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287 * Locate "item" list "l" and return its index.
6288 * Returns -1 when "item" is not in the list.
6289 */
6290 static long
6291list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 list_T *l;
6293 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006294{
6295 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297
6298 if (l == NULL)
6299 return -1;
6300 idx = 0;
6301 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6302 ++idx;
6303 if (li == NULL)
6304 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006305 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006306}
6307
6308/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 * Append item "item" to the end of list "l".
6310 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006311 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 list_T *l;
6314 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315{
6316 if (l->lv_last == NULL)
6317 {
6318 /* empty list */
6319 l->lv_first = item;
6320 l->lv_last = item;
6321 item->li_prev = NULL;
6322 }
6323 else
6324 {
6325 l->lv_last->li_next = item;
6326 item->li_prev = l->lv_last;
6327 l->lv_last = item;
6328 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006329 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 item->li_next = NULL;
6331}
6332
6333/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006337 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 list_T *l;
6340 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006342 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343
Bram Moolenaar05159a02005-02-26 23:04:13 +00006344 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 copy_tv(tv, &li->li_tv);
6347 list_append(l, li);
6348 return OK;
6349}
6350
6351/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006352 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006353 * Return FAIL when out of memory.
6354 */
6355 int
6356list_append_dict(list, dict)
6357 list_T *list;
6358 dict_T *dict;
6359{
6360 listitem_T *li = listitem_alloc();
6361
6362 if (li == NULL)
6363 return FAIL;
6364 li->li_tv.v_type = VAR_DICT;
6365 li->li_tv.v_lock = 0;
6366 li->li_tv.vval.v_dict = dict;
6367 list_append(list, li);
6368 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 return OK;
6370}
6371
6372/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006373 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375 * Returns FAIL when out of memory.
6376 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006377 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006378list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006379 list_T *l;
6380 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382{
6383 listitem_T *li = listitem_alloc();
6384
6385 if (li == NULL)
6386 return FAIL;
6387 list_append(l, li);
6388 li->li_tv.v_type = VAR_STRING;
6389 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006390 if (str == NULL)
6391 li->li_tv.vval.v_string = NULL;
6392 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006393 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006394 return FAIL;
6395 return OK;
6396}
6397
6398/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006399 * Append "n" to list "l".
6400 * Returns FAIL when out of memory.
6401 */
6402 static int
6403list_append_number(l, n)
6404 list_T *l;
6405 varnumber_T n;
6406{
6407 listitem_T *li;
6408
6409 li = listitem_alloc();
6410 if (li == NULL)
6411 return FAIL;
6412 li->li_tv.v_type = VAR_NUMBER;
6413 li->li_tv.v_lock = 0;
6414 li->li_tv.vval.v_number = n;
6415 list_append(l, li);
6416 return OK;
6417}
6418
6419/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 * If "item" is NULL append at the end.
6422 * Return FAIL when out of memory.
6423 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006424 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006426 list_T *l;
6427 typval_T *tv;
6428 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429{
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431
6432 if (ni == NULL)
6433 return FAIL;
6434 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006435 list_insert(l, ni, item);
6436 return OK;
6437}
6438
6439 void
6440list_insert(l, ni, item)
6441 list_T *l;
6442 listitem_T *ni;
6443 listitem_T *item;
6444{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445 if (item == NULL)
6446 /* Append new item at end of list. */
6447 list_append(l, ni);
6448 else
6449 {
6450 /* Insert new item before existing item. */
6451 ni->li_prev = item->li_prev;
6452 ni->li_next = item;
6453 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006456 ++l->lv_idx;
6457 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006461 l->lv_idx_item = NULL;
6462 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466}
6467
6468/*
6469 * Extend "l1" with "l2".
6470 * If "bef" is NULL append at the end, otherwise insert before this item.
6471 * Returns FAIL when out of memory.
6472 */
6473 static int
6474list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *l1;
6476 list_T *l2;
6477 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478{
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006480 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006482 /* We also quit the loop when we have inserted the original item count of
6483 * the list, avoid a hang when we extend a list with itself. */
6484 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6486 return FAIL;
6487 return OK;
6488}
6489
6490/*
6491 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6492 * Return FAIL when out of memory.
6493 */
6494 static int
6495list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *l1;
6497 list_T *l2;
6498 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006502 if (l1 == NULL || l2 == NULL)
6503 return FAIL;
6504
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006506 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 if (l == NULL)
6508 return FAIL;
6509 tv->v_type = VAR_LIST;
6510 tv->vval.v_list = l;
6511
6512 /* append all items from the second list */
6513 return list_extend(l, l2, NULL);
6514}
6515
6516/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006517 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 * Returns NULL when out of memory.
6521 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527{
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 list_T *copy;
6529 listitem_T *item;
6530 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531
6532 if (orig == NULL)
6533 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 copy = list_alloc();
6536 if (copy != NULL)
6537 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 if (copyID != 0)
6539 {
6540 /* Do this before adding the items, because one of the items may
6541 * refer back to this list. */
6542 orig->lv_copyID = copyID;
6543 orig->lv_copylist = copy;
6544 }
6545 for (item = orig->lv_first; item != NULL && !got_int;
6546 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006547 {
6548 ni = listitem_alloc();
6549 if (ni == NULL)
6550 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006552 {
6553 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6554 {
6555 vim_free(ni);
6556 break;
6557 }
6558 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006560 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 list_append(copy, ni);
6562 }
6563 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 if (item != NULL)
6565 {
6566 list_unref(copy);
6567 copy = NULL;
6568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569 }
6570
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571 return copy;
6572}
6573
6574/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006576 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006578 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 list_T *l;
6581 listitem_T *item;
6582 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583{
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586 /* notify watchers */
6587 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006589 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590 list_fix_watch(l, ip);
6591 if (ip == item2)
6592 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594
6595 if (item2->li_next == NULL)
6596 l->lv_last = item->li_prev;
6597 else
6598 item2->li_next->li_prev = item->li_prev;
6599 if (item->li_prev == NULL)
6600 l->lv_first = item2->li_next;
6601 else
6602 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006603 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604}
6605
6606/*
6607 * Return an allocated string with the string representation of a list.
6608 * May return NULL.
6609 */
6610 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006611list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614{
6615 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616
6617 if (tv->vval.v_list == NULL)
6618 return NULL;
6619 ga_init2(&ga, (int)sizeof(char), 80);
6620 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006621 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 {
6623 vim_free(ga.ga_data);
6624 return NULL;
6625 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 ga_append(&ga, ']');
6627 ga_append(&ga, NUL);
6628 return (char_u *)ga.ga_data;
6629}
6630
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006631typedef struct join_S {
6632 char_u *s;
6633 char_u *tofree;
6634} join_T;
6635
6636 static int
6637list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6638 garray_T *gap; /* to store the result in */
6639 list_T *l;
6640 char_u *sep;
6641 int echo_style;
6642 int copyID;
6643 garray_T *join_gap; /* to keep each list item string */
6644{
6645 int i;
6646 join_T *p;
6647 int len;
6648 int sumlen = 0;
6649 int first = TRUE;
6650 char_u *tofree;
6651 char_u numbuf[NUMBUFLEN];
6652 listitem_T *item;
6653 char_u *s;
6654
6655 /* Stringify each item in the list. */
6656 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6657 {
6658 if (echo_style)
6659 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6660 else
6661 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6662 if (s == NULL)
6663 return FAIL;
6664
6665 len = (int)STRLEN(s);
6666 sumlen += len;
6667
6668 ga_grow(join_gap, 1);
6669 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6670 if (tofree != NULL || s != numbuf)
6671 {
6672 p->s = s;
6673 p->tofree = tofree;
6674 }
6675 else
6676 {
6677 p->s = vim_strnsave(s, len);
6678 p->tofree = p->s;
6679 }
6680
6681 line_breakcheck();
6682 }
6683
6684 /* Allocate result buffer with its total size, avoid re-allocation and
6685 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6686 if (join_gap->ga_len >= 2)
6687 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6688 if (ga_grow(gap, sumlen + 2) == FAIL)
6689 return FAIL;
6690
6691 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6692 {
6693 if (first)
6694 first = FALSE;
6695 else
6696 ga_concat(gap, sep);
6697 p = ((join_T *)join_gap->ga_data) + i;
6698
6699 if (p->s != NULL)
6700 ga_concat(gap, p->s);
6701 line_breakcheck();
6702 }
6703
6704 return OK;
6705}
6706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006708 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006709 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006710 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006713list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006715 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006717 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006718 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720 garray_T join_ga;
6721 int retval;
6722 join_T *p;
6723 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6726 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6727
6728 /* Dispose each item in join_ga. */
6729 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006731 p = (join_T *)join_ga.ga_data;
6732 for (i = 0; i < join_ga.ga_len; ++i)
6733 {
6734 vim_free(p->tofree);
6735 ++p;
6736 }
6737 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006738 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739
6740 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741}
6742
6743/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 * Garbage collection for lists and dictionaries.
6745 *
6746 * We use reference counts to be able to free most items right away when they
6747 * are no longer used. But for composite items it's possible that it becomes
6748 * unused while the reference count is > 0: When there is a recursive
6749 * reference. Example:
6750 * :let l = [1, 2, 3]
6751 * :let d = {9: l}
6752 * :let l[1] = d
6753 *
6754 * Since this is quite unusual we handle this with garbage collection: every
6755 * once in a while find out which lists and dicts are not referenced from any
6756 * variable.
6757 *
6758 * Here is a good reference text about garbage collection (refers to Python
6759 * but it applies to all reference-counting mechanisms):
6760 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762
6763/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 * Do garbage collection for lists and dicts.
6765 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006767 int
6768garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006769{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 buf_T *buf;
6772 win_T *wp;
6773 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006774 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006775 int did_free;
6776 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006777#ifdef FEAT_WINDOWS
6778 tabpage_T *tp;
6779#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006781 /* Only do this once. */
6782 want_garbage_collect = FALSE;
6783 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006784 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006785
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006786 /* We advance by two because we add one for items referenced through
6787 * previous_funccal. */
6788 current_copyID += COPYID_INC;
6789 copyID = current_copyID;
6790
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006791 /*
6792 * 1. Go through all accessible variables and mark all lists and dicts
6793 * with copyID.
6794 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006795
6796 /* Don't free variables in the previous_funccal list unless they are only
6797 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006798 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006799 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6800 {
6801 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6802 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6803 }
6804
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 /* script-local variables */
6806 for (i = 1; i <= ga_scripts.ga_len; ++i)
6807 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6808
6809 /* buffer-local variables */
6810 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006811 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812
6813 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006815 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006816#ifdef FEAT_AUTOCMD
6817 if (aucmd_win != NULL)
6818 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6819#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006821#ifdef FEAT_WINDOWS
6822 /* tabpage-local variables */
6823 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006824 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006825#endif
6826
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006827 /* global variables */
6828 set_ref_in_ht(&globvarht, copyID);
6829
6830 /* function-local variables */
6831 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6832 {
6833 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6834 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6835 }
6836
Bram Moolenaard812df62008-11-09 12:46:09 +00006837 /* v: vars */
6838 set_ref_in_ht(&vimvarht, copyID);
6839
Bram Moolenaar1dced572012-04-05 16:54:08 +02006840#ifdef FEAT_LUA
6841 set_ref_in_lua(copyID);
6842#endif
6843
Bram Moolenaardb913952012-06-29 12:54:53 +02006844#ifdef FEAT_PYTHON
6845 set_ref_in_python(copyID);
6846#endif
6847
6848#ifdef FEAT_PYTHON3
6849 set_ref_in_python3(copyID);
6850#endif
6851
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006852 /*
6853 * 2. Free lists and dictionaries that are not referenced.
6854 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 did_free = free_unref_items(copyID);
6856
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006857 /*
6858 * 3. Check if any funccal can be freed now.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 for (pfc = &previous_funccal; *pfc != NULL; )
6861 {
6862 if (can_free_funccal(*pfc, copyID))
6863 {
6864 fc = *pfc;
6865 *pfc = fc->caller;
6866 free_funccal(fc, TRUE);
6867 did_free = TRUE;
6868 did_free_funccal = TRUE;
6869 }
6870 else
6871 pfc = &(*pfc)->caller;
6872 }
6873 if (did_free_funccal)
6874 /* When a funccal was freed some more items might be garbage
6875 * collected, so run again. */
6876 (void)garbage_collect();
6877
6878 return did_free;
6879}
6880
6881/*
6882 * Free lists and dictionaries that are no longer referenced.
6883 */
6884 static int
6885free_unref_items(copyID)
6886 int copyID;
6887{
6888 dict_T *dd;
6889 list_T *ll;
6890 int did_free = FALSE;
6891
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 */
6895 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006896 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006898 /* Free the Dictionary and ordinary items it contains, but don't
6899 * recurse into Lists and Dictionaries, they will be in the list
6900 * of dicts or list of lists. */
6901 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 did_free = TRUE;
6903
6904 /* restart, next dict may also have been freed */
6905 dd = first_dict;
6906 }
6907 else
6908 dd = dd->dv_used_next;
6909
6910 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006911 * Go through the list of lists and free items without the copyID.
6912 * But don't free a list that has a watcher (used in a for loop), these
6913 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914 */
6915 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006916 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6917 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006919 /* Free the List and ordinary items it contains, but don't recurse
6920 * into Lists and Dictionaries, they will be in the list of dicts
6921 * or list of lists. */
6922 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 did_free = TRUE;
6924
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006925 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 ll = first_list;
6927 }
6928 else
6929 ll = ll->lv_used_next;
6930
6931 return did_free;
6932}
6933
6934/*
6935 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6936 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006937 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938set_ref_in_ht(ht, copyID)
6939 hashtab_T *ht;
6940 int copyID;
6941{
6942 int todo;
6943 hashitem_T *hi;
6944
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006945 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006946 for (hi = ht->ht_array; todo > 0; ++hi)
6947 if (!HASHITEM_EMPTY(hi))
6948 {
6949 --todo;
6950 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6951 }
6952}
6953
6954/*
6955 * Mark all lists and dicts referenced through list "l" with "copyID".
6956 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006957 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958set_ref_in_list(l, copyID)
6959 list_T *l;
6960 int copyID;
6961{
6962 listitem_T *li;
6963
6964 for (li = l->lv_first; li != NULL; li = li->li_next)
6965 set_ref_in_item(&li->li_tv, copyID);
6966}
6967
6968/*
6969 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006971 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972set_ref_in_item(tv, copyID)
6973 typval_T *tv;
6974 int copyID;
6975{
6976 dict_T *dd;
6977 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006978
6979 switch (tv->v_type)
6980 {
6981 case VAR_DICT:
6982 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006983 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006984 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 /* Didn't see this dict yet. */
6986 dd->dv_copyID = copyID;
6987 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006990
6991 case VAR_LIST:
6992 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006993 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006994 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 /* Didn't see this list yet. */
6996 ll->lv_copyID = copyID;
6997 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006998 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007000 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007002}
7003
7004/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007005 * Allocate an empty header for a dictionary.
7006 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007007 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007008dict_alloc()
7009{
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007011
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007013 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007014 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007015 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 if (first_dict != NULL)
7017 first_dict->dv_used_prev = d;
7018 d->dv_used_next = first_dict;
7019 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021
Bram Moolenaar33570922005-01-25 22:26:29 +00007022 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007023 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007024 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007025 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007026 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007027 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007028 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029}
7030
7031/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007032 * Allocate an empty dict for a return value.
7033 * Returns OK or FAIL.
7034 */
7035 static int
7036rettv_dict_alloc(rettv)
7037 typval_T *rettv;
7038{
7039 dict_T *d = dict_alloc();
7040
7041 if (d == NULL)
7042 return FAIL;
7043
7044 rettv->vval.v_dict = d;
7045 rettv->v_type = VAR_DICT;
7046 ++d->dv_refcount;
7047 return OK;
7048}
7049
7050
7051/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052 * Unreference a Dictionary: decrement the reference count and free it when it
7053 * becomes zero.
7054 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007055 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007056dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007059 if (d != NULL && --d->dv_refcount <= 0)
7060 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061}
7062
7063/*
7064 * Free a Dictionary, including all items it contains.
7065 * Ignores the reference count.
7066 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007067 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007068dict_free(d, recurse)
7069 dict_T *d;
7070 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007074 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 /* Remove the dict from the list of dicts for garbage collection. */
7077 if (d->dv_used_prev == NULL)
7078 first_dict = d->dv_used_next;
7079 else
7080 d->dv_used_prev->dv_used_next = d->dv_used_next;
7081 if (d->dv_used_next != NULL)
7082 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7083
7084 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007085 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007086 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007087 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089 if (!HASHITEM_EMPTY(hi))
7090 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007091 /* Remove the item before deleting it, just in case there is
7092 * something recursive causing trouble. */
7093 di = HI2DI(hi);
7094 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007095 if (recurse || (di->di_tv.v_type != VAR_LIST
7096 && di->di_tv.v_type != VAR_DICT))
7097 clear_tv(&di->di_tv);
7098 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 --todo;
7100 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 vim_free(d);
7104}
7105
7106/*
7107 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007108 * The "key" is copied to the new item.
7109 * Note that the value of the item "di_tv" still needs to be initialized!
7110 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007111 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007112 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113dictitem_alloc(key)
7114 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115{
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007118 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007120 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007122 di->di_flags = 0;
7123 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125}
7126
7127/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128 * Make a copy of a Dictionary item.
7129 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133{
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007136 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7137 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138 if (di != NULL)
7139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007140 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007141 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007142 copy_tv(&org->di_tv, &di->di_tv);
7143 }
7144 return di;
7145}
7146
7147/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 * Remove item "item" from Dictionary "dict" and free it.
7149 */
7150 static void
7151dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 dict_T *dict;
7153 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154{
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007156
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007158 if (HASHITEM_EMPTY(hi))
7159 EMSG2(_(e_intern2), "dictitem_remove()");
7160 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 dictitem_free(item);
7163}
7164
7165/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007166 * Free a dict item. Also clears the value.
7167 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007168 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007169dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007170 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 clear_tv(&item->di_tv);
7173 vim_free(item);
7174}
7175
7176/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7178 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 * Returns NULL when out of memory.
7181 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007183dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007187{
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 dict_T *copy;
7189 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192
7193 if (orig == NULL)
7194 return NULL;
7195
7196 copy = dict_alloc();
7197 if (copy != NULL)
7198 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 if (copyID != 0)
7200 {
7201 orig->dv_copyID = copyID;
7202 orig->dv_copydict = copy;
7203 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007204 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209 --todo;
7210
7211 di = dictitem_alloc(hi->hi_key);
7212 if (di == NULL)
7213 break;
7214 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007215 {
7216 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7217 copyID) == FAIL)
7218 {
7219 vim_free(di);
7220 break;
7221 }
7222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 else
7224 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7225 if (dict_add(copy, di) == FAIL)
7226 {
7227 dictitem_free(di);
7228 break;
7229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007232
Bram Moolenaare9a41262005-01-15 22:18:47 +00007233 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007234 if (todo > 0)
7235 {
7236 dict_unref(copy);
7237 copy = NULL;
7238 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007239 }
7240
7241 return copy;
7242}
7243
7244/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007246 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007248 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 dict_T *d;
7251 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252{
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254}
7255
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007257 * Add a number or string entry to dictionary "d".
7258 * When "str" is NULL use number "nr", otherwise use "str".
7259 * Returns FAIL when out of memory and when key already exists.
7260 */
7261 int
7262dict_add_nr_str(d, key, nr, str)
7263 dict_T *d;
7264 char *key;
7265 long nr;
7266 char_u *str;
7267{
7268 dictitem_T *item;
7269
7270 item = dictitem_alloc((char_u *)key);
7271 if (item == NULL)
7272 return FAIL;
7273 item->di_tv.v_lock = 0;
7274 if (str == NULL)
7275 {
7276 item->di_tv.v_type = VAR_NUMBER;
7277 item->di_tv.vval.v_number = nr;
7278 }
7279 else
7280 {
7281 item->di_tv.v_type = VAR_STRING;
7282 item->di_tv.vval.v_string = vim_strsave(str);
7283 }
7284 if (dict_add(d, item) == FAIL)
7285 {
7286 dictitem_free(item);
7287 return FAIL;
7288 }
7289 return OK;
7290}
7291
7292/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007293 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007294 * Returns FAIL when out of memory and when key already exists.
7295 */
7296 int
7297dict_add_list(d, key, list)
7298 dict_T *d;
7299 char *key;
7300 list_T *list;
7301{
7302 dictitem_T *item;
7303
7304 item = dictitem_alloc((char_u *)key);
7305 if (item == NULL)
7306 return FAIL;
7307 item->di_tv.v_lock = 0;
7308 item->di_tv.v_type = VAR_LIST;
7309 item->di_tv.vval.v_list = list;
7310 if (dict_add(d, item) == FAIL)
7311 {
7312 dictitem_free(item);
7313 return FAIL;
7314 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007315 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007316 return OK;
7317}
7318
7319/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 * Get the number of items in a Dictionary.
7321 */
7322 static long
7323dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 if (d == NULL)
7327 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007328 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329}
7330
7331/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 * Find item "key[len]" in Dictionary "d".
7333 * If "len" is negative use strlen(key).
7334 * Returns NULL when not found.
7335 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007336 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 char_u *key;
7340 int len;
7341{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342#define AKEYLEN 200
7343 char_u buf[AKEYLEN];
7344 char_u *akey;
7345 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 if (len < 0)
7349 akey = key;
7350 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 tofree = akey = vim_strnsave(key, len);
7353 if (akey == NULL)
7354 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007355 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007356 else
7357 {
7358 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007359 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 akey = buf;
7361 }
7362
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 vim_free(tofree);
7365 if (HASHITEM_EMPTY(hi))
7366 return NULL;
7367 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368}
7369
7370/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007371 * Get a string item from a dictionary.
7372 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007373 * Returns NULL if the entry doesn't exist or out of memory.
7374 */
7375 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007376get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007377 dict_T *d;
7378 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007379 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007380{
7381 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007383
7384 di = dict_find(d, key, -1);
7385 if (di == NULL)
7386 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007387 s = get_tv_string(&di->di_tv);
7388 if (save && s != NULL)
7389 s = vim_strsave(s);
7390 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007391}
7392
7393/*
7394 * Get a number item from a dictionary.
7395 * Returns 0 if the entry doesn't exist or out of memory.
7396 */
7397 long
7398get_dict_number(d, key)
7399 dict_T *d;
7400 char_u *key;
7401{
7402 dictitem_T *di;
7403
7404 di = dict_find(d, key, -1);
7405 if (di == NULL)
7406 return 0;
7407 return get_tv_number(&di->di_tv);
7408}
7409
7410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 * Return an allocated string with the string representation of a Dictionary.
7412 * May return NULL.
7413 */
7414 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007417 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418{
7419 garray_T ga;
7420 int first = TRUE;
7421 char_u *tofree;
7422 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 return NULL;
7430 ga_init2(&ga, (int)sizeof(char), 80);
7431 ga_append(&ga, '{');
7432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007433 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007434 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 --todo;
7439
7440 if (first)
7441 first = FALSE;
7442 else
7443 ga_concat(&ga, (char_u *)", ");
7444
7445 tofree = string_quote(hi->hi_key, FALSE);
7446 if (tofree != NULL)
7447 {
7448 ga_concat(&ga, tofree);
7449 vim_free(tofree);
7450 }
7451 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007452 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007453 if (s != NULL)
7454 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007456 if (s == NULL)
7457 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007460 if (todo > 0)
7461 {
7462 vim_free(ga.ga_data);
7463 return NULL;
7464 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465
7466 ga_append(&ga, '}');
7467 ga_append(&ga, NUL);
7468 return (char_u *)ga.ga_data;
7469}
7470
7471/*
7472 * Allocate a variable for a Dictionary and fill it from "*arg".
7473 * Return OK or FAIL. Returns NOTDONE for {expr}.
7474 */
7475 static int
7476get_dict_tv(arg, rettv, evaluate)
7477 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 int evaluate;
7480{
Bram Moolenaar33570922005-01-25 22:26:29 +00007481 dict_T *d = NULL;
7482 typval_T tvkey;
7483 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007484 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488
7489 /*
7490 * First check if it's not a curly-braces thing: {expr}.
7491 * Must do this without evaluating, otherwise a function may be called
7492 * twice. Unfortunately this means we need to call eval1() twice for the
7493 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007496 if (*start != '}')
7497 {
7498 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7499 return FAIL;
7500 if (*start == '}')
7501 return NOTDONE;
7502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503
7504 if (evaluate)
7505 {
7506 d = dict_alloc();
7507 if (d == NULL)
7508 return FAIL;
7509 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 tvkey.v_type = VAR_UNKNOWN;
7511 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512
7513 *arg = skipwhite(*arg + 1);
7514 while (**arg != '}' && **arg != NUL)
7515 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 goto failret;
7518 if (**arg != ':')
7519 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007520 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 goto failret;
7523 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007524 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007526 key = get_tv_string_buf_chk(&tvkey, buf);
7527 if (key == NULL || *key == NUL)
7528 {
7529 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7530 if (key != NULL)
7531 EMSG(_(e_emptykey));
7532 clear_tv(&tvkey);
7533 goto failret;
7534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536
7537 *arg = skipwhite(*arg + 1);
7538 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7539 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007540 if (evaluate)
7541 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 goto failret;
7543 }
7544 if (evaluate)
7545 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007546 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 if (item != NULL)
7548 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007549 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 clear_tv(&tv);
7552 goto failret;
7553 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 item = dictitem_alloc(key);
7555 clear_tv(&tvkey);
7556 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007559 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 if (dict_add(d, item) == FAIL)
7561 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 }
7563 }
7564
7565 if (**arg == '}')
7566 break;
7567 if (**arg != ',')
7568 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007569 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 goto failret;
7571 }
7572 *arg = skipwhite(*arg + 1);
7573 }
7574
7575 if (**arg != '}')
7576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007577 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578failret:
7579 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007580 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 return FAIL;
7582 }
7583
7584 *arg = skipwhite(*arg + 1);
7585 if (evaluate)
7586 {
7587 rettv->v_type = VAR_DICT;
7588 rettv->vval.v_dict = d;
7589 ++d->dv_refcount;
7590 }
7591
7592 return OK;
7593}
7594
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 * Return a string with the string representation of a variable.
7597 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007598 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007599 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007601 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 */
7603 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007605 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007606 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007607 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007608 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007609{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 static int recurse = 0;
7611 char_u *r = NULL;
7612
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007615 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 *tofree = NULL;
7617 return NULL;
7618 }
7619 ++recurse;
7620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 switch (tv->v_type)
7622 {
7623 case VAR_FUNC:
7624 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625 r = tv->vval.v_string;
7626 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007628 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007629 if (tv->vval.v_list == NULL)
7630 {
7631 *tofree = NULL;
7632 r = NULL;
7633 }
7634 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7635 {
7636 *tofree = NULL;
7637 r = (char_u *)"[...]";
7638 }
7639 else
7640 {
7641 tv->vval.v_list->lv_copyID = copyID;
7642 *tofree = list2string(tv, copyID);
7643 r = *tofree;
7644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648 if (tv->vval.v_dict == NULL)
7649 {
7650 *tofree = NULL;
7651 r = NULL;
7652 }
7653 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7654 {
7655 *tofree = NULL;
7656 r = (char_u *)"{...}";
7657 }
7658 else
7659 {
7660 tv->vval.v_dict->dv_copyID = copyID;
7661 *tofree = dict2string(tv, copyID);
7662 r = *tofree;
7663 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 case VAR_STRING:
7667 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 *tofree = NULL;
7669 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007670 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 case VAR_FLOAT:
7674 *tofree = NULL;
7675 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7676 r = numbuf;
7677 break;
7678#endif
7679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007681 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007682 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684
7685 --recurse;
7686 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007687}
7688
7689/*
7690 * Return a string with the string representation of a variable.
7691 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7692 * "numbuf" is used for a number.
7693 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007694 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695 */
7696 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007697tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007699 char_u **tofree;
7700 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007701 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702{
7703 switch (tv->v_type)
7704 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 case VAR_FUNC:
7706 *tofree = string_quote(tv->vval.v_string, TRUE);
7707 return *tofree;
7708 case VAR_STRING:
7709 *tofree = string_quote(tv->vval.v_string, FALSE);
7710 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007711#ifdef FEAT_FLOAT
7712 case VAR_FLOAT:
7713 *tofree = NULL;
7714 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7715 return numbuf;
7716#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007722 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007723 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007724 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007725}
7726
7727/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 * Return string "str" in ' quotes, doubling ' characters.
7729 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 */
7732 static char_u *
7733string_quote(str, function)
7734 char_u *str;
7735 int function;
7736{
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 char_u *p, *r, *s;
7739
Bram Moolenaar33570922005-01-25 22:26:29 +00007740 len = (function ? 13 : 3);
7741 if (str != NULL)
7742 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007743 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007744 for (p = str; *p != NUL; mb_ptr_adv(p))
7745 if (*p == '\'')
7746 ++len;
7747 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007748 s = r = alloc(len);
7749 if (r != NULL)
7750 {
7751 if (function)
7752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007754 r += 10;
7755 }
7756 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 if (str != NULL)
7759 for (p = str; *p != NUL; )
7760 {
7761 if (*p == '\'')
7762 *r++ = '\'';
7763 MB_COPY_CHAR(p, r);
7764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766 if (function)
7767 *r++ = ')';
7768 *r++ = NUL;
7769 }
7770 return s;
7771}
7772
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007773#ifdef FEAT_FLOAT
7774/*
7775 * Convert the string "text" to a floating point number.
7776 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7777 * this always uses a decimal point.
7778 * Returns the length of the text that was consumed.
7779 */
7780 static int
7781string2float(text, value)
7782 char_u *text;
7783 float_T *value; /* result stored here */
7784{
7785 char *s = (char *)text;
7786 float_T f;
7787
7788 f = strtod(s, &s);
7789 *value = f;
7790 return (int)((char_u *)s - text);
7791}
7792#endif
7793
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 * Get the value of an environment variable.
7796 * "arg" is pointing to the '$'. It is advanced to after the name.
7797 * If the environment variable was not set, silently assume it is empty.
7798 * Always return OK.
7799 */
7800 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 int evaluate;
7805{
7806 char_u *string = NULL;
7807 int len;
7808 int cc;
7809 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007810 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812 ++*arg;
7813 name = *arg;
7814 len = get_env_len(arg);
7815 if (evaluate)
7816 {
7817 if (len != 0)
7818 {
7819 cc = name[len];
7820 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007821 /* first try vim_getenv(), fast for normal environment vars */
7822 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824 {
7825 if (!mustfree)
7826 string = vim_strsave(string);
7827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 else
7829 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007830 if (mustfree)
7831 vim_free(string);
7832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 /* next try expanding things like $VIM and ${HOME} */
7834 string = expand_env_save(name - 1);
7835 if (string != NULL && *string == '$')
7836 {
7837 vim_free(string);
7838 string = NULL;
7839 }
7840 }
7841 name[len] = cc;
7842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 rettv->v_type = VAR_STRING;
7844 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846
7847 return OK;
7848}
7849
7850/*
7851 * Array with names and number of arguments of all internal functions
7852 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7853 */
7854static struct fst
7855{
7856 char *f_name; /* function name */
7857 char f_min_argc; /* minimal number of arguments */
7858 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007860 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861} functions[] =
7862{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007863#ifdef FEAT_FLOAT
7864 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007865 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007867 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007868 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"append", 2, 2, f_append},
7870 {"argc", 0, 0, f_argc},
7871 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007872 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007874 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007875 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007876 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007879 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {"bufexists", 1, 1, f_bufexists},
7881 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7882 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7883 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7884 {"buflisted", 1, 1, f_buflisted},
7885 {"bufloaded", 1, 1, f_bufloaded},
7886 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007887 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"bufwinnr", 1, 1, f_bufwinnr},
7889 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007890 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007891 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007893#ifdef FEAT_FLOAT
7894 {"ceil", 1, 1, f_ceil},
7895#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007896 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007897 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007899 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007901#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007902 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007903 {"complete_add", 1, 1, f_complete_add},
7904 {"complete_check", 0, 0, f_complete_check},
7905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007907 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908#ifdef FEAT_FLOAT
7909 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007910 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007911#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007914 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007915 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"delete", 1, 1, f_delete},
7917 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007918 {"diff_filler", 1, 1, f_diff_filler},
7919 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007920 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"eventhandler", 0, 0, f_eventhandler},
7924 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007925 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007927#ifdef FEAT_FLOAT
7928 {"exp", 1, 1, f_exp},
7929#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007930 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007931 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007932 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7934 {"filereadable", 1, 1, f_filereadable},
7935 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007936 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007937 {"finddir", 1, 3, f_finddir},
7938 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007939#ifdef FEAT_FLOAT
7940 {"float2nr", 1, 1, f_float2nr},
7941 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007942 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007943#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007944 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"fnamemodify", 2, 2, f_fnamemodify},
7946 {"foldclosed", 1, 1, f_foldclosed},
7947 {"foldclosedend", 1, 1, f_foldclosedend},
7948 {"foldlevel", 1, 1, f_foldlevel},
7949 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007950 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007952 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007953 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007954 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007955 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007956 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"getchar", 0, 1, f_getchar},
7958 {"getcharmod", 0, 0, f_getcharmod},
7959 {"getcmdline", 0, 0, f_getcmdline},
7960 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007961 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007963 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007964 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"getfsize", 1, 1, f_getfsize},
7966 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007967 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007968 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007969 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007970 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007971 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007972 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007973 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007974 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007976 {"gettabvar", 2, 3, f_gettabvar},
7977 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"getwinposx", 0, 0, f_getwinposx},
7979 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007980 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007981 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007982 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007985 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007986 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7988 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7989 {"histadd", 2, 2, f_histadd},
7990 {"histdel", 1, 2, f_histdel},
7991 {"histget", 1, 2, f_histget},
7992 {"histnr", 1, 1, f_histnr},
7993 {"hlID", 1, 1, f_hlID},
7994 {"hlexists", 1, 1, f_hlexists},
7995 {"hostname", 0, 0, f_hostname},
7996 {"iconv", 3, 3, f_iconv},
7997 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007999 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008001 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {"inputrestore", 0, 0, f_inputrestore},
8003 {"inputsave", 0, 0, f_inputsave},
8004 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008006 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008008 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {"libcall", 3, 3, f_libcall},
8015 {"libcallnr", 3, 3, f_libcallnr},
8016 {"line", 1, 1, f_line},
8017 {"line2byte", 1, 1, f_line2byte},
8018 {"lispindent", 1, 1, f_lispindent},
8019 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008021 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008022 {"log10", 1, 1, f_log10},
8023#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008024#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008025 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008026#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008027 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008028 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008029 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008030 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008031 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008032 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008033 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008034 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008035 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008036 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008037 {"max", 1, 1, f_max},
8038 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008039#ifdef vim_mkdir
8040 {"mkdir", 1, 3, f_mkdir},
8041#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008043#ifdef FEAT_MZSCHEME
8044 {"mzeval", 1, 1, f_mzeval},
8045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008047 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008048 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008049 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050#ifdef FEAT_FLOAT
8051 {"pow", 2, 2, f_pow},
8052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008054 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008055 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008056#ifdef FEAT_PYTHON3
8057 {"py3eval", 1, 1, f_py3eval},
8058#endif
8059#ifdef FEAT_PYTHON
8060 {"pyeval", 1, 1, f_pyeval},
8061#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008062 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008063 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008064 {"reltime", 0, 2, f_reltime},
8065 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"remote_expr", 2, 3, f_remote_expr},
8067 {"remote_foreground", 1, 1, f_remote_foreground},
8068 {"remote_peek", 1, 2, f_remote_peek},
8069 {"remote_read", 1, 1, f_remote_read},
8070 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008071 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008073 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008075 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
8077 {"round", 1, 1, f_round},
8078#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008079 {"screenattr", 2, 2, f_screenattr},
8080 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008081 {"screencol", 0, 0, f_screencol},
8082 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008083 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008084 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008085 {"searchpair", 3, 7, f_searchpair},
8086 {"searchpairpos", 3, 7, f_searchpairpos},
8087 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"server2client", 2, 2, f_server2client},
8089 {"serverlist", 0, 0, f_serverlist},
8090 {"setbufvar", 3, 3, f_setbufvar},
8091 {"setcmdpos", 1, 1, f_setcmdpos},
8092 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008093 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008095 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008096 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008098 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008099 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008101#ifdef FEAT_CRYPT
8102 {"sha256", 1, 1, f_sha256},
8103#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008104 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008105 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#ifdef FEAT_FLOAT
8108 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008109 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008110#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008111 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008112 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008113 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008114 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008115 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#ifdef FEAT_FLOAT
8117 {"sqrt", 1, 1, f_sqrt},
8118 {"str2float", 1, 1, f_str2float},
8119#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008120 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008121 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008122 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123#ifdef HAVE_STRFTIME
8124 {"strftime", 1, 2, f_strftime},
8125#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008127 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"strlen", 1, 1, f_strlen},
8129 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008130 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008132 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008133 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"substitute", 4, 4, f_substitute},
8135 {"synID", 3, 3, f_synID},
8136 {"synIDattr", 2, 3, f_synIDattr},
8137 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008138 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008139 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008140 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008141 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008142 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008143 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008144 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008145 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008146#ifdef FEAT_FLOAT
8147 {"tan", 1, 1, f_tan},
8148 {"tanh", 1, 1, f_tanh},
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008151 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {"tolower", 1, 1, f_tolower},
8153 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008154 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155#ifdef FEAT_FLOAT
8156 {"trunc", 1, 1, f_trunc},
8157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008159 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008160 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008161 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008162 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"virtcol", 1, 1, f_virtcol},
8164 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008165 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"winbufnr", 1, 1, f_winbufnr},
8167 {"wincol", 0, 0, f_wincol},
8168 {"winheight", 1, 1, f_winheight},
8169 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008170 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008172 {"winrestview", 1, 1, f_winrestview},
8173 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008175 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008176 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177};
8178
8179#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8180
8181/*
8182 * Function given to ExpandGeneric() to obtain the list of internal
8183 * or user defined function names.
8184 */
8185 char_u *
8186get_function_name(xp, idx)
8187 expand_T *xp;
8188 int idx;
8189{
8190 static int intidx = -1;
8191 char_u *name;
8192
8193 if (idx == 0)
8194 intidx = -1;
8195 if (intidx < 0)
8196 {
8197 name = get_user_func_name(xp, idx);
8198 if (name != NULL)
8199 return name;
8200 }
8201 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8202 {
8203 STRCPY(IObuff, functions[intidx].f_name);
8204 STRCAT(IObuff, "(");
8205 if (functions[intidx].f_max_argc == 0)
8206 STRCAT(IObuff, ")");
8207 return IObuff;
8208 }
8209
8210 return NULL;
8211}
8212
8213/*
8214 * Function given to ExpandGeneric() to obtain the list of internal or
8215 * user defined variable or function names.
8216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 char_u *
8218get_expr_name(xp, idx)
8219 expand_T *xp;
8220 int idx;
8221{
8222 static int intidx = -1;
8223 char_u *name;
8224
8225 if (idx == 0)
8226 intidx = -1;
8227 if (intidx < 0)
8228 {
8229 name = get_function_name(xp, idx);
8230 if (name != NULL)
8231 return name;
8232 }
8233 return get_user_var_name(xp, ++intidx);
8234}
8235
8236#endif /* FEAT_CMDL_COMPL */
8237
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008238#if defined(EBCDIC) || defined(PROTO)
8239/*
8240 * Compare struct fst by function name.
8241 */
8242 static int
8243compare_func_name(s1, s2)
8244 const void *s1;
8245 const void *s2;
8246{
8247 struct fst *p1 = (struct fst *)s1;
8248 struct fst *p2 = (struct fst *)s2;
8249
8250 return STRCMP(p1->f_name, p2->f_name);
8251}
8252
8253/*
8254 * Sort the function table by function name.
8255 * The sorting of the table above is ASCII dependant.
8256 * On machines using EBCDIC we have to sort it.
8257 */
8258 static void
8259sortFunctions()
8260{
8261 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8262
8263 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8264}
8265#endif
8266
8267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268/*
8269 * Find internal function in table above.
8270 * Return index, or -1 if not found
8271 */
8272 static int
8273find_internal_func(name)
8274 char_u *name; /* name of the function */
8275{
8276 int first = 0;
8277 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8278 int cmp;
8279 int x;
8280
8281 /*
8282 * Find the function name in the table. Binary search.
8283 */
8284 while (first <= last)
8285 {
8286 x = first + ((unsigned)(last - first) >> 1);
8287 cmp = STRCMP(name, functions[x].f_name);
8288 if (cmp < 0)
8289 last = x - 1;
8290 else if (cmp > 0)
8291 first = x + 1;
8292 else
8293 return x;
8294 }
8295 return -1;
8296}
8297
8298/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8300 * name it contains, otherwise return "name".
8301 */
8302 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008303deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008304 char_u *name;
8305 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008306 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008307{
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 int cc;
8310
8311 cc = name[*lenp];
8312 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008313 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 {
8319 *lenp = 0;
8320 return (char_u *)""; /* just in case */
8321 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008322 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008324 }
8325
8326 return name;
8327}
8328
8329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 * Allocate a variable for the result of a function.
8331 * Return OK or FAIL.
8332 */
8333 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008334get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8335 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 char_u *name; /* name of the function */
8337 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 char_u **arg; /* argument, pointing to the '(' */
8340 linenr_T firstline; /* first line of range */
8341 linenr_T lastline; /* last line of range */
8342 int *doesrange; /* return: function handled range */
8343 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 char_u *argp;
8347 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008348 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 int argcount = 0; /* number of arguments found */
8350
8351 /*
8352 * Get the arguments.
8353 */
8354 argp = *arg;
8355 while (argcount < MAX_FUNC_ARGS)
8356 {
8357 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8358 if (*argp == ')' || *argp == ',' || *argp == NUL)
8359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8361 {
8362 ret = FAIL;
8363 break;
8364 }
8365 ++argcount;
8366 if (*argp != ',')
8367 break;
8368 }
8369 if (*argp == ')')
8370 ++argp;
8371 else
8372 ret = FAIL;
8373
8374 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008376 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008378 {
8379 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008380 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008381 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008382 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384
8385 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
8388 *arg = skipwhite(argp);
8389 return ret;
8390}
8391
8392
8393/*
8394 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008395 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008396 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 */
8398 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008399call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008400 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008401 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008403 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008405 typval_T *argvars; /* vars for arguments, must have "argcount"
8406 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 linenr_T firstline; /* first line of range */
8408 linenr_T lastline; /* last line of range */
8409 int *doesrange; /* return: function handled range */
8410 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008411 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412{
8413 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414#define ERROR_UNKNOWN 0
8415#define ERROR_TOOMANY 1
8416#define ERROR_TOOFEW 2
8417#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008418#define ERROR_DICT 4
8419#define ERROR_NONE 5
8420#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 int error = ERROR_NONE;
8422 int i;
8423 int llen;
8424 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425#define FLEN_FIXED 40
8426 char_u fname_buf[FLEN_FIXED + 1];
8427 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008428 char_u *name;
8429
8430 /* Make a copy of the name, if it comes from a funcref variable it could
8431 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008432 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008433 if (name == NULL)
8434 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 /*
8437 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8438 * Change <SNR>123_name() to K_SNR 123_name().
8439 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 llen = eval_fname_script(name);
8442 if (llen > 0)
8443 {
8444 fname_buf[0] = K_SPECIAL;
8445 fname_buf[1] = KS_EXTRA;
8446 fname_buf[2] = (int)KE_SNR;
8447 i = 3;
8448 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8449 {
8450 if (current_SID <= 0)
8451 error = ERROR_SCRIPT;
8452 else
8453 {
8454 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8455 i = (int)STRLEN(fname_buf);
8456 }
8457 }
8458 if (i + STRLEN(name + llen) < FLEN_FIXED)
8459 {
8460 STRCPY(fname_buf + i, name + llen);
8461 fname = fname_buf;
8462 }
8463 else
8464 {
8465 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8466 if (fname == NULL)
8467 error = ERROR_OTHER;
8468 else
8469 {
8470 mch_memmove(fname, fname_buf, (size_t)i);
8471 STRCPY(fname + i, name + llen);
8472 }
8473 }
8474 }
8475 else
8476 fname = name;
8477
8478 *doesrange = FALSE;
8479
8480
8481 /* execute the function if no errors detected and executing */
8482 if (evaluate && error == ERROR_NONE)
8483 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008484 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8485 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_UNKNOWN;
8487
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008488 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
8490 /*
8491 * User defined function.
8492 */
8493 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008496 /* Trigger FuncUndefined event, may load the function. */
8497 if (fp == NULL
8498 && apply_autocmds(EVENT_FUNCUNDEFINED,
8499 fname, fname, TRUE, NULL)
8500 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008502 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 fp = find_func(fname);
8504 }
8505#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008506 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008507 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008508 {
8509 /* loaded a package, search for the function again */
8510 fp = find_func(fname);
8511 }
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (fp != NULL)
8514 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008515 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008517 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008519 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008521 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 else
8524 {
8525 /*
8526 * Call the user function.
8527 * Save and restore search patterns, script variables and
8528 * redo buffer.
8529 */
8530 save_search_patterns();
8531 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008533 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008534 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008535 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8536 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8537 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008538 /* Function was unreferenced while being used, free it
8539 * now. */
8540 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 restoreRedobuff();
8542 restore_search_patterns();
8543 error = ERROR_NONE;
8544 }
8545 }
8546 }
8547 else
8548 {
8549 /*
8550 * Find the function name in the table, call its implementation.
8551 */
8552 i = find_internal_func(fname);
8553 if (i >= 0)
8554 {
8555 if (argcount < functions[i].f_min_argc)
8556 error = ERROR_TOOFEW;
8557 else if (argcount > functions[i].f_max_argc)
8558 error = ERROR_TOOMANY;
8559 else
8560 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008561 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008562 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 error = ERROR_NONE;
8564 }
8565 }
8566 }
8567 /*
8568 * The function call (or "FuncUndefined" autocommand sequence) might
8569 * have been aborted by an error, an interrupt, or an explicitly thrown
8570 * exception that has not been caught so far. This situation can be
8571 * tested for by calling aborting(). For an error in an internal
8572 * function or for the "E132" error in call_user_func(), however, the
8573 * throw point at which the "force_abort" flag (temporarily reset by
8574 * emsg()) is normally updated has not been reached yet. We need to
8575 * update that flag first to make aborting() reliable.
8576 */
8577 update_force_abort();
8578 }
8579 if (error == ERROR_NONE)
8580 ret = OK;
8581
8582 /*
8583 * Report an error unless the argument evaluation or function call has been
8584 * cancelled due to an aborting error, an interrupt, or an exception.
8585 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008586 if (!aborting())
8587 {
8588 switch (error)
8589 {
8590 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008591 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008592 break;
8593 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008595 break;
8596 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008597 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008598 name);
8599 break;
8600 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008601 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008602 name);
8603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008604 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008605 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008606 name);
8607 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008608 }
8609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (fname != name && fname != fname_buf)
8612 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008613 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614
8615 return ret;
8616}
8617
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008618/*
8619 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008620 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008621 */
8622 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008623emsg_funcname(ermsg, name)
8624 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 char_u *name;
8626{
8627 char_u *p;
8628
8629 if (*name == K_SPECIAL)
8630 p = concat_str((char_u *)"<SNR>", name + 3);
8631 else
8632 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008633 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008634 if (p != name)
8635 vim_free(p);
8636}
8637
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008638/*
8639 * Return TRUE for a non-zero Number and a non-empty String.
8640 */
8641 static int
8642non_zero_arg(argvars)
8643 typval_T *argvars;
8644{
8645 return ((argvars[0].v_type == VAR_NUMBER
8646 && argvars[0].vval.v_number != 0)
8647 || (argvars[0].v_type == VAR_STRING
8648 && argvars[0].vval.v_string != NULL
8649 && *argvars[0].vval.v_string != NUL));
8650}
8651
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652/*********************************************
8653 * Implementation of the built-in functions
8654 */
8655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008656#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008657static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8658
8659/*
8660 * Get the float value of "argvars[0]" into "f".
8661 * Returns FAIL when the argument is not a Number or Float.
8662 */
8663 static int
8664get_float_arg(argvars, f)
8665 typval_T *argvars;
8666 float_T *f;
8667{
8668 if (argvars[0].v_type == VAR_FLOAT)
8669 {
8670 *f = argvars[0].vval.v_float;
8671 return OK;
8672 }
8673 if (argvars[0].v_type == VAR_NUMBER)
8674 {
8675 *f = (float_T)argvars[0].vval.v_number;
8676 return OK;
8677 }
8678 EMSG(_("E808: Number or Float required"));
8679 return FAIL;
8680}
8681
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008682/*
8683 * "abs(expr)" function
8684 */
8685 static void
8686f_abs(argvars, rettv)
8687 typval_T *argvars;
8688 typval_T *rettv;
8689{
8690 if (argvars[0].v_type == VAR_FLOAT)
8691 {
8692 rettv->v_type = VAR_FLOAT;
8693 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8694 }
8695 else
8696 {
8697 varnumber_T n;
8698 int error = FALSE;
8699
8700 n = get_tv_number_chk(&argvars[0], &error);
8701 if (error)
8702 rettv->vval.v_number = -1;
8703 else if (n > 0)
8704 rettv->vval.v_number = n;
8705 else
8706 rettv->vval.v_number = -n;
8707 }
8708}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008709
8710/*
8711 * "acos()" function
8712 */
8713 static void
8714f_acos(argvars, rettv)
8715 typval_T *argvars;
8716 typval_T *rettv;
8717{
8718 float_T f;
8719
8720 rettv->v_type = VAR_FLOAT;
8721 if (get_float_arg(argvars, &f) == OK)
8722 rettv->vval.v_float = acos(f);
8723 else
8724 rettv->vval.v_float = 0.0;
8725}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#endif
8727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 */
8731 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 typval_T *argvars;
8734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008738 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008739 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008741 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008742 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008743 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008744 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008745 }
8746 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 EMSG(_(e_listreq));
8748}
8749
8750/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008751 * "and(expr, expr)" function
8752 */
8753 static void
8754f_and(argvars, rettv)
8755 typval_T *argvars;
8756 typval_T *rettv;
8757{
8758 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8759 & get_tv_number_chk(&argvars[1], NULL);
8760}
8761
8762/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008763 * "append(lnum, string/list)" function
8764 */
8765 static void
8766f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008769{
8770 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008771 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008772 list_T *l = NULL;
8773 listitem_T *li = NULL;
8774 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008775 long added = 0;
8776
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008777 /* When coming here from Insert mode, sync undo, so that this can be
8778 * undone separately from what was previously inserted. */
8779 if (u_sync_once == 2)
8780 {
8781 u_sync_once = 1; /* notify that u_sync() was called */
8782 u_sync(TRUE);
8783 }
8784
Bram Moolenaar0d660222005-01-07 21:51:51 +00008785 lnum = get_tv_lnum(argvars);
8786 if (lnum >= 0
8787 && lnum <= curbuf->b_ml.ml_line_count
8788 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008789 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008790 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008791 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008792 l = argvars[1].vval.v_list;
8793 if (l == NULL)
8794 return;
8795 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008796 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008797 for (;;)
8798 {
8799 if (l == NULL)
8800 tv = &argvars[1]; /* append a string */
8801 else if (li == NULL)
8802 break; /* end of list */
8803 else
8804 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008805 line = get_tv_string_chk(tv);
8806 if (line == NULL) /* type error */
8807 {
8808 rettv->vval.v_number = 1; /* Failed */
8809 break;
8810 }
8811 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008812 ++added;
8813 if (l == NULL)
8814 break;
8815 li = li->li_next;
8816 }
8817
8818 appended_lines_mark(lnum, added);
8819 if (curwin->w_cursor.lnum > lnum)
8820 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008822 else
8823 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
8826/*
8827 * "argc()" function
8828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008830f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008831 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
8837/*
8838 * "argidx()" function
8839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008841f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008842 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846}
8847
8848/*
8849 * "argv(nr)" function
8850 */
8851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008852f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 typval_T *argvars;
8854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 int idx;
8857
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008858 if (argvars[0].v_type != VAR_UNKNOWN)
8859 {
8860 idx = get_tv_number_chk(&argvars[0], NULL);
8861 if (idx >= 0 && idx < ARGCOUNT)
8862 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8863 else
8864 rettv->vval.v_string = NULL;
8865 rettv->v_type = VAR_STRING;
8866 }
8867 else if (rettv_list_alloc(rettv) == OK)
8868 for (idx = 0; idx < ARGCOUNT; ++idx)
8869 list_append_string(rettv->vval.v_list,
8870 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871}
8872
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008873#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008874/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008875 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008876 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008877 static void
8878f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008879 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008880 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008881{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008882 float_T f;
8883
8884 rettv->v_type = VAR_FLOAT;
8885 if (get_float_arg(argvars, &f) == OK)
8886 rettv->vval.v_float = asin(f);
8887 else
8888 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889}
8890
8891/*
8892 * "atan()" function
8893 */
8894 static void
8895f_atan(argvars, rettv)
8896 typval_T *argvars;
8897 typval_T *rettv;
8898{
8899 float_T f;
8900
8901 rettv->v_type = VAR_FLOAT;
8902 if (get_float_arg(argvars, &f) == OK)
8903 rettv->vval.v_float = atan(f);
8904 else
8905 rettv->vval.v_float = 0.0;
8906}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008907
8908/*
8909 * "atan2()" function
8910 */
8911 static void
8912f_atan2(argvars, rettv)
8913 typval_T *argvars;
8914 typval_T *rettv;
8915{
8916 float_T fx, fy;
8917
8918 rettv->v_type = VAR_FLOAT;
8919 if (get_float_arg(argvars, &fx) == OK
8920 && get_float_arg(&argvars[1], &fy) == OK)
8921 rettv->vval.v_float = atan2(fx, fy);
8922 else
8923 rettv->vval.v_float = 0.0;
8924}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008925#endif
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927/*
8928 * "browse(save, title, initdir, default)" function
8929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935#ifdef FEAT_BROWSE
8936 int save;
8937 char_u *title;
8938 char_u *initdir;
8939 char_u *defname;
8940 char_u buf[NUMBUFLEN];
8941 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 save = get_tv_number_chk(&argvars[0], &error);
8945 title = get_tv_string_chk(&argvars[1]);
8946 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8947 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 if (error || title == NULL || initdir == NULL || defname == NULL)
8950 rettv->vval.v_string = NULL;
8951 else
8952 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008953 do_browse(save ? BROWSE_SAVE : 0,
8954 title, defname, NULL, initdir, NULL, curbuf);
8955#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008959}
8960
8961/*
8962 * "browsedir(title, initdir)" function
8963 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008968{
8969#ifdef FEAT_BROWSE
8970 char_u *title;
8971 char_u *initdir;
8972 char_u buf[NUMBUFLEN];
8973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008974 title = get_tv_string_chk(&argvars[0]);
8975 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008977 if (title == NULL || initdir == NULL)
8978 rettv->vval.v_string = NULL;
8979 else
8980 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008981 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986}
8987
Bram Moolenaar33570922005-01-25 22:26:29 +00008988static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990/*
8991 * Find a buffer by number or exact name.
8992 */
8993 static buf_T *
8994find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 if (avar->v_type == VAR_NUMBER)
9000 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009001 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009004 if (buf == NULL)
9005 {
9006 /* No full path name match, try a match with a URL or a "nofile"
9007 * buffer, these don't use the full path. */
9008 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9009 if (buf->b_fname != NULL
9010 && (path_with_url(buf->b_fname)
9011#ifdef FEAT_QUICKFIX
9012 || bt_nofile(buf)
9013#endif
9014 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009016 break;
9017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 }
9019 return buf;
9020}
9021
9022/*
9023 * "bufexists(expr)" function
9024 */
9025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 typval_T *argvars;
9028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031}
9032
9033/*
9034 * "buflisted(expr)" function
9035 */
9036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009037f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009038 typval_T *argvars;
9039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
9041 buf_T *buf;
9042
9043 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045}
9046
9047/*
9048 * "bufloaded(expr)" function
9049 */
9050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 typval_T *argvars;
9053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 buf_T *buf;
9056
9057 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
9060
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009061static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009062
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063/*
9064 * Get buffer by number or pattern.
9065 */
9066 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009067get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009069 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int save_magic;
9073 char_u *save_cpo;
9074 buf_T *buf;
9075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 if (tv->v_type == VAR_NUMBER)
9077 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009078 if (tv->v_type != VAR_STRING)
9079 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 if (name == NULL || *name == NUL)
9081 return curbuf;
9082 if (name[0] == '$' && name[1] == NUL)
9083 return lastbuf;
9084
9085 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9086 save_magic = p_magic;
9087 p_magic = TRUE;
9088 save_cpo = p_cpo;
9089 p_cpo = (char_u *)"";
9090
9091 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009092 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093
9094 p_magic = save_magic;
9095 p_cpo = save_cpo;
9096
9097 /* If not found, try expanding the name, like done for bufexists(). */
9098 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100
9101 return buf;
9102}
9103
9104/*
9105 * "bufname(expr)" function
9106 */
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *argvars;
9110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 buf_T *buf;
9113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009116 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 --emsg_off;
9123}
9124
9125/*
9126 * "bufnr(expr)" function
9127 */
9128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009130 typval_T *argvars;
9131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009134 int error = FALSE;
9135 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009139 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009140 --emsg_off;
9141
9142 /* If the buffer isn't found and the second argument is not zero create a
9143 * new buffer. */
9144 if (buf == NULL
9145 && argvars[1].v_type != VAR_UNKNOWN
9146 && get_tv_number_chk(&argvars[1], &error) != 0
9147 && !error
9148 && (name = get_tv_string_chk(&argvars[0])) != NULL
9149 && !error)
9150 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9151
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
9157
9158/*
9159 * "bufwinnr(nr)" function
9160 */
9161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 typval_T *argvars;
9164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166#ifdef FEAT_WINDOWS
9167 win_T *wp;
9168 int winnr = 0;
9169#endif
9170 buf_T *buf;
9171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009174 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#ifdef FEAT_WINDOWS
9176 for (wp = firstwin; wp; wp = wp->w_next)
9177 {
9178 ++winnr;
9179 if (wp->w_buffer == buf)
9180 break;
9181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#endif
9186 --emsg_off;
9187}
9188
9189/*
9190 * "byte2line(byte)" function
9191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009194 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
9197#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199#else
9200 long boff = 0;
9201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 (linenr_T)0, &boff);
9208#endif
9209}
9210
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009211 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009212byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009213 typval_T *argvars;
9214 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009215 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009216{
9217#ifdef FEAT_MBYTE
9218 char_u *t;
9219#endif
9220 char_u *str;
9221 long idx;
9222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223 str = get_tv_string_chk(&argvars[0]);
9224 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009225 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009226 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009227 return;
9228
9229#ifdef FEAT_MBYTE
9230 t = str;
9231 for ( ; idx > 0; idx--)
9232 {
9233 if (*t == NUL) /* EOL reached */
9234 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009235 if (enc_utf8 && comp)
9236 t += utf_ptr2len(t);
9237 else
9238 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009239 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009240 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009241#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009242 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009244#endif
9245}
9246
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009247/*
9248 * "byteidx()" function
9249 */
9250 static void
9251f_byteidx(argvars, rettv)
9252 typval_T *argvars;
9253 typval_T *rettv;
9254{
9255 byteidx(argvars, rettv, FALSE);
9256}
9257
9258/*
9259 * "byteidxcomp()" function
9260 */
9261 static void
9262f_byteidxcomp(argvars, rettv)
9263 typval_T *argvars;
9264 typval_T *rettv;
9265{
9266 byteidx(argvars, rettv, TRUE);
9267}
9268
Bram Moolenaardb913952012-06-29 12:54:53 +02009269 int
9270func_call(name, args, selfdict, rettv)
9271 char_u *name;
9272 typval_T *args;
9273 dict_T *selfdict;
9274 typval_T *rettv;
9275{
9276 listitem_T *item;
9277 typval_T argv[MAX_FUNC_ARGS + 1];
9278 int argc = 0;
9279 int dummy;
9280 int r = 0;
9281
9282 for (item = args->vval.v_list->lv_first; item != NULL;
9283 item = item->li_next)
9284 {
9285 if (argc == MAX_FUNC_ARGS)
9286 {
9287 EMSG(_("E699: Too many arguments"));
9288 break;
9289 }
9290 /* Make a copy of each argument. This is needed to be able to set
9291 * v_lock to VAR_FIXED in the copy without changing the original list.
9292 */
9293 copy_tv(&item->li_tv, &argv[argc++]);
9294 }
9295
9296 if (item == NULL)
9297 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9298 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9299 &dummy, TRUE, selfdict);
9300
9301 /* Free the arguments. */
9302 while (argc > 0)
9303 clear_tv(&argv[--argc]);
9304
9305 return r;
9306}
9307
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009308/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009309 * "call(func, arglist)" function
9310 */
9311 static void
9312f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009315{
9316 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009317 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009318
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319 if (argvars[1].v_type != VAR_LIST)
9320 {
9321 EMSG(_(e_listreq));
9322 return;
9323 }
9324 if (argvars[1].vval.v_list == NULL)
9325 return;
9326
9327 if (argvars[0].v_type == VAR_FUNC)
9328 func = argvars[0].vval.v_string;
9329 else
9330 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 if (*func == NUL)
9332 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009333
Bram Moolenaare9a41262005-01-15 22:18:47 +00009334 if (argvars[2].v_type != VAR_UNKNOWN)
9335 {
9336 if (argvars[2].v_type != VAR_DICT)
9337 {
9338 EMSG(_(e_dictreq));
9339 return;
9340 }
9341 selfdict = argvars[2].vval.v_dict;
9342 }
9343
Bram Moolenaardb913952012-06-29 12:54:53 +02009344 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009345}
9346
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009347#ifdef FEAT_FLOAT
9348/*
9349 * "ceil({float})" function
9350 */
9351 static void
9352f_ceil(argvars, rettv)
9353 typval_T *argvars;
9354 typval_T *rettv;
9355{
9356 float_T f;
9357
9358 rettv->v_type = VAR_FLOAT;
9359 if (get_float_arg(argvars, &f) == OK)
9360 rettv->vval.v_float = ceil(f);
9361 else
9362 rettv->vval.v_float = 0.0;
9363}
9364#endif
9365
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009366/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009367 * "changenr()" function
9368 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009369 static void
9370f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009371 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009372 typval_T *rettv;
9373{
9374 rettv->vval.v_number = curbuf->b_u_seq_cur;
9375}
9376
9377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 * "char2nr(string)" function
9379 */
9380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385#ifdef FEAT_MBYTE
9386 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009387 {
9388 int utf8 = 0;
9389
9390 if (argvars[1].v_type != VAR_UNKNOWN)
9391 utf8 = get_tv_number_chk(&argvars[1], NULL);
9392
9393 if (utf8)
9394 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9395 else
9396 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 else
9399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401}
9402
9403/*
9404 * "cindent(lnum)" function
9405 */
9406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410{
9411#ifdef FEAT_CINDENT
9412 pos_T pos;
9413 linenr_T lnum;
9414
9415 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9418 {
9419 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 curwin->w_cursor = pos;
9422 }
9423 else
9424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009429 * "clearmatches()" function
9430 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009431 static void
9432f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009433 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009434 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009435{
9436#ifdef FEAT_SEARCH_EXTRA
9437 clear_matches(curwin);
9438#endif
9439}
9440
9441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 * "col(string)" function
9443 */
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 colnr_T col = 0;
9450 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009451 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009453 fp = var2fpos(&argvars[0], FALSE, &fnum);
9454 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 {
9456 if (fp->col == MAXCOL)
9457 {
9458 /* '> can be MAXCOL, get the length of the line then */
9459 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009460 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 else
9462 col = MAXCOL;
9463 }
9464 else
9465 {
9466 col = fp->col + 1;
9467#ifdef FEAT_VIRTUALEDIT
9468 /* col(".") when the cursor is on the NUL at the end of the line
9469 * because of "coladd" can be seen as an extra column. */
9470 if (virtual_active() && fp == &curwin->w_cursor)
9471 {
9472 char_u *p = ml_get_cursor();
9473
9474 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9475 curwin->w_virtcol - curwin->w_cursor.coladd))
9476 {
9477# ifdef FEAT_MBYTE
9478 int l;
9479
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009480 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 col += l;
9482# else
9483 if (*p != NUL && p[1] == NUL)
9484 ++col;
9485# endif
9486 }
9487 }
9488#endif
9489 }
9490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492}
9493
Bram Moolenaar572cb562005-08-05 21:35:02 +00009494#if defined(FEAT_INS_EXPAND)
9495/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009496 * "complete()" function
9497 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009498 static void
9499f_complete(argvars, rettv)
9500 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009501 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009502{
9503 int startcol;
9504
9505 if ((State & INSERT) == 0)
9506 {
9507 EMSG(_("E785: complete() can only be used in Insert mode"));
9508 return;
9509 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009510
9511 /* Check for undo allowed here, because if something was already inserted
9512 * the line was already saved for undo and this check isn't done. */
9513 if (!undo_allowed())
9514 return;
9515
Bram Moolenaarade00832006-03-10 21:46:58 +00009516 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9517 {
9518 EMSG(_(e_invarg));
9519 return;
9520 }
9521
9522 startcol = get_tv_number_chk(&argvars[0], NULL);
9523 if (startcol <= 0)
9524 return;
9525
9526 set_completion(startcol - 1, argvars[1].vval.v_list);
9527}
9528
9529/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009530 * "complete_add()" function
9531 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009532 static void
9533f_complete_add(argvars, rettv)
9534 typval_T *argvars;
9535 typval_T *rettv;
9536{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009537 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009538}
9539
9540/*
9541 * "complete_check()" function
9542 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 static void
9544f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009545 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009546 typval_T *rettv;
9547{
9548 int saved = RedrawingDisabled;
9549
9550 RedrawingDisabled = 0;
9551 ins_compl_check_keys(0);
9552 rettv->vval.v_number = compl_interrupted;
9553 RedrawingDisabled = saved;
9554}
9555#endif
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557/*
9558 * "confirm(message, buttons[, default [, type]])" function
9559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009561f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009562 typval_T *argvars UNUSED;
9563 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9566 char_u *message;
9567 char_u *buttons = NULL;
9568 char_u buf[NUMBUFLEN];
9569 char_u buf2[NUMBUFLEN];
9570 int def = 1;
9571 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 char_u *typestr;
9573 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 message = get_tv_string_chk(&argvars[0]);
9576 if (message == NULL)
9577 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009578 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9581 if (buttons == NULL)
9582 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009586 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9589 if (typestr == NULL)
9590 error = TRUE;
9591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 switch (TOUPPER_ASC(*typestr))
9594 {
9595 case 'E': type = VIM_ERROR; break;
9596 case 'Q': type = VIM_QUESTION; break;
9597 case 'I': type = VIM_INFO; break;
9598 case 'W': type = VIM_WARNING; break;
9599 case 'G': type = VIM_GENERIC; break;
9600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 }
9602 }
9603 }
9604 }
9605
9606 if (buttons == NULL || *buttons == NUL)
9607 buttons = (char_u *)_("&Ok");
9608
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009609 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009610 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009611 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612#endif
9613}
9614
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615/*
9616 * "copy()" function
9617 */
9618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 typval_T *argvars;
9621 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009622{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009623 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009626#ifdef FEAT_FLOAT
9627/*
9628 * "cos()" function
9629 */
9630 static void
9631f_cos(argvars, rettv)
9632 typval_T *argvars;
9633 typval_T *rettv;
9634{
9635 float_T f;
9636
9637 rettv->v_type = VAR_FLOAT;
9638 if (get_float_arg(argvars, &f) == OK)
9639 rettv->vval.v_float = cos(f);
9640 else
9641 rettv->vval.v_float = 0.0;
9642}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009643
9644/*
9645 * "cosh()" function
9646 */
9647 static void
9648f_cosh(argvars, rettv)
9649 typval_T *argvars;
9650 typval_T *rettv;
9651{
9652 float_T f;
9653
9654 rettv->v_type = VAR_FLOAT;
9655 if (get_float_arg(argvars, &f) == OK)
9656 rettv->vval.v_float = cosh(f);
9657 else
9658 rettv->vval.v_float = 0.0;
9659}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009660#endif
9661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009663 * "count()" function
9664 */
9665 static void
9666f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009667 typval_T *argvars;
9668 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009669{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009670 long n = 0;
9671 int ic = FALSE;
9672
Bram Moolenaare9a41262005-01-15 22:18:47 +00009673 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009675 listitem_T *li;
9676 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009678
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 if ((l = argvars[0].vval.v_list) != NULL)
9680 {
9681 li = l->lv_first;
9682 if (argvars[2].v_type != VAR_UNKNOWN)
9683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009684 int error = FALSE;
9685
9686 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 if (argvars[3].v_type != VAR_UNKNOWN)
9688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 idx = get_tv_number_chk(&argvars[3], &error);
9690 if (!error)
9691 {
9692 li = list_find(l, idx);
9693 if (li == NULL)
9694 EMSGN(_(e_listidx), idx);
9695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 if (error)
9698 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009699 }
9700
9701 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009702 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009703 ++n;
9704 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009706 else if (argvars[0].v_type == VAR_DICT)
9707 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009708 int todo;
9709 dict_T *d;
9710 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009711
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009712 if ((d = argvars[0].vval.v_dict) != NULL)
9713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009714 int error = FALSE;
9715
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 if (argvars[2].v_type != VAR_UNKNOWN)
9717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009719 if (argvars[3].v_type != VAR_UNKNOWN)
9720 EMSG(_(e_invarg));
9721 }
9722
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009723 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009725 {
9726 if (!HASHITEM_EMPTY(hi))
9727 {
9728 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009729 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009730 ++n;
9731 }
9732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009733 }
9734 }
9735 else
9736 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009737 rettv->vval.v_number = n;
9738}
9739
9740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9742 *
9743 * Checks the existence of a cscope connection.
9744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009747 typval_T *argvars UNUSED;
9748 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750#ifdef FEAT_CSCOPE
9751 int num = 0;
9752 char_u *dbpath = NULL;
9753 char_u *prepend = NULL;
9754 char_u buf[NUMBUFLEN];
9755
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009756 if (argvars[0].v_type != VAR_UNKNOWN
9757 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 num = (int)get_tv_number(&argvars[0]);
9760 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009761 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 }
9764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#endif
9767}
9768
9769/*
9770 * "cursor(lnum, col)" function
9771 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009772 * Moves the cursor to the specified line and column.
9773 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009777 typval_T *argvars;
9778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009781#ifdef FEAT_VIRTUALEDIT
9782 long coladd = 0;
9783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009785 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009786 if (argvars[1].v_type == VAR_UNKNOWN)
9787 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009788 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009789
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009790 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009791 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009792 line = pos.lnum;
9793 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009794#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009795 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009796#endif
9797 }
9798 else
9799 {
9800 line = get_tv_lnum(argvars);
9801 col = get_tv_number_chk(&argvars[1], NULL);
9802#ifdef FEAT_VIRTUALEDIT
9803 if (argvars[2].v_type != VAR_UNKNOWN)
9804 coladd = get_tv_number_chk(&argvars[2], NULL);
9805#endif
9806 }
9807 if (line < 0 || col < 0
9808#ifdef FEAT_VIRTUALEDIT
9809 || coladd < 0
9810#endif
9811 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 if (line > 0)
9814 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 if (col > 0)
9816 curwin->w_cursor.col = col - 1;
9817#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009818 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819#endif
9820
9821 /* Make sure the cursor is in a valid position. */
9822 check_cursor();
9823#ifdef FEAT_MBYTE
9824 /* Correct cursor for multi-byte character. */
9825 if (has_mbyte)
9826 mb_adjust_cursor();
9827#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009828
9829 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831}
9832
9833/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009834 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 */
9836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009838 typval_T *argvars;
9839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009841 int noref = 0;
9842
9843 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009844 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009845 if (noref < 0 || noref > 1)
9846 EMSG(_(e_invarg));
9847 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009848 {
9849 current_copyID += COPYID_INC;
9850 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852}
9853
9854/*
9855 * "delete()" function
9856 */
9857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 typval_T *argvars;
9860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861{
9862 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866}
9867
9868/*
9869 * "did_filetype()" function
9870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009873 typval_T *argvars UNUSED;
9874 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
9876#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878#endif
9879}
9880
9881/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 * "diff_filler()" function
9883 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009886 typval_T *argvars UNUSED;
9887 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888{
9889#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009891#endif
9892}
9893
9894/*
9895 * "diff_hlID()" function
9896 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009899 typval_T *argvars UNUSED;
9900 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901{
9902#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009904 static linenr_T prev_lnum = 0;
9905 static int changedtick = 0;
9906 static int fnum = 0;
9907 static int change_start = 0;
9908 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009909 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009910 int filler_lines;
9911 int col;
9912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 if (lnum < 0) /* ignore type error in {lnum} arg */
9914 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009915 if (lnum != prev_lnum
9916 || changedtick != curbuf->b_changedtick
9917 || fnum != curbuf->b_fnum)
9918 {
9919 /* New line, buffer, change: need to get the values. */
9920 filler_lines = diff_check(curwin, lnum);
9921 if (filler_lines < 0)
9922 {
9923 if (filler_lines == -1)
9924 {
9925 change_start = MAXCOL;
9926 change_end = -1;
9927 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9928 hlID = HLF_ADD; /* added line */
9929 else
9930 hlID = HLF_CHD; /* changed line */
9931 }
9932 else
9933 hlID = HLF_ADD; /* added line */
9934 }
9935 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009936 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009937 prev_lnum = lnum;
9938 changedtick = curbuf->b_changedtick;
9939 fnum = curbuf->b_fnum;
9940 }
9941
9942 if (hlID == HLF_CHD || hlID == HLF_TXD)
9943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009945 if (col >= change_start && col <= change_end)
9946 hlID = HLF_TXD; /* changed text */
9947 else
9948 hlID = HLF_CHD; /* changed line */
9949 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009950 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009951#endif
9952}
9953
9954/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009955 * "empty({expr})" function
9956 */
9957 static void
9958f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 typval_T *argvars;
9960 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009961{
9962 int n;
9963
9964 switch (argvars[0].v_type)
9965 {
9966 case VAR_STRING:
9967 case VAR_FUNC:
9968 n = argvars[0].vval.v_string == NULL
9969 || *argvars[0].vval.v_string == NUL;
9970 break;
9971 case VAR_NUMBER:
9972 n = argvars[0].vval.v_number == 0;
9973 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009974#ifdef FEAT_FLOAT
9975 case VAR_FLOAT:
9976 n = argvars[0].vval.v_float == 0.0;
9977 break;
9978#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009979 case VAR_LIST:
9980 n = argvars[0].vval.v_list == NULL
9981 || argvars[0].vval.v_list->lv_first == NULL;
9982 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 case VAR_DICT:
9984 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009986 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009987 default:
9988 EMSG2(_(e_intern2), "f_empty()");
9989 n = 0;
9990 }
9991
9992 rettv->vval.v_number = n;
9993}
9994
9995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 * "escape({string}, {chars})" function
9997 */
9998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009999f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010000 typval_T *argvars;
10001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002{
10003 char_u buf[NUMBUFLEN];
10004
Bram Moolenaar758711c2005-02-02 23:11:38 +000010005 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10006 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008}
10009
10010/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010011 * "eval()" function
10012 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010013 static void
10014f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010015 typval_T *argvars;
10016 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010017{
10018 char_u *s;
10019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 s = get_tv_string_chk(&argvars[0]);
10021 if (s != NULL)
10022 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010024 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10025 {
10026 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010027 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010028 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010029 else if (*s != NUL)
10030 EMSG(_(e_trailing));
10031}
10032
10033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 * "eventhandler()" function
10035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010037f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010041 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
10043
10044/*
10045 * "executable()" function
10046 */
10047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010049 typval_T *argvars;
10050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010052 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10053}
10054
10055/*
10056 * "exepath()" function
10057 */
10058 static void
10059f_exepath(argvars, rettv)
10060 typval_T *argvars;
10061 typval_T *rettv;
10062{
10063 char_u *p = NULL;
10064
10065 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10066 rettv->v_type = VAR_STRING;
10067 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068}
10069
10070/*
10071 * "exists()" function
10072 */
10073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010074f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010075 typval_T *argvars;
10076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077{
10078 char_u *p;
10079 char_u *name;
10080 int n = FALSE;
10081 int len = 0;
10082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if (*p == '$') /* environment variable */
10085 {
10086 /* first try "normal" environment variables (fast) */
10087 if (mch_getenv(p + 1) != NULL)
10088 n = TRUE;
10089 else
10090 {
10091 /* try expanding things like $VIM and ${HOME} */
10092 p = expand_env_save(p);
10093 if (p != NULL && *p != '$')
10094 n = TRUE;
10095 vim_free(p);
10096 }
10097 }
10098 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010101 if (*skipwhite(p) != NUL)
10102 n = FALSE; /* trailing garbage */
10103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 else if (*p == '*') /* internal or user defined function */
10105 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010106 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108 else if (*p == ':')
10109 {
10110 n = cmd_exists(p + 1);
10111 }
10112 else if (*p == '#')
10113 {
10114#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010115 if (p[1] == '#')
10116 n = autocmd_supported(p + 2);
10117 else
10118 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119#endif
10120 }
10121 else /* internal variable */
10122 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010123 char_u *tofree;
10124 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010126 /* get_name_len() takes care of expanding curly braces */
10127 name = p;
10128 len = get_name_len(&p, &tofree, TRUE, FALSE);
10129 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010131 if (tofree != NULL)
10132 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010133 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010134 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010136 /* handle d.key, l[idx], f(expr) */
10137 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10138 if (n)
10139 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 }
10141 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010142 if (*p != NUL)
10143 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010145 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010151#ifdef FEAT_FLOAT
10152/*
10153 * "exp()" function
10154 */
10155 static void
10156f_exp(argvars, rettv)
10157 typval_T *argvars;
10158 typval_T *rettv;
10159{
10160 float_T f;
10161
10162 rettv->v_type = VAR_FLOAT;
10163 if (get_float_arg(argvars, &f) == OK)
10164 rettv->vval.v_float = exp(f);
10165 else
10166 rettv->vval.v_float = 0.0;
10167}
10168#endif
10169
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170/*
10171 * "expand()" function
10172 */
10173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010174f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 typval_T *argvars;
10176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177{
10178 char_u *s;
10179 int len;
10180 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010181 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010184 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010187 if (argvars[1].v_type != VAR_UNKNOWN
10188 && argvars[2].v_type != VAR_UNKNOWN
10189 && get_tv_number_chk(&argvars[2], &error)
10190 && !error)
10191 {
10192 rettv->v_type = VAR_LIST;
10193 rettv->vval.v_list = NULL;
10194 }
10195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010196 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 if (*s == '%' || *s == '#' || *s == '<')
10198 {
10199 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010200 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010202 if (rettv->v_type == VAR_LIST)
10203 {
10204 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10205 list_append_string(rettv->vval.v_list, result, -1);
10206 else
10207 vim_free(result);
10208 }
10209 else
10210 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 }
10212 else
10213 {
10214 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010215 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010216 if (argvars[1].v_type != VAR_UNKNOWN
10217 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010218 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 if (!error)
10220 {
10221 ExpandInit(&xpc);
10222 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010223 if (p_wic)
10224 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010225 if (rettv->v_type == VAR_STRING)
10226 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10227 options, WILD_ALL);
10228 else if (rettv_list_alloc(rettv) != FAIL)
10229 {
10230 int i;
10231
10232 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10233 for (i = 0; i < xpc.xp_numfiles; i++)
10234 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10235 ExpandCleanup(&xpc);
10236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 }
10238 else
10239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 }
10241}
10242
10243/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010244 * Go over all entries in "d2" and add them to "d1".
10245 * When "action" is "error" then a duplicate key is an error.
10246 * When "action" is "force" then a duplicate key is overwritten.
10247 * Otherwise duplicate keys are ignored ("action" is "keep").
10248 */
10249 void
10250dict_extend(d1, d2, action)
10251 dict_T *d1;
10252 dict_T *d2;
10253 char_u *action;
10254{
10255 dictitem_T *di1;
10256 hashitem_T *hi2;
10257 int todo;
10258
10259 todo = (int)d2->dv_hashtab.ht_used;
10260 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10261 {
10262 if (!HASHITEM_EMPTY(hi2))
10263 {
10264 --todo;
10265 di1 = dict_find(d1, hi2->hi_key, -1);
10266 if (d1->dv_scope != 0)
10267 {
10268 /* Disallow replacing a builtin function in l: and g:.
10269 * Check the key to be valid when adding to any
10270 * scope. */
10271 if (d1->dv_scope == VAR_DEF_SCOPE
10272 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10273 && var_check_func_name(hi2->hi_key,
10274 di1 == NULL))
10275 break;
10276 if (!valid_varname(hi2->hi_key))
10277 break;
10278 }
10279 if (di1 == NULL)
10280 {
10281 di1 = dictitem_copy(HI2DI(hi2));
10282 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10283 dictitem_free(di1);
10284 }
10285 else if (*action == 'e')
10286 {
10287 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10288 break;
10289 }
10290 else if (*action == 'f' && HI2DI(hi2) != di1)
10291 {
10292 clear_tv(&di1->di_tv);
10293 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10294 }
10295 }
10296 }
10297}
10298
10299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010300 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010302 */
10303 static void
10304f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010305 typval_T *argvars;
10306 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010307{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010308 char *arg_errmsg = N_("extend() argument");
10309
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010312 list_T *l1, *l2;
10313 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010316
Bram Moolenaare9a41262005-01-15 22:18:47 +000010317 l1 = argvars[0].vval.v_list;
10318 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010319 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010320 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010321 {
10322 if (argvars[2].v_type != VAR_UNKNOWN)
10323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 before = get_tv_number_chk(&argvars[2], &error);
10325 if (error)
10326 return; /* type error; errmsg already given */
10327
Bram Moolenaar758711c2005-02-02 23:11:38 +000010328 if (before == l1->lv_len)
10329 item = NULL;
10330 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010332 item = list_find(l1, before);
10333 if (item == NULL)
10334 {
10335 EMSGN(_(e_listidx), before);
10336 return;
10337 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 }
10339 }
10340 else
10341 item = NULL;
10342 list_extend(l1, l2, item);
10343
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344 copy_tv(&argvars[0], rettv);
10345 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010346 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010347 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10348 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010349 dict_T *d1, *d2;
10350 char_u *action;
10351 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010352
10353 d1 = argvars[0].vval.v_dict;
10354 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010355 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010356 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357 {
10358 /* Check the third argument. */
10359 if (argvars[2].v_type != VAR_UNKNOWN)
10360 {
10361 static char *(av[]) = {"keep", "force", "error"};
10362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010363 action = get_tv_string_chk(&argvars[2]);
10364 if (action == NULL)
10365 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010366 for (i = 0; i < 3; ++i)
10367 if (STRCMP(action, av[i]) == 0)
10368 break;
10369 if (i == 3)
10370 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010371 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010372 return;
10373 }
10374 }
10375 else
10376 action = (char_u *)"force";
10377
Bram Moolenaara9922d62013-05-30 13:01:18 +020010378 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379
Bram Moolenaare9a41262005-01-15 22:18:47 +000010380 copy_tv(&argvars[0], rettv);
10381 }
10382 }
10383 else
10384 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010385}
10386
10387/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010388 * "feedkeys()" function
10389 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010390 static void
10391f_feedkeys(argvars, rettv)
10392 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010393 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010394{
10395 int remap = TRUE;
10396 char_u *keys, *flags;
10397 char_u nbuf[NUMBUFLEN];
10398 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010399 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010400
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010401 /* This is not allowed in the sandbox. If the commands would still be
10402 * executed in the sandbox it would be OK, but it probably happens later,
10403 * when "sandbox" is no longer set. */
10404 if (check_secure())
10405 return;
10406
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010407 keys = get_tv_string(&argvars[0]);
10408 if (*keys != NUL)
10409 {
10410 if (argvars[1].v_type != VAR_UNKNOWN)
10411 {
10412 flags = get_tv_string_buf(&argvars[1], nbuf);
10413 for ( ; *flags != NUL; ++flags)
10414 {
10415 switch (*flags)
10416 {
10417 case 'n': remap = FALSE; break;
10418 case 'm': remap = TRUE; break;
10419 case 't': typed = TRUE; break;
10420 }
10421 }
10422 }
10423
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010424 /* Need to escape K_SPECIAL and CSI before putting the string in the
10425 * typeahead buffer. */
10426 keys_esc = vim_strsave_escape_csi(keys);
10427 if (keys_esc != NULL)
10428 {
10429 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010430 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010431 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010432 if (vgetc_busy)
10433 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010434 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010435 }
10436}
10437
10438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 * "filereadable()" function
10440 */
10441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010442f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 typval_T *argvars;
10444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010446 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 char_u *p;
10448 int n;
10449
Bram Moolenaarc236c162008-07-13 17:41:49 +000010450#ifndef O_NONBLOCK
10451# define O_NONBLOCK 0
10452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010454 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10455 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 {
10457 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010458 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 }
10460 else
10461 n = FALSE;
10462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010463 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010467 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 * rights to write into.
10469 */
10470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010471f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *argvars;
10473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010475 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476}
10477
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010478static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479
10480 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010481findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010482 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010483 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010484 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010485{
10486#ifdef FEAT_SEARCHPATH
10487 char_u *fname;
10488 char_u *fresult = NULL;
10489 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10490 char_u *p;
10491 char_u pathbuf[NUMBUFLEN];
10492 int count = 1;
10493 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010494 int error = FALSE;
10495#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010496
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010497 rettv->vval.v_string = NULL;
10498 rettv->v_type = VAR_STRING;
10499
10500#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010501 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010503 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10506 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010507 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 else
10509 {
10510 if (*p != NUL)
10511 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010514 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010516 }
10517
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10519 error = TRUE;
10520
10521 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 do
10524 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010525 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010526 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 fresult = find_file_in_path_option(first ? fname : NULL,
10528 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010529 0, first, path,
10530 find_what,
10531 curbuf->b_ffname,
10532 find_what == FINDFILE_DIR
10533 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010535
10536 if (fresult != NULL && rettv->v_type == VAR_LIST)
10537 list_append_string(rettv->vval.v_list, fresult, -1);
10538
10539 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010541
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010542 if (rettv->v_type == VAR_STRING)
10543 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010544#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010545}
10546
Bram Moolenaar33570922005-01-25 22:26:29 +000010547static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10548static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010549
10550/*
10551 * Implementation of map() and filter().
10552 */
10553 static void
10554filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *argvars;
10556 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010557 int map;
10558{
10559 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010560 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 listitem_T *li, *nli;
10562 list_T *l = NULL;
10563 dictitem_T *di;
10564 hashtab_T *ht;
10565 hashitem_T *hi;
10566 dict_T *d = NULL;
10567 typval_T save_val;
10568 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010569 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010570 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010571 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10572 char *arg_errmsg = (map ? N_("map() argument")
10573 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010574 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010575 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010576
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010578 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010579 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010580 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 return;
10582 }
10583 else if (argvars[0].v_type == VAR_DICT)
10584 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010585 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010586 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 return;
10588 }
10589 else
10590 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010591 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 return;
10593 }
10594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 expr = get_tv_string_buf_chk(&argvars[1], buf);
10596 /* On type errors, the preceding call has already displayed an error
10597 * message. Avoid a misleading error message for an empty string that
10598 * was not passed as argument. */
10599 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 prepare_vimvar(VV_VAL, &save_val);
10602 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010603
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010604 /* We reset "did_emsg" to be able to detect whether an error
10605 * occurred during evaluation of the expression. */
10606 save_did_emsg = did_emsg;
10607 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010608
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010609 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 vimvars[VV_KEY].vv_type = VAR_STRING;
10613
10614 ht = &d->dv_hashtab;
10615 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010616 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 if (!HASHITEM_EMPTY(hi))
10620 {
10621 --todo;
10622 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010623 if (tv_check_lock(di->di_tv.v_lock,
10624 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 break;
10626 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010627 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010628 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 break;
10630 if (!map && rem)
10631 dictitem_remove(d, di);
10632 clear_tv(&vimvars[VV_KEY].vv_tv);
10633 }
10634 }
10635 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636 }
10637 else
10638 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010639 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010641 for (li = l->lv_first; li != NULL; li = nli)
10642 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010643 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010644 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010645 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010646 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010647 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010648 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010649 break;
10650 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010651 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010652 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010653 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010655
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010656 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010657 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010658
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010659 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010661
10662 copy_tv(&argvars[0], rettv);
10663}
10664
10665 static int
10666filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010667 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010668 char_u *expr;
10669 int map;
10670 int *remp;
10671{
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010673 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010674 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010675
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010677 s = expr;
10678 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010679 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010680 if (*s != NUL) /* check for trailing chars after expr */
10681 {
10682 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010684 }
10685 if (map)
10686 {
10687 /* map(): replace the list item value */
10688 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010689 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010690 *tv = rettv;
10691 }
10692 else
10693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 int error = FALSE;
10695
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010698 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010699 /* On type error, nothing has been removed; return FAIL to stop the
10700 * loop. The error message was given by get_tv_number_chk(). */
10701 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010702 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010704 retval = OK;
10705theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010706 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010707 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010708}
10709
10710/*
10711 * "filter()" function
10712 */
10713 static void
10714f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010717{
10718 filter_map(argvars, rettv, FALSE);
10719}
10720
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010722 * "finddir({fname}[, {path}[, {count}]])" function
10723 */
10724 static void
10725f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010728{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010729 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010730}
10731
10732/*
10733 * "findfile({fname}[, {path}[, {count}]])" function
10734 */
10735 static void
10736f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010739{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010740 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741}
10742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010743#ifdef FEAT_FLOAT
10744/*
10745 * "float2nr({float})" function
10746 */
10747 static void
10748f_float2nr(argvars, rettv)
10749 typval_T *argvars;
10750 typval_T *rettv;
10751{
10752 float_T f;
10753
10754 if (get_float_arg(argvars, &f) == OK)
10755 {
10756 if (f < -0x7fffffff)
10757 rettv->vval.v_number = -0x7fffffff;
10758 else if (f > 0x7fffffff)
10759 rettv->vval.v_number = 0x7fffffff;
10760 else
10761 rettv->vval.v_number = (varnumber_T)f;
10762 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010763}
10764
10765/*
10766 * "floor({float})" function
10767 */
10768 static void
10769f_floor(argvars, rettv)
10770 typval_T *argvars;
10771 typval_T *rettv;
10772{
10773 float_T f;
10774
10775 rettv->v_type = VAR_FLOAT;
10776 if (get_float_arg(argvars, &f) == OK)
10777 rettv->vval.v_float = floor(f);
10778 else
10779 rettv->vval.v_float = 0.0;
10780}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010781
10782/*
10783 * "fmod()" function
10784 */
10785 static void
10786f_fmod(argvars, rettv)
10787 typval_T *argvars;
10788 typval_T *rettv;
10789{
10790 float_T fx, fy;
10791
10792 rettv->v_type = VAR_FLOAT;
10793 if (get_float_arg(argvars, &fx) == OK
10794 && get_float_arg(&argvars[1], &fy) == OK)
10795 rettv->vval.v_float = fmod(fx, fy);
10796 else
10797 rettv->vval.v_float = 0.0;
10798}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010799#endif
10800
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010802 * "fnameescape({string})" function
10803 */
10804 static void
10805f_fnameescape(argvars, rettv)
10806 typval_T *argvars;
10807 typval_T *rettv;
10808{
10809 rettv->vval.v_string = vim_strsave_fnameescape(
10810 get_tv_string(&argvars[0]), FALSE);
10811 rettv->v_type = VAR_STRING;
10812}
10813
10814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 * "fnamemodify({fname}, {mods})" function
10816 */
10817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010819 typval_T *argvars;
10820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821{
10822 char_u *fname;
10823 char_u *mods;
10824 int usedlen = 0;
10825 int len;
10826 char_u *fbuf = NULL;
10827 char_u buf[NUMBUFLEN];
10828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010829 fname = get_tv_string_chk(&argvars[0]);
10830 mods = get_tv_string_buf_chk(&argvars[1], buf);
10831 if (fname == NULL || mods == NULL)
10832 fname = NULL;
10833 else
10834 {
10835 len = (int)STRLEN(fname);
10836 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 vim_free(fbuf);
10845}
10846
Bram Moolenaar33570922005-01-25 22:26:29 +000010847static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848
10849/*
10850 * "foldclosed()" function
10851 */
10852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010855 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010856 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857{
10858#ifdef FEAT_FOLDING
10859 linenr_T lnum;
10860 linenr_T first, last;
10861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10864 {
10865 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10866 {
10867 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 return;
10872 }
10873 }
10874#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876}
10877
10878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010879 * "foldclosed()" function
10880 */
10881 static void
10882f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010883 typval_T *argvars;
10884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010885{
10886 foldclosed_both(argvars, rettv, FALSE);
10887}
10888
10889/*
10890 * "foldclosedend()" function
10891 */
10892 static void
10893f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 typval_T *argvars;
10895 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010896{
10897 foldclosed_both(argvars, rettv, TRUE);
10898}
10899
10900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 * "foldlevel()" function
10902 */
10903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010905 typval_T *argvars UNUSED;
10906 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907{
10908#ifdef FEAT_FOLDING
10909 linenr_T lnum;
10910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915}
10916
10917/*
10918 * "foldtext()" function
10919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924{
10925#ifdef FEAT_FOLDING
10926 linenr_T lnum;
10927 char_u *s;
10928 char_u *r;
10929 int len;
10930 char *txt;
10931#endif
10932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->v_type = VAR_STRING;
10934 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010936 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10937 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10938 <= curbuf->b_ml.ml_line_count
10939 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 {
10941 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10943 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 {
10945 if (!linewhite(lnum))
10946 break;
10947 ++lnum;
10948 }
10949
10950 /* Find interesting text in this line. */
10951 s = skipwhite(ml_get(lnum));
10952 /* skip C comment-start */
10953 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010956 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010957 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010958 {
10959 s = skipwhite(ml_get(lnum + 1));
10960 if (*s == '*')
10961 s = skipwhite(s + 1);
10962 }
10963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 txt = _("+-%s%3ld lines: ");
10965 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 + 20 /* for %3ld */
10968 + STRLEN(s))); /* concatenated */
10969 if (r != NULL)
10970 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010971 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10972 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10973 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 len = (int)STRLEN(r);
10975 STRCAT(r, s);
10976 /* remove 'foldmarker' and 'commentstring' */
10977 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 }
10980 }
10981#endif
10982}
10983
10984/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010985 * "foldtextresult(lnum)" function
10986 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010989 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991{
10992#ifdef FEAT_FOLDING
10993 linenr_T lnum;
10994 char_u *text;
10995 char_u buf[51];
10996 foldinfo_T foldinfo;
10997 int fold_count;
10998#endif
10999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 rettv->v_type = VAR_STRING;
11001 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011002#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 /* treat illegal types and illegal string values for {lnum} the same */
11005 if (lnum < 0)
11006 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011007 fold_count = foldedCount(curwin, lnum, &foldinfo);
11008 if (fold_count > 0)
11009 {
11010 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11011 &foldinfo, buf);
11012 if (text == buf)
11013 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011015 }
11016#endif
11017}
11018
11019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 * "foreground()" function
11021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011024 typval_T *argvars UNUSED;
11025 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027#ifdef FEAT_GUI
11028 if (gui.in_use)
11029 gui_mch_set_foreground();
11030#else
11031# ifdef WIN32
11032 win32_set_foreground();
11033# endif
11034#endif
11035}
11036
11037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011038 * "function()" function
11039 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 typval_T *argvars;
11043 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011044{
11045 char_u *s;
11046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011048 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011049 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011050 /* Don't check an autoload name for existence here. */
11051 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011052 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 else
11054 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011055 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011056 {
11057 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011058 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011059
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011060 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11061 * also be called from another script. Using trans_function_name()
11062 * would also work, but some plugins depend on the name being
11063 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011064 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011065 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011066 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011067 if (rettv->vval.v_string != NULL)
11068 {
11069 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011070 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011071 }
11072 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011073 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011074 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011076 }
11077}
11078
11079/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011080 * "garbagecollect()" function
11081 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011082 static void
11083f_garbagecollect(argvars, rettv)
11084 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011085 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011086{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011087 /* This is postponed until we are back at the toplevel, because we may be
11088 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11089 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011090
11091 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11092 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011093}
11094
11095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011096 * "get()" function
11097 */
11098 static void
11099f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 typval_T *argvars;
11101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011102{
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 listitem_T *li;
11104 list_T *l;
11105 dictitem_T *di;
11106 dict_T *d;
11107 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011108
Bram Moolenaare9a41262005-01-15 22:18:47 +000011109 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 int error = FALSE;
11114
11115 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11116 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011117 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011120 else if (argvars[0].v_type == VAR_DICT)
11121 {
11122 if ((d = argvars[0].vval.v_dict) != NULL)
11123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011124 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125 if (di != NULL)
11126 tv = &di->di_tv;
11127 }
11128 }
11129 else
11130 EMSG2(_(e_listdictarg), "get()");
11131
11132 if (tv == NULL)
11133 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011134 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 copy_tv(&argvars[2], rettv);
11136 }
11137 else
11138 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011139}
11140
Bram Moolenaar342337a2005-07-21 21:11:17 +000011141static 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 +000011142
11143/*
11144 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011145 * Return a range (from start to end) of lines in rettv from the specified
11146 * buffer.
11147 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011148 */
11149 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011150get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011151 buf_T *buf;
11152 linenr_T start;
11153 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011155 typval_T *rettv;
11156{
11157 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011158
Bram Moolenaar959a1432013-12-14 12:17:38 +010011159 rettv->v_type = VAR_STRING;
11160 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011161 if (retlist && rettv_list_alloc(rettv) == FAIL)
11162 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163
11164 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11165 return;
11166
11167 if (!retlist)
11168 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011169 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11170 p = ml_get_buf(buf, start, FALSE);
11171 else
11172 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011173 rettv->vval.v_string = vim_strsave(p);
11174 }
11175 else
11176 {
11177 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011178 return;
11179
11180 if (start < 1)
11181 start = 1;
11182 if (end > buf->b_ml.ml_line_count)
11183 end = buf->b_ml.ml_line_count;
11184 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011185 if (list_append_string(rettv->vval.v_list,
11186 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011187 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011188 }
11189}
11190
11191/*
11192 * "getbufline()" function
11193 */
11194 static void
11195f_getbufline(argvars, rettv)
11196 typval_T *argvars;
11197 typval_T *rettv;
11198{
11199 linenr_T lnum;
11200 linenr_T end;
11201 buf_T *buf;
11202
11203 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11204 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011205 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011206 --emsg_off;
11207
Bram Moolenaar661b1822005-07-28 22:36:45 +000011208 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011209 if (argvars[2].v_type == VAR_UNKNOWN)
11210 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011211 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011212 end = get_tv_lnum_buf(&argvars[2], buf);
11213
Bram Moolenaar342337a2005-07-21 21:11:17 +000011214 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011215}
11216
Bram Moolenaar0d660222005-01-07 21:51:51 +000011217/*
11218 * "getbufvar()" function
11219 */
11220 static void
11221f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011222 typval_T *argvars;
11223 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011224{
11225 buf_T *buf;
11226 buf_T *save_curbuf;
11227 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011228 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011229 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11232 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011234 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011235
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011236 rettv->v_type = VAR_STRING;
11237 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238
11239 if (buf != NULL && varname != NULL)
11240 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011241 /* set curbuf to be our buf, temporarily */
11242 save_curbuf = curbuf;
11243 curbuf = buf;
11244
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011246 {
11247 if (get_option_tv(&varname, rettv, TRUE) == OK)
11248 done = TRUE;
11249 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011250 else if (STRCMP(varname, "changedtick") == 0)
11251 {
11252 rettv->v_type = VAR_NUMBER;
11253 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011254 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011256 else
11257 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011258 /* Look up the variable. */
11259 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11260 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11261 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011262 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011263 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011264 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011265 done = TRUE;
11266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011267 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011268
11269 /* restore previous notion of curbuf */
11270 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271 }
11272
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011273 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11274 /* use the default value */
11275 copy_tv(&argvars[2], rettv);
11276
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 --emsg_off;
11278}
11279
11280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 * "getchar()" function
11282 */
11283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011285 typval_T *argvars;
11286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
11288 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011289 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011291 /* Position the cursor. Needed after a message that ends in a space. */
11292 windgoto(msg_row, msg_col);
11293
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 ++no_mapping;
11295 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011296 for (;;)
11297 {
11298 if (argvars[0].v_type == VAR_UNKNOWN)
11299 /* getchar(): blocking wait. */
11300 n = safe_vgetc();
11301 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11302 /* getchar(1): only check if char avail */
11303 n = vpeekc();
11304 else if (error || vpeekc() == NUL)
11305 /* illegal argument or getchar(0) and no char avail: return zero */
11306 n = 0;
11307 else
11308 /* getchar(0) and char avail: return char */
11309 n = safe_vgetc();
11310 if (n == K_IGNORE)
11311 continue;
11312 break;
11313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314 --no_mapping;
11315 --allow_keys;
11316
Bram Moolenaar219b8702006-11-01 14:32:36 +000011317 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11318 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11319 vimvars[VV_MOUSE_COL].vv_nr = 0;
11320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 if (IS_SPECIAL(n) || mod_mask != 0)
11323 {
11324 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11325 int i = 0;
11326
11327 /* Turn a special key into three bytes, plus modifier. */
11328 if (mod_mask != 0)
11329 {
11330 temp[i++] = K_SPECIAL;
11331 temp[i++] = KS_MODIFIER;
11332 temp[i++] = mod_mask;
11333 }
11334 if (IS_SPECIAL(n))
11335 {
11336 temp[i++] = K_SPECIAL;
11337 temp[i++] = K_SECOND(n);
11338 temp[i++] = K_THIRD(n);
11339 }
11340#ifdef FEAT_MBYTE
11341 else if (has_mbyte)
11342 i += (*mb_char2bytes)(n, temp + i);
11343#endif
11344 else
11345 temp[i++] = n;
11346 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->v_type = VAR_STRING;
11348 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011349
11350#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011351 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011352 {
11353 int row = mouse_row;
11354 int col = mouse_col;
11355 win_T *win;
11356 linenr_T lnum;
11357# ifdef FEAT_WINDOWS
11358 win_T *wp;
11359# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011360 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011361
11362 if (row >= 0 && col >= 0)
11363 {
11364 /* Find the window at the mouse coordinates and compute the
11365 * text position. */
11366 win = mouse_find_win(&row, &col);
11367 (void)mouse_comp_pos(win, &row, &col, &lnum);
11368# ifdef FEAT_WINDOWS
11369 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011370 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011371# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011372 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011373 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11374 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11375 }
11376 }
11377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 }
11379}
11380
11381/*
11382 * "getcharmod()" function
11383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011386 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390}
11391
11392/*
11393 * "getcmdline()" function
11394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->v_type = VAR_STRING;
11401 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403
11404/*
11405 * "getcmdpos()" function
11406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011409 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413}
11414
11415/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011416 * "getcmdtype()" function
11417 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011418 static void
11419f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011420 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011421 typval_T *rettv;
11422{
11423 rettv->v_type = VAR_STRING;
11424 rettv->vval.v_string = alloc(2);
11425 if (rettv->vval.v_string != NULL)
11426 {
11427 rettv->vval.v_string[0] = get_cmdline_type();
11428 rettv->vval.v_string[1] = NUL;
11429 }
11430}
11431
11432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 * "getcwd()" function
11434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011437 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011440 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011443 rettv->vval.v_string = NULL;
11444 cwd = alloc(MAXPATHL);
11445 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011447 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11448 {
11449 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011451 if (rettv->vval.v_string != NULL)
11452 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011454 }
11455 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 }
11457}
11458
11459/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011460 * "getfontname()" function
11461 */
11462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011464 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011466{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = VAR_STRING;
11468 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011469#ifdef FEAT_GUI
11470 if (gui.in_use)
11471 {
11472 GuiFont font;
11473 char_u *name = NULL;
11474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011476 {
11477 /* Get the "Normal" font. Either the name saved by
11478 * hl_set_font_name() or from the font ID. */
11479 font = gui.norm_font;
11480 name = hl_get_font_name();
11481 }
11482 else
11483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011485 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11486 return;
11487 font = gui_mch_get_font(name, FALSE);
11488 if (font == NOFONT)
11489 return; /* Invalid font name, return empty string. */
11490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011492 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011493 gui_mch_free_font(font);
11494 }
11495#endif
11496}
11497
11498/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011499 * "getfperm({fname})" function
11500 */
11501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 typval_T *argvars;
11504 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011505{
11506 char_u *fname;
11507 struct stat st;
11508 char_u *perm = NULL;
11509 char_u flags[] = "rwx";
11510 int i;
11511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011515 if (mch_stat((char *)fname, &st) >= 0)
11516 {
11517 perm = vim_strsave((char_u *)"---------");
11518 if (perm != NULL)
11519 {
11520 for (i = 0; i < 9; i++)
11521 {
11522 if (st.st_mode & (1 << (8 - i)))
11523 perm[i] = flags[i % 3];
11524 }
11525 }
11526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011528}
11529
11530/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 * "getfsize({fname})" function
11532 */
11533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011535 typval_T *argvars;
11536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537{
11538 char_u *fname;
11539 struct stat st;
11540
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544
11545 if (mch_stat((char *)fname, &st) >= 0)
11546 {
11547 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011552
11553 /* non-perfect check for overflow */
11554 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11555 rettv->vval.v_number = -2;
11556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 }
11558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560}
11561
11562/*
11563 * "getftime({fname})" function
11564 */
11565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 typval_T *argvars;
11568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
11570 char_u *fname;
11571 struct stat st;
11572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574
11575 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011576 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579}
11580
11581/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011582 * "getftype({fname})" function
11583 */
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011588{
11589 char_u *fname;
11590 struct stat st;
11591 char_u *type = NULL;
11592 char *t;
11593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011597 if (mch_lstat((char *)fname, &st) >= 0)
11598 {
11599#ifdef S_ISREG
11600 if (S_ISREG(st.st_mode))
11601 t = "file";
11602 else if (S_ISDIR(st.st_mode))
11603 t = "dir";
11604# ifdef S_ISLNK
11605 else if (S_ISLNK(st.st_mode))
11606 t = "link";
11607# endif
11608# ifdef S_ISBLK
11609 else if (S_ISBLK(st.st_mode))
11610 t = "bdev";
11611# endif
11612# ifdef S_ISCHR
11613 else if (S_ISCHR(st.st_mode))
11614 t = "cdev";
11615# endif
11616# ifdef S_ISFIFO
11617 else if (S_ISFIFO(st.st_mode))
11618 t = "fifo";
11619# endif
11620# ifdef S_ISSOCK
11621 else if (S_ISSOCK(st.st_mode))
11622 t = "fifo";
11623# endif
11624 else
11625 t = "other";
11626#else
11627# ifdef S_IFMT
11628 switch (st.st_mode & S_IFMT)
11629 {
11630 case S_IFREG: t = "file"; break;
11631 case S_IFDIR: t = "dir"; break;
11632# ifdef S_IFLNK
11633 case S_IFLNK: t = "link"; break;
11634# endif
11635# ifdef S_IFBLK
11636 case S_IFBLK: t = "bdev"; break;
11637# endif
11638# ifdef S_IFCHR
11639 case S_IFCHR: t = "cdev"; break;
11640# endif
11641# ifdef S_IFIFO
11642 case S_IFIFO: t = "fifo"; break;
11643# endif
11644# ifdef S_IFSOCK
11645 case S_IFSOCK: t = "socket"; break;
11646# endif
11647 default: t = "other";
11648 }
11649# else
11650 if (mch_isdir(fname))
11651 t = "dir";
11652 else
11653 t = "file";
11654# endif
11655#endif
11656 type = vim_strsave((char_u *)t);
11657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011659}
11660
11661/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011662 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011663 */
11664 static void
11665f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011666 typval_T *argvars;
11667 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011668{
11669 linenr_T lnum;
11670 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011671 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011672
11673 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011674 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011675 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011676 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011677 retlist = FALSE;
11678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011679 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011680 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011681 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011682 retlist = TRUE;
11683 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011684
Bram Moolenaar342337a2005-07-21 21:11:17 +000011685 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686}
11687
11688/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011689 * "getmatches()" function
11690 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011691 static void
11692f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011693 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011694 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011695{
11696#ifdef FEAT_SEARCH_EXTRA
11697 dict_T *dict;
11698 matchitem_T *cur = curwin->w_match_head;
11699
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011700 if (rettv_list_alloc(rettv) == OK)
11701 {
11702 while (cur != NULL)
11703 {
11704 dict = dict_alloc();
11705 if (dict == NULL)
11706 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011707 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11708 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11709 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11710 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11711 list_append_dict(rettv->vval.v_list, dict);
11712 cur = cur->next;
11713 }
11714 }
11715#endif
11716}
11717
11718/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011719 * "getpid()" function
11720 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011721 static void
11722f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011723 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011724 typval_T *rettv;
11725{
11726 rettv->vval.v_number = mch_get_pid();
11727}
11728
11729/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011730 * "getpos(string)" function
11731 */
11732 static void
11733f_getpos(argvars, rettv)
11734 typval_T *argvars;
11735 typval_T *rettv;
11736{
11737 pos_T *fp;
11738 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011739 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011740
11741 if (rettv_list_alloc(rettv) == OK)
11742 {
11743 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011744 fp = var2fpos(&argvars[0], TRUE, &fnum);
11745 if (fnum != -1)
11746 list_append_number(l, (varnumber_T)fnum);
11747 else
11748 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011749 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11750 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011751 list_append_number(l, (fp != NULL)
11752 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011753 : (varnumber_T)0);
11754 list_append_number(l,
11755#ifdef FEAT_VIRTUALEDIT
11756 (fp != NULL) ? (varnumber_T)fp->coladd :
11757#endif
11758 (varnumber_T)0);
11759 }
11760 else
11761 rettv->vval.v_number = FALSE;
11762}
11763
11764/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011765 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011766 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011767 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011768f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011769 typval_T *argvars UNUSED;
11770 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011771{
11772#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011773 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011774#endif
11775
Bram Moolenaar2641f772005-03-25 21:58:17 +000011776#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011777 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011778 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011779 wp = NULL;
11780 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11781 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011782 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011783 if (wp == NULL)
11784 return;
11785 }
11786
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011787 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011788 }
11789#endif
11790}
11791
11792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 * "getreg()" function
11794 */
11795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011796f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 typval_T *argvars;
11798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799{
11800 char_u *strregname;
11801 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011802 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011803 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011806 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 strregname = get_tv_string_chk(&argvars[0]);
11809 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011810 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011813 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11814 return_list = get_tv_number_chk(&argvars[2], &error);
11815 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011818 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011819
11820 if (error)
11821 return;
11822
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 regname = (strregname == NULL ? '"' : *strregname);
11824 if (regname == 0)
11825 regname = '"';
11826
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011827 if (return_list)
11828 {
11829 rettv->v_type = VAR_LIST;
11830 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11831 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11832 }
11833 else
11834 {
11835 rettv->v_type = VAR_STRING;
11836 rettv->vval.v_string = get_reg_contents(regname,
11837 arg2 ? GREG_EXPR_SRC : 0);
11838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839}
11840
11841/*
11842 * "getregtype()" function
11843 */
11844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011846 typval_T *argvars;
11847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
11849 char_u *strregname;
11850 int regname;
11851 char_u buf[NUMBUFLEN + 2];
11852 long reglen = 0;
11853
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011854 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 {
11856 strregname = get_tv_string_chk(&argvars[0]);
11857 if (strregname == NULL) /* type error; errmsg already given */
11858 {
11859 rettv->v_type = VAR_STRING;
11860 rettv->vval.v_string = NULL;
11861 return;
11862 }
11863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 else
11865 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867
11868 regname = (strregname == NULL ? '"' : *strregname);
11869 if (regname == 0)
11870 regname = '"';
11871
11872 buf[0] = NUL;
11873 buf[1] = NUL;
11874 switch (get_reg_type(regname, &reglen))
11875 {
11876 case MLINE: buf[0] = 'V'; break;
11877 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 case MBLOCK:
11879 buf[0] = Ctrl_V;
11880 sprintf((char *)buf + 1, "%ld", reglen + 1);
11881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011883 rettv->v_type = VAR_STRING;
11884 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885}
11886
11887/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011888 * "gettabvar()" function
11889 */
11890 static void
11891f_gettabvar(argvars, rettv)
11892 typval_T *argvars;
11893 typval_T *rettv;
11894{
11895 tabpage_T *tp;
11896 dictitem_T *v;
11897 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011898 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011899
11900 rettv->v_type = VAR_STRING;
11901 rettv->vval.v_string = NULL;
11902
11903 varname = get_tv_string_chk(&argvars[1]);
11904 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11905 if (tp != NULL && varname != NULL)
11906 {
11907 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011908 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011909 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011910 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011911 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011912 done = TRUE;
11913 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011914 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011915
11916 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011917 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011918}
11919
11920/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011921 * "gettabwinvar()" function
11922 */
11923 static void
11924f_gettabwinvar(argvars, rettv)
11925 typval_T *argvars;
11926 typval_T *rettv;
11927{
11928 getwinvar(argvars, rettv, 1);
11929}
11930
11931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 * "getwinposx()" function
11933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011936 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940#ifdef FEAT_GUI
11941 if (gui.in_use)
11942 {
11943 int x, y;
11944
11945 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011946 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 }
11948#endif
11949}
11950
11951/*
11952 * "getwinposy()" function
11953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960#ifdef FEAT_GUI
11961 if (gui.in_use)
11962 {
11963 int x, y;
11964
11965 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967 }
11968#endif
11969}
11970
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011971/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011972 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011973 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011974 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011975find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011976 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011977 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011978{
11979#ifdef FEAT_WINDOWS
11980 win_T *wp;
11981#endif
11982 int nr;
11983
11984 nr = get_tv_number_chk(vp, NULL);
11985
11986#ifdef FEAT_WINDOWS
11987 if (nr < 0)
11988 return NULL;
11989 if (nr == 0)
11990 return curwin;
11991
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011992 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11993 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011994 if (--nr <= 0)
11995 break;
11996 return wp;
11997#else
11998 if (nr == 0 || nr == 1)
11999 return curwin;
12000 return NULL;
12001#endif
12002}
12003
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004/*
12005 * "getwinvar()" function
12006 */
12007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012008f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 typval_T *argvars;
12010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012012 getwinvar(argvars, rettv, 0);
12013}
12014
12015/*
12016 * getwinvar() and gettabwinvar()
12017 */
12018 static void
12019getwinvar(argvars, rettv, off)
12020 typval_T *argvars;
12021 typval_T *rettv;
12022 int off; /* 1 for gettabwinvar() */
12023{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 win_T *win, *oldcurwin;
12025 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012026 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012027 tabpage_T *tp = NULL;
12028 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012029 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012031#ifdef FEAT_WINDOWS
12032 if (off == 1)
12033 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12034 else
12035 tp = curtab;
12036#endif
12037 win = find_win_by_nr(&argvars[off], tp);
12038 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012041 rettv->v_type = VAR_STRING;
12042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043
12044 if (win != NULL && varname != NULL)
12045 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012046 /* Set curwin to be our win, temporarily. Also set the tabpage,
12047 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012048 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012049
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012051 {
12052 if (get_option_tv(&varname, rettv, 1) == OK)
12053 done = TRUE;
12054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 else
12056 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012057 /* Look up the variable. */
12058 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12059 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012061 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012062 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012063 done = TRUE;
12064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012066
12067 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012068 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 }
12070
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012071 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12072 /* use the default return value */
12073 copy_tv(&argvars[off + 2], rettv);
12074
Bram Moolenaar071d4272004-06-13 20:20:40 +000012075 --emsg_off;
12076}
12077
12078/*
12079 * "glob()" function
12080 */
12081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012082f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012083 typval_T *argvars;
12084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012086 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012088 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012090 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012091 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012093 if (argvars[1].v_type != VAR_UNKNOWN)
12094 {
12095 if (get_tv_number_chk(&argvars[1], &error))
12096 options |= WILD_KEEP_ALL;
12097 if (argvars[2].v_type != VAR_UNKNOWN
12098 && get_tv_number_chk(&argvars[2], &error))
12099 {
12100 rettv->v_type = VAR_LIST;
12101 rettv->vval.v_list = NULL;
12102 }
12103 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012104 if (!error)
12105 {
12106 ExpandInit(&xpc);
12107 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012108 if (p_wic)
12109 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012110 if (rettv->v_type == VAR_STRING)
12111 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012112 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012113 else if (rettv_list_alloc(rettv) != FAIL)
12114 {
12115 int i;
12116
12117 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12118 NULL, options, WILD_ALL_KEEP);
12119 for (i = 0; i < xpc.xp_numfiles; i++)
12120 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12121
12122 ExpandCleanup(&xpc);
12123 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012124 }
12125 else
12126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127}
12128
12129/*
12130 * "globpath()" function
12131 */
12132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012133f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012134 typval_T *argvars;
12135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012137 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012139 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012140 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012142 /* When the optional second argument is non-zero, don't remove matches
12143 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12144 if (argvars[2].v_type != VAR_UNKNOWN
12145 && get_tv_number_chk(&argvars[2], &error))
12146 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012148 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012149 rettv->vval.v_string = NULL;
12150 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012151 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12152 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153}
12154
12155/*
12156 * "has()" function
12157 */
12158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *argvars;
12161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
12163 int i;
12164 char_u *name;
12165 int n = FALSE;
12166 static char *(has_list[]) =
12167 {
12168#ifdef AMIGA
12169 "amiga",
12170# ifdef FEAT_ARP
12171 "arp",
12172# endif
12173#endif
12174#ifdef __BEOS__
12175 "beos",
12176#endif
12177#ifdef MSDOS
12178# ifdef DJGPP
12179 "dos32",
12180# else
12181 "dos16",
12182# endif
12183#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012184#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 "mac",
12186#endif
12187#if defined(MACOS_X_UNIX)
12188 "macunix",
12189#endif
12190#ifdef OS2
12191 "os2",
12192#endif
12193#ifdef __QNX__
12194 "qnx",
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196#ifdef UNIX
12197 "unix",
12198#endif
12199#ifdef VMS
12200 "vms",
12201#endif
12202#ifdef WIN16
12203 "win16",
12204#endif
12205#ifdef WIN32
12206 "win32",
12207#endif
12208#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12209 "win32unix",
12210#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012211#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 "win64",
12213#endif
12214#ifdef EBCDIC
12215 "ebcdic",
12216#endif
12217#ifndef CASE_INSENSITIVE_FILENAME
12218 "fname_case",
12219#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012220#ifdef HAVE_ACL
12221 "acl",
12222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223#ifdef FEAT_ARABIC
12224 "arabic",
12225#endif
12226#ifdef FEAT_AUTOCMD
12227 "autocmd",
12228#endif
12229#ifdef FEAT_BEVAL
12230 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012231# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12232 "balloon_multiline",
12233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#endif
12235#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12236 "builtin_terms",
12237# ifdef ALL_BUILTIN_TCAPS
12238 "all_builtin_terms",
12239# endif
12240#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012241#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12242 || defined(FEAT_GUI_W32) \
12243 || defined(FEAT_GUI_MOTIF))
12244 "browsefilter",
12245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246#ifdef FEAT_BYTEOFF
12247 "byte_offset",
12248#endif
12249#ifdef FEAT_CINDENT
12250 "cindent",
12251#endif
12252#ifdef FEAT_CLIENTSERVER
12253 "clientserver",
12254#endif
12255#ifdef FEAT_CLIPBOARD
12256 "clipboard",
12257#endif
12258#ifdef FEAT_CMDL_COMPL
12259 "cmdline_compl",
12260#endif
12261#ifdef FEAT_CMDHIST
12262 "cmdline_hist",
12263#endif
12264#ifdef FEAT_COMMENTS
12265 "comments",
12266#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012267#ifdef FEAT_CONCEAL
12268 "conceal",
12269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270#ifdef FEAT_CRYPT
12271 "cryptv",
12272#endif
12273#ifdef FEAT_CSCOPE
12274 "cscope",
12275#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012276#ifdef FEAT_CURSORBIND
12277 "cursorbind",
12278#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012279#ifdef CURSOR_SHAPE
12280 "cursorshape",
12281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282#ifdef DEBUG
12283 "debug",
12284#endif
12285#ifdef FEAT_CON_DIALOG
12286 "dialog_con",
12287#endif
12288#ifdef FEAT_GUI_DIALOG
12289 "dialog_gui",
12290#endif
12291#ifdef FEAT_DIFF
12292 "diff",
12293#endif
12294#ifdef FEAT_DIGRAPHS
12295 "digraphs",
12296#endif
12297#ifdef FEAT_DND
12298 "dnd",
12299#endif
12300#ifdef FEAT_EMACS_TAGS
12301 "emacs_tags",
12302#endif
12303 "eval", /* always present, of course! */
12304#ifdef FEAT_EX_EXTRA
12305 "ex_extra",
12306#endif
12307#ifdef FEAT_SEARCH_EXTRA
12308 "extra_search",
12309#endif
12310#ifdef FEAT_FKMAP
12311 "farsi",
12312#endif
12313#ifdef FEAT_SEARCHPATH
12314 "file_in_path",
12315#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012316#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012317 "filterpipe",
12318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319#ifdef FEAT_FIND_ID
12320 "find_in_path",
12321#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012322#ifdef FEAT_FLOAT
12323 "float",
12324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325#ifdef FEAT_FOLDING
12326 "folding",
12327#endif
12328#ifdef FEAT_FOOTER
12329 "footer",
12330#endif
12331#if !defined(USE_SYSTEM) && defined(UNIX)
12332 "fork",
12333#endif
12334#ifdef FEAT_GETTEXT
12335 "gettext",
12336#endif
12337#ifdef FEAT_GUI
12338 "gui",
12339#endif
12340#ifdef FEAT_GUI_ATHENA
12341# ifdef FEAT_GUI_NEXTAW
12342 "gui_neXtaw",
12343# else
12344 "gui_athena",
12345# endif
12346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347#ifdef FEAT_GUI_GTK
12348 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012351#ifdef FEAT_GUI_GNOME
12352 "gui_gnome",
12353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#ifdef FEAT_GUI_MAC
12355 "gui_mac",
12356#endif
12357#ifdef FEAT_GUI_MOTIF
12358 "gui_motif",
12359#endif
12360#ifdef FEAT_GUI_PHOTON
12361 "gui_photon",
12362#endif
12363#ifdef FEAT_GUI_W16
12364 "gui_win16",
12365#endif
12366#ifdef FEAT_GUI_W32
12367 "gui_win32",
12368#endif
12369#ifdef FEAT_HANGULIN
12370 "hangul_input",
12371#endif
12372#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12373 "iconv",
12374#endif
12375#ifdef FEAT_INS_EXPAND
12376 "insert_expand",
12377#endif
12378#ifdef FEAT_JUMPLIST
12379 "jumplist",
12380#endif
12381#ifdef FEAT_KEYMAP
12382 "keymap",
12383#endif
12384#ifdef FEAT_LANGMAP
12385 "langmap",
12386#endif
12387#ifdef FEAT_LIBCALL
12388 "libcall",
12389#endif
12390#ifdef FEAT_LINEBREAK
12391 "linebreak",
12392#endif
12393#ifdef FEAT_LISP
12394 "lispindent",
12395#endif
12396#ifdef FEAT_LISTCMDS
12397 "listcmds",
12398#endif
12399#ifdef FEAT_LOCALMAP
12400 "localmap",
12401#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012402#ifdef FEAT_LUA
12403# ifndef DYNAMIC_LUA
12404 "lua",
12405# endif
12406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407#ifdef FEAT_MENU
12408 "menu",
12409#endif
12410#ifdef FEAT_SESSION
12411 "mksession",
12412#endif
12413#ifdef FEAT_MODIFY_FNAME
12414 "modify_fname",
12415#endif
12416#ifdef FEAT_MOUSE
12417 "mouse",
12418#endif
12419#ifdef FEAT_MOUSESHAPE
12420 "mouseshape",
12421#endif
12422#if defined(UNIX) || defined(VMS)
12423# ifdef FEAT_MOUSE_DEC
12424 "mouse_dec",
12425# endif
12426# ifdef FEAT_MOUSE_GPM
12427 "mouse_gpm",
12428# endif
12429# ifdef FEAT_MOUSE_JSB
12430 "mouse_jsbterm",
12431# endif
12432# ifdef FEAT_MOUSE_NET
12433 "mouse_netterm",
12434# endif
12435# ifdef FEAT_MOUSE_PTERM
12436 "mouse_pterm",
12437# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012438# ifdef FEAT_MOUSE_SGR
12439 "mouse_sgr",
12440# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012441# ifdef FEAT_SYSMOUSE
12442 "mouse_sysmouse",
12443# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012444# ifdef FEAT_MOUSE_URXVT
12445 "mouse_urxvt",
12446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447# ifdef FEAT_MOUSE_XTERM
12448 "mouse_xterm",
12449# endif
12450#endif
12451#ifdef FEAT_MBYTE
12452 "multi_byte",
12453#endif
12454#ifdef FEAT_MBYTE_IME
12455 "multi_byte_ime",
12456#endif
12457#ifdef FEAT_MULTI_LANG
12458 "multi_lang",
12459#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012460#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012461#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012462 "mzscheme",
12463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465#ifdef FEAT_OLE
12466 "ole",
12467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468#ifdef FEAT_PATH_EXTRA
12469 "path_extra",
12470#endif
12471#ifdef FEAT_PERL
12472#ifndef DYNAMIC_PERL
12473 "perl",
12474#endif
12475#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012476#ifdef FEAT_PERSISTENT_UNDO
12477 "persistent_undo",
12478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479#ifdef FEAT_PYTHON
12480#ifndef DYNAMIC_PYTHON
12481 "python",
12482#endif
12483#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012484#ifdef FEAT_PYTHON3
12485#ifndef DYNAMIC_PYTHON3
12486 "python3",
12487#endif
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_POSTSCRIPT
12490 "postscript",
12491#endif
12492#ifdef FEAT_PRINTER
12493 "printer",
12494#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012495#ifdef FEAT_PROFILE
12496 "profile",
12497#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012498#ifdef FEAT_RELTIME
12499 "reltime",
12500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef FEAT_QUICKFIX
12502 "quickfix",
12503#endif
12504#ifdef FEAT_RIGHTLEFT
12505 "rightleft",
12506#endif
12507#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12508 "ruby",
12509#endif
12510#ifdef FEAT_SCROLLBIND
12511 "scrollbind",
12512#endif
12513#ifdef FEAT_CMDL_INFO
12514 "showcmd",
12515 "cmdline_info",
12516#endif
12517#ifdef FEAT_SIGNS
12518 "signs",
12519#endif
12520#ifdef FEAT_SMARTINDENT
12521 "smartindent",
12522#endif
12523#ifdef FEAT_SNIFF
12524 "sniff",
12525#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012526#ifdef STARTUPTIME
12527 "startuptime",
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef FEAT_STL_OPT
12530 "statusline",
12531#endif
12532#ifdef FEAT_SUN_WORKSHOP
12533 "sun_workshop",
12534#endif
12535#ifdef FEAT_NETBEANS_INTG
12536 "netbeans_intg",
12537#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012538#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012539 "spell",
12540#endif
12541#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542 "syntax",
12543#endif
12544#if defined(USE_SYSTEM) || !defined(UNIX)
12545 "system",
12546#endif
12547#ifdef FEAT_TAG_BINS
12548 "tag_binary",
12549#endif
12550#ifdef FEAT_TAG_OLDSTATIC
12551 "tag_old_static",
12552#endif
12553#ifdef FEAT_TAG_ANYWHITE
12554 "tag_any_white",
12555#endif
12556#ifdef FEAT_TCL
12557# ifndef DYNAMIC_TCL
12558 "tcl",
12559# endif
12560#endif
12561#ifdef TERMINFO
12562 "terminfo",
12563#endif
12564#ifdef FEAT_TERMRESPONSE
12565 "termresponse",
12566#endif
12567#ifdef FEAT_TEXTOBJ
12568 "textobjects",
12569#endif
12570#ifdef HAVE_TGETENT
12571 "tgetent",
12572#endif
12573#ifdef FEAT_TITLE
12574 "title",
12575#endif
12576#ifdef FEAT_TOOLBAR
12577 "toolbar",
12578#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012579#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12580 "unnamedplus",
12581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582#ifdef FEAT_USR_CMDS
12583 "user-commands", /* was accidentally included in 5.4 */
12584 "user_commands",
12585#endif
12586#ifdef FEAT_VIMINFO
12587 "viminfo",
12588#endif
12589#ifdef FEAT_VERTSPLIT
12590 "vertsplit",
12591#endif
12592#ifdef FEAT_VIRTUALEDIT
12593 "virtualedit",
12594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#ifdef FEAT_VISUALEXTRA
12597 "visualextra",
12598#endif
12599#ifdef FEAT_VREPLACE
12600 "vreplace",
12601#endif
12602#ifdef FEAT_WILDIGN
12603 "wildignore",
12604#endif
12605#ifdef FEAT_WILDMENU
12606 "wildmenu",
12607#endif
12608#ifdef FEAT_WINDOWS
12609 "windows",
12610#endif
12611#ifdef FEAT_WAK
12612 "winaltkeys",
12613#endif
12614#ifdef FEAT_WRITEBACKUP
12615 "writebackup",
12616#endif
12617#ifdef FEAT_XIM
12618 "xim",
12619#endif
12620#ifdef FEAT_XFONTSET
12621 "xfontset",
12622#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012623#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012624 "xpm",
12625 "xpm_w32", /* for backward compatibility */
12626#else
12627# if defined(HAVE_XPM)
12628 "xpm",
12629# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631#ifdef USE_XSMP
12632 "xsmp",
12633#endif
12634#ifdef USE_XSMP_INTERACT
12635 "xsmp_interact",
12636#endif
12637#ifdef FEAT_XCLIPBOARD
12638 "xterm_clipboard",
12639#endif
12640#ifdef FEAT_XTERM_SAVE
12641 "xterm_save",
12642#endif
12643#if defined(UNIX) && defined(FEAT_X11)
12644 "X11",
12645#endif
12646 NULL
12647 };
12648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 for (i = 0; has_list[i] != NULL; ++i)
12651 if (STRICMP(name, has_list[i]) == 0)
12652 {
12653 n = TRUE;
12654 break;
12655 }
12656
12657 if (n == FALSE)
12658 {
12659 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012660 {
12661 if (name[5] == '-'
12662 && STRLEN(name) > 11
12663 && vim_isdigit(name[6])
12664 && vim_isdigit(name[8])
12665 && vim_isdigit(name[10]))
12666 {
12667 int major = atoi((char *)name + 6);
12668 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012669
12670 /* Expect "patch-9.9.01234". */
12671 n = (major < VIM_VERSION_MAJOR
12672 || (major == VIM_VERSION_MAJOR
12673 && (minor < VIM_VERSION_MINOR
12674 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012675 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012676 }
12677 else
12678 n = has_patch(atoi((char *)name + 5));
12679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 else if (STRICMP(name, "vim_starting") == 0)
12681 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012682#ifdef FEAT_MBYTE
12683 else if (STRICMP(name, "multi_byte_encoding") == 0)
12684 n = has_mbyte;
12685#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012686#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12687 else if (STRICMP(name, "balloon_multiline") == 0)
12688 n = multiline_balloon_available();
12689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690#ifdef DYNAMIC_TCL
12691 else if (STRICMP(name, "tcl") == 0)
12692 n = tcl_enabled(FALSE);
12693#endif
12694#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12695 else if (STRICMP(name, "iconv") == 0)
12696 n = iconv_enabled(FALSE);
12697#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012698#ifdef DYNAMIC_LUA
12699 else if (STRICMP(name, "lua") == 0)
12700 n = lua_enabled(FALSE);
12701#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012702#ifdef DYNAMIC_MZSCHEME
12703 else if (STRICMP(name, "mzscheme") == 0)
12704 n = mzscheme_enabled(FALSE);
12705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706#ifdef DYNAMIC_RUBY
12707 else if (STRICMP(name, "ruby") == 0)
12708 n = ruby_enabled(FALSE);
12709#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012710#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711#ifdef DYNAMIC_PYTHON
12712 else if (STRICMP(name, "python") == 0)
12713 n = python_enabled(FALSE);
12714#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012715#endif
12716#ifdef FEAT_PYTHON3
12717#ifdef DYNAMIC_PYTHON3
12718 else if (STRICMP(name, "python3") == 0)
12719 n = python3_enabled(FALSE);
12720#endif
12721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722#ifdef DYNAMIC_PERL
12723 else if (STRICMP(name, "perl") == 0)
12724 n = perl_enabled(FALSE);
12725#endif
12726#ifdef FEAT_GUI
12727 else if (STRICMP(name, "gui_running") == 0)
12728 n = (gui.in_use || gui.starting);
12729# ifdef FEAT_GUI_W32
12730 else if (STRICMP(name, "gui_win32s") == 0)
12731 n = gui_is_win32s();
12732# endif
12733# ifdef FEAT_BROWSE
12734 else if (STRICMP(name, "browse") == 0)
12735 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12736# endif
12737#endif
12738#ifdef FEAT_SYN_HL
12739 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012740 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741#endif
12742#if defined(WIN3264)
12743 else if (STRICMP(name, "win95") == 0)
12744 n = mch_windows95();
12745#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012746#ifdef FEAT_NETBEANS_INTG
12747 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012748 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 }
12751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753}
12754
12755/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012756 * "has_key()" function
12757 */
12758 static void
12759f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012760 typval_T *argvars;
12761 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012762{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012763 if (argvars[0].v_type != VAR_DICT)
12764 {
12765 EMSG(_(e_dictreq));
12766 return;
12767 }
12768 if (argvars[0].vval.v_dict == NULL)
12769 return;
12770
12771 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012772 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012773}
12774
12775/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012776 * "haslocaldir()" function
12777 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012778 static void
12779f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012780 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012781 typval_T *rettv;
12782{
12783 rettv->vval.v_number = (curwin->w_localdir != NULL);
12784}
12785
12786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 * "hasmapto()" function
12788 */
12789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012791 typval_T *argvars;
12792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793{
12794 char_u *name;
12795 char_u *mode;
12796 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012797 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012800 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 mode = (char_u *)"nvo";
12802 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012805 if (argvars[2].v_type != VAR_UNKNOWN)
12806 abbr = get_tv_number(&argvars[2]);
12807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808
Bram Moolenaar2c932302006-03-18 21:42:09 +000012809 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813}
12814
12815/*
12816 * "histadd()" function
12817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
12823#ifdef FEAT_CMDHIST
12824 int histype;
12825 char_u *str;
12826 char_u buf[NUMBUFLEN];
12827#endif
12828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012829 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 if (check_restricted() || check_secure())
12831 return;
12832#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12834 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 if (histype >= 0)
12836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 if (*str != NUL)
12839 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012840 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 return;
12844 }
12845 }
12846#endif
12847}
12848
12849/*
12850 * "histdel()" function
12851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012854 typval_T *argvars UNUSED;
12855 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856{
12857#ifdef FEAT_CMDHIST
12858 int n;
12859 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012860 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012862 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12863 if (str == NULL)
12864 n = 0;
12865 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012868 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012870 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872 else
12873 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012874 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 get_tv_string_buf(&argvars[1], buf));
12876 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877#endif
12878}
12879
12880/*
12881 * "histget()" function
12882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
12888#ifdef FEAT_CMDHIST
12889 int type;
12890 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012891 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12894 if (str == NULL)
12895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 {
12898 type = get_histtype(str);
12899 if (argvars[1].v_type == VAR_UNKNOWN)
12900 idx = get_history_idx(type);
12901 else
12902 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12903 /* -1 on type error */
12904 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910}
12911
12912/*
12913 * "histnr()" function
12914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919{
12920 int i;
12921
12922#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 char_u *history = get_tv_string_chk(&argvars[0]);
12924
12925 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926 if (i >= HIST_CMD && i < HIST_COUNT)
12927 i = get_history_idx(i);
12928 else
12929#endif
12930 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
12934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 * "highlightID(name)" function
12936 */
12937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012939 typval_T *argvars;
12940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
12945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012946 * "highlight_exists()" function
12947 */
12948 static void
12949f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012950 typval_T *argvars;
12951 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012952{
12953 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12954}
12955
12956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 * "hostname()" function
12958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012961 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963{
12964 char_u hostname[256];
12965
12966 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->v_type = VAR_STRING;
12968 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012969}
12970
12971/*
12972 * iconv() function
12973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012976 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978{
12979#ifdef FEAT_MBYTE
12980 char_u buf1[NUMBUFLEN];
12981 char_u buf2[NUMBUFLEN];
12982 char_u *from, *to, *str;
12983 vimconv_T vimconv;
12984#endif
12985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 rettv->v_type = VAR_STRING;
12987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988
12989#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012990 str = get_tv_string(&argvars[0]);
12991 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12992 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 vimconv.vc_type = CONV_NONE;
12994 convert_setup(&vimconv, from, to);
12995
12996 /* If the encodings are equal, no conversion needed. */
12997 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001
13002 convert_setup(&vimconv, NULL, NULL);
13003 vim_free(from);
13004 vim_free(to);
13005#endif
13006}
13007
13008/*
13009 * "indent()" function
13010 */
13011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015{
13016 linenr_T lnum;
13017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023}
13024
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013025/*
13026 * "index()" function
13027 */
13028 static void
13029f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013030 typval_T *argvars;
13031 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013032{
Bram Moolenaar33570922005-01-25 22:26:29 +000013033 list_T *l;
13034 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013035 long idx = 0;
13036 int ic = FALSE;
13037
13038 rettv->vval.v_number = -1;
13039 if (argvars[0].v_type != VAR_LIST)
13040 {
13041 EMSG(_(e_listreq));
13042 return;
13043 }
13044 l = argvars[0].vval.v_list;
13045 if (l != NULL)
13046 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013047 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013050 int error = FALSE;
13051
Bram Moolenaar758711c2005-02-02 23:11:38 +000013052 /* Start at specified item. Use the cached index that list_find()
13053 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013055 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 ic = get_tv_number_chk(&argvars[3], &error);
13058 if (error)
13059 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013060 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013061
Bram Moolenaar758711c2005-02-02 23:11:38 +000013062 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013063 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013064 {
13065 rettv->vval.v_number = idx;
13066 break;
13067 }
13068 }
13069}
13070
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071static int inputsecret_flag = 0;
13072
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013073static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13074
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013076 * This function is used by f_input() and f_inputdialog() functions. The third
13077 * argument to f_input() specifies the type of completion to use at the
13078 * prompt. The third argument to f_inputdialog() specifies the value to return
13079 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 */
13081 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013082get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *argvars;
13084 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013085 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 char_u *p = NULL;
13089 int c;
13090 char_u buf[NUMBUFLEN];
13091 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013093 int xp_type = EXPAND_NOTHING;
13094 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013097 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098
13099#ifdef NO_CONSOLE_INPUT
13100 /* While starting up, there is no place to enter text. */
13101 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103#endif
13104
13105 cmd_silent = FALSE; /* Want to see the prompt. */
13106 if (prompt != NULL)
13107 {
13108 /* Only the part of the message after the last NL is considered as
13109 * prompt for the command line */
13110 p = vim_strrchr(prompt, '\n');
13111 if (p == NULL)
13112 p = prompt;
13113 else
13114 {
13115 ++p;
13116 c = *p;
13117 *p = NUL;
13118 msg_start();
13119 msg_clr_eos();
13120 msg_puts_attr(prompt, echo_attr);
13121 msg_didout = FALSE;
13122 msg_starthere();
13123 *p = c;
13124 }
13125 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013127 if (argvars[1].v_type != VAR_UNKNOWN)
13128 {
13129 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13130 if (defstr != NULL)
13131 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013133 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013134 {
13135 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013136 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013137 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013138
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013139 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013140 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013141
Bram Moolenaar4463f292005-09-25 22:20:24 +000013142 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13143 if (xp_name == NULL)
13144 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013146 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013147
Bram Moolenaar4463f292005-09-25 22:20:24 +000013148 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13149 &xp_arg) == FAIL)
13150 return;
13151 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013152 }
13153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013155 {
13156# ifdef FEAT_EX_EXTRA
13157 int save_ex_normal_busy = ex_normal_busy;
13158 ex_normal_busy = 0;
13159# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013160 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013161 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13162 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013163# ifdef FEAT_EX_EXTRA
13164 ex_normal_busy = save_ex_normal_busy;
13165# endif
13166 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013167 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013168 && argvars[1].v_type != VAR_UNKNOWN
13169 && argvars[2].v_type != VAR_UNKNOWN)
13170 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13171 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013172
13173 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 /* since the user typed this, no need to wait for return */
13176 need_wait_return = FALSE;
13177 msg_didout = FALSE;
13178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 cmd_silent = cmd_silent_save;
13180}
13181
13182/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013183 * "input()" function
13184 * Also handles inputsecret() when inputsecret is set.
13185 */
13186 static void
13187f_input(argvars, rettv)
13188 typval_T *argvars;
13189 typval_T *rettv;
13190{
13191 get_user_input(argvars, rettv, FALSE);
13192}
13193
13194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 * "inputdialog()" function
13196 */
13197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013199 typval_T *argvars;
13200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201{
13202#if defined(FEAT_GUI_TEXTDIALOG)
13203 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13204 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13205 {
13206 char_u *message;
13207 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013210 message = get_tv_string_chk(&argvars[0]);
13211 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013212 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013213 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 else
13215 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 if (message != NULL && defstr != NULL
13217 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013218 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 else
13221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 if (message != NULL && defstr != NULL
13223 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013224 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->vval.v_string = vim_strsave(
13226 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 }
13232 else
13233#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013234 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013237/*
13238 * "inputlist()" function
13239 */
13240 static void
13241f_inputlist(argvars, rettv)
13242 typval_T *argvars;
13243 typval_T *rettv;
13244{
13245 listitem_T *li;
13246 int selected;
13247 int mouse_used;
13248
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013249#ifdef NO_CONSOLE_INPUT
13250 /* While starting up, there is no place to enter text. */
13251 if (no_console_input())
13252 return;
13253#endif
13254 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13255 {
13256 EMSG2(_(e_listarg), "inputlist()");
13257 return;
13258 }
13259
13260 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013261 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013262 lines_left = Rows; /* avoid more prompt */
13263 msg_scroll = TRUE;
13264 msg_clr_eos();
13265
13266 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13267 {
13268 msg_puts(get_tv_string(&li->li_tv));
13269 msg_putchar('\n');
13270 }
13271
13272 /* Ask for choice. */
13273 selected = prompt_for_number(&mouse_used);
13274 if (mouse_used)
13275 selected -= lines_left;
13276
13277 rettv->vval.v_number = selected;
13278}
13279
13280
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13282
13283/*
13284 * "inputrestore()" function
13285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013288 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290{
13291 if (ga_userinput.ga_len > 0)
13292 {
13293 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13295 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013296 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297 }
13298 else if (p_verbose > 1)
13299 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013300 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 }
13303}
13304
13305/*
13306 * "inputsave()" function
13307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013310 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013313 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 if (ga_grow(&ga_userinput, 1) == OK)
13315 {
13316 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13317 + ga_userinput.ga_len);
13318 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013319 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 }
13321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013322 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323}
13324
13325/*
13326 * "inputsecret()" function
13327 */
13328 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013329f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013330 typval_T *argvars;
13331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332{
13333 ++cmdline_star;
13334 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013335 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 --cmdline_star;
13337 --inputsecret_flag;
13338}
13339
13340/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013341 * "insert()" function
13342 */
13343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013347{
13348 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013349 listitem_T *item;
13350 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013351 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013352
13353 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013354 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013355 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013356 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013357 {
13358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013359 before = get_tv_number_chk(&argvars[2], &error);
13360 if (error)
13361 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013362
Bram Moolenaar758711c2005-02-02 23:11:38 +000013363 if (before == l->lv_len)
13364 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013365 else
13366 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013367 item = list_find(l, before);
13368 if (item == NULL)
13369 {
13370 EMSGN(_(e_listidx), before);
13371 l = NULL;
13372 }
13373 }
13374 if (l != NULL)
13375 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013376 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013377 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013378 }
13379 }
13380}
13381
13382/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013383 * "invert(expr)" function
13384 */
13385 static void
13386f_invert(argvars, rettv)
13387 typval_T *argvars;
13388 typval_T *rettv;
13389{
13390 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13391}
13392
13393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 * "isdirectory()" function
13395 */
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402}
13403
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013404/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013405 * "islocked()" function
13406 */
13407 static void
13408f_islocked(argvars, rettv)
13409 typval_T *argvars;
13410 typval_T *rettv;
13411{
13412 lval_T lv;
13413 char_u *end;
13414 dictitem_T *di;
13415
13416 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013417 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13418 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013419 if (end != NULL && lv.ll_name != NULL)
13420 {
13421 if (*end != NUL)
13422 EMSG(_(e_trailing));
13423 else
13424 {
13425 if (lv.ll_tv == NULL)
13426 {
13427 if (check_changedtick(lv.ll_name))
13428 rettv->vval.v_number = 1; /* always locked */
13429 else
13430 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013431 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013432 if (di != NULL)
13433 {
13434 /* Consider a variable locked when:
13435 * 1. the variable itself is locked
13436 * 2. the value of the variable is locked.
13437 * 3. the List or Dict value is locked.
13438 */
13439 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13440 || tv_islocked(&di->di_tv));
13441 }
13442 }
13443 }
13444 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013445 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013446 else if (lv.ll_newkey != NULL)
13447 EMSG2(_(e_dictkey), lv.ll_newkey);
13448 else if (lv.ll_list != NULL)
13449 /* List item. */
13450 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13451 else
13452 /* Dictionary item. */
13453 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13454 }
13455 }
13456
13457 clear_lval(&lv);
13458}
13459
Bram Moolenaar33570922005-01-25 22:26:29 +000013460static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013461
13462/*
13463 * Turn a dict into a list:
13464 * "what" == 0: list of keys
13465 * "what" == 1: list of values
13466 * "what" == 2: list of items
13467 */
13468 static void
13469dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013470 typval_T *argvars;
13471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013472 int what;
13473{
Bram Moolenaar33570922005-01-25 22:26:29 +000013474 list_T *l2;
13475 dictitem_T *di;
13476 hashitem_T *hi;
13477 listitem_T *li;
13478 listitem_T *li2;
13479 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013480 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013481
Bram Moolenaar8c711452005-01-14 21:53:12 +000013482 if (argvars[0].v_type != VAR_DICT)
13483 {
13484 EMSG(_(e_dictreq));
13485 return;
13486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013487 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013488 return;
13489
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013490 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013491 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013492
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013493 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013496 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013497 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013498 --todo;
13499 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013501 li = listitem_alloc();
13502 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013503 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013504 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013506 if (what == 0)
13507 {
13508 /* keys() */
13509 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013510 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013511 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13512 }
13513 else if (what == 1)
13514 {
13515 /* values() */
13516 copy_tv(&di->di_tv, &li->li_tv);
13517 }
13518 else
13519 {
13520 /* items() */
13521 l2 = list_alloc();
13522 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013523 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013524 li->li_tv.vval.v_list = l2;
13525 if (l2 == NULL)
13526 break;
13527 ++l2->lv_refcount;
13528
13529 li2 = listitem_alloc();
13530 if (li2 == NULL)
13531 break;
13532 list_append(l2, li2);
13533 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013534 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013535 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13536
13537 li2 = listitem_alloc();
13538 if (li2 == NULL)
13539 break;
13540 list_append(l2, li2);
13541 copy_tv(&di->di_tv, &li2->li_tv);
13542 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013543 }
13544 }
13545}
13546
13547/*
13548 * "items(dict)" function
13549 */
13550 static void
13551f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013552 typval_T *argvars;
13553 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013554{
13555 dict_list(argvars, rettv, 2);
13556}
13557
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013559 * "join()" function
13560 */
13561 static void
13562f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013563 typval_T *argvars;
13564 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013565{
13566 garray_T ga;
13567 char_u *sep;
13568
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013569 if (argvars[0].v_type != VAR_LIST)
13570 {
13571 EMSG(_(e_listreq));
13572 return;
13573 }
13574 if (argvars[0].vval.v_list == NULL)
13575 return;
13576 if (argvars[1].v_type == VAR_UNKNOWN)
13577 sep = (char_u *)" ";
13578 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013579 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013580
13581 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582
13583 if (sep != NULL)
13584 {
13585 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013586 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587 ga_append(&ga, NUL);
13588 rettv->vval.v_string = (char_u *)ga.ga_data;
13589 }
13590 else
13591 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013592}
13593
13594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013595 * "keys()" function
13596 */
13597 static void
13598f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013599 typval_T *argvars;
13600 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013601{
13602 dict_list(argvars, rettv, 0);
13603}
13604
13605/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 * "last_buffer_nr()" function.
13607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013610 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612{
13613 int n = 0;
13614 buf_T *buf;
13615
13616 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13617 if (n < buf->b_fnum)
13618 n = buf->b_fnum;
13619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013620 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013621}
13622
13623/*
13624 * "len()" function
13625 */
13626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013628 typval_T *argvars;
13629 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013630{
13631 switch (argvars[0].v_type)
13632 {
13633 case VAR_STRING:
13634 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->vval.v_number = (varnumber_T)STRLEN(
13636 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013637 break;
13638 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013640 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013641 case VAR_DICT:
13642 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13643 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013645 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013646 break;
13647 }
13648}
13649
Bram Moolenaar33570922005-01-25 22:26:29 +000013650static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013651
13652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013654 typval_T *argvars;
13655 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013656 int type;
13657{
13658#ifdef FEAT_LIBCALL
13659 char_u *string_in;
13660 char_u **string_result;
13661 int nr_result;
13662#endif
13663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013665 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013667
13668 if (check_restricted() || check_secure())
13669 return;
13670
13671#ifdef FEAT_LIBCALL
13672 /* The first two args must be strings, otherwise its meaningless */
13673 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13674 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013675 string_in = NULL;
13676 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013677 string_in = argvars[2].vval.v_string;
13678 if (type == VAR_NUMBER)
13679 string_result = NULL;
13680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013682 if (mch_libcall(argvars[0].vval.v_string,
13683 argvars[1].vval.v_string,
13684 string_in,
13685 argvars[2].vval.v_number,
13686 string_result,
13687 &nr_result) == OK
13688 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013690 }
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692}
13693
13694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013695 * "libcall()" function
13696 */
13697 static void
13698f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013699 typval_T *argvars;
13700 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013701{
13702 libcall_common(argvars, rettv, VAR_STRING);
13703}
13704
13705/*
13706 * "libcallnr()" function
13707 */
13708 static void
13709f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712{
13713 libcall_common(argvars, rettv, VAR_NUMBER);
13714}
13715
13716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 * "line(string)" function
13718 */
13719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *argvars;
13722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723{
13724 linenr_T lnum = 0;
13725 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013726 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013728 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 if (fp != NULL)
13730 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732}
13733
13734/*
13735 * "line2byte(lnum)" function
13736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013739 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
13742#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#else
13745 linenr_T lnum;
13746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013747 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13752 if (rettv->vval.v_number >= 0)
13753 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#endif
13755}
13756
13757/*
13758 * "lispindent(lnum)" function
13759 */
13760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013762 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764{
13765#ifdef FEAT_LISP
13766 pos_T pos;
13767 linenr_T lnum;
13768
13769 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13772 {
13773 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 curwin->w_cursor = pos;
13776 }
13777 else
13778#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780}
13781
13782/*
13783 * "localtime()" function
13784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013790 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791}
13792
Bram Moolenaar33570922005-01-25 22:26:29 +000013793static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794
13795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 int exact;
13800{
13801 char_u *keys;
13802 char_u *which;
13803 char_u buf[NUMBUFLEN];
13804 char_u *keys_buf = NULL;
13805 char_u *rhs;
13806 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013807 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013808 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013809 mapblock_T *mp;
13810 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811
13812 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813 rettv->v_type = VAR_STRING;
13814 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 if (*keys == NUL)
13818 return;
13819
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013820 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013823 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013824 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013825 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013826 if (argvars[3].v_type != VAR_UNKNOWN)
13827 get_dict = get_tv_number(&argvars[3]);
13828 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 else
13831 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 if (which == NULL)
13833 return;
13834
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 mode = get_map_mode(&which, 0);
13836
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013837 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013838 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013840
13841 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013843 /* Return a string. */
13844 if (rhs != NULL)
13845 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846
Bram Moolenaarbd743252010-10-20 21:23:33 +020013847 }
13848 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13849 {
13850 /* Return a dictionary. */
13851 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13852 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13853 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854
Bram Moolenaarbd743252010-10-20 21:23:33 +020013855 dict_add_nr_str(dict, "lhs", 0L, lhs);
13856 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13857 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13858 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13859 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13860 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13861 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013862 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013863 dict_add_nr_str(dict, "mode", 0L, mapmode);
13864
13865 vim_free(lhs);
13866 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 }
13868}
13869
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870#ifdef FEAT_FLOAT
13871/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013872 * "log()" function
13873 */
13874 static void
13875f_log(argvars, rettv)
13876 typval_T *argvars;
13877 typval_T *rettv;
13878{
13879 float_T f;
13880
13881 rettv->v_type = VAR_FLOAT;
13882 if (get_float_arg(argvars, &f) == OK)
13883 rettv->vval.v_float = log(f);
13884 else
13885 rettv->vval.v_float = 0.0;
13886}
13887
13888/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013889 * "log10()" function
13890 */
13891 static void
13892f_log10(argvars, rettv)
13893 typval_T *argvars;
13894 typval_T *rettv;
13895{
13896 float_T f;
13897
13898 rettv->v_type = VAR_FLOAT;
13899 if (get_float_arg(argvars, &f) == OK)
13900 rettv->vval.v_float = log10(f);
13901 else
13902 rettv->vval.v_float = 0.0;
13903}
13904#endif
13905
Bram Moolenaar1dced572012-04-05 16:54:08 +020013906#ifdef FEAT_LUA
13907/*
13908 * "luaeval()" function
13909 */
13910 static void
13911f_luaeval(argvars, rettv)
13912 typval_T *argvars;
13913 typval_T *rettv;
13914{
13915 char_u *str;
13916 char_u buf[NUMBUFLEN];
13917
13918 str = get_tv_string_buf(&argvars[0], buf);
13919 do_luaeval(str, argvars + 1, rettv);
13920}
13921#endif
13922
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013924 * "map()" function
13925 */
13926 static void
13927f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 typval_T *argvars;
13929 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013930{
13931 filter_map(argvars, rettv, TRUE);
13932}
13933
13934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013935 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 */
13937 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 typval_T *argvars;
13940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943}
13944
13945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013946 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947 */
13948 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013950 typval_T *argvars;
13951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954}
13955
Bram Moolenaar33570922005-01-25 22:26:29 +000013956static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957
13958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013960 typval_T *argvars;
13961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 int type;
13963{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013964 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013965 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967 char_u *pat;
13968 regmatch_T regmatch;
13969 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 char_u *save_cpo;
13972 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013973 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013974 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013975 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013976 list_T *l = NULL;
13977 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013978 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013979 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980
13981 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13982 save_cpo = p_cpo;
13983 p_cpo = (char_u *)"";
13984
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013985 rettv->vval.v_number = -1;
13986 if (type == 3)
13987 {
13988 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013989 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013990 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013991 }
13992 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 rettv->v_type = VAR_STRING;
13995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013998 if (argvars[0].v_type == VAR_LIST)
13999 {
14000 if ((l = argvars[0].vval.v_list) == NULL)
14001 goto theend;
14002 li = l->lv_first;
14003 }
14004 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014005 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014007 len = (long)STRLEN(str);
14008 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14011 if (pat == NULL)
14012 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014013
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 int error = FALSE;
14017
14018 start = get_tv_number_chk(&argvars[2], &error);
14019 if (error)
14020 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014021 if (l != NULL)
14022 {
14023 li = list_find(l, start);
14024 if (li == NULL)
14025 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014026 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014027 }
14028 else
14029 {
14030 if (start < 0)
14031 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014032 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014033 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014034 /* When "count" argument is there ignore matches before "start",
14035 * otherwise skip part of the string. Differs when pattern is "^"
14036 * or "\<". */
14037 if (argvars[3].v_type != VAR_UNKNOWN)
14038 startcol = start;
14039 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014040 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014041 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014042 len -= start;
14043 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014046 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 nth = get_tv_number_chk(&argvars[3], &error);
14048 if (error)
14049 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 }
14051
14052 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14053 if (regmatch.regprog != NULL)
14054 {
14055 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014056
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014057 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014058 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014059 if (l != NULL)
14060 {
14061 if (li == NULL)
14062 {
14063 match = FALSE;
14064 break;
14065 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014066 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014067 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014068 if (str == NULL)
14069 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014070 }
14071
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014072 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014073
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014074 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014075 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014076 if (l == NULL && !match)
14077 break;
14078
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014079 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014080 if (l != NULL)
14081 {
14082 li = li->li_next;
14083 ++idx;
14084 }
14085 else
14086 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014087#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014088 startcol = (colnr_T)(regmatch.startp[0]
14089 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014090#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014091 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014092#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014093 if (startcol > (colnr_T)len
14094 || str + startcol <= regmatch.startp[0])
14095 {
14096 match = FALSE;
14097 break;
14098 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014099 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014100 }
14101
14102 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014104 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014106 int i;
14107
14108 /* return list with matched string and submatches */
14109 for (i = 0; i < NSUBEXP; ++i)
14110 {
14111 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014112 {
14113 if (list_append_string(rettv->vval.v_list,
14114 (char_u *)"", 0) == FAIL)
14115 break;
14116 }
14117 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014118 regmatch.startp[i],
14119 (int)(regmatch.endp[i] - regmatch.startp[i]))
14120 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014121 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014122 }
14123 }
14124 else if (type == 2)
14125 {
14126 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014127 if (l != NULL)
14128 copy_tv(&li->li_tv, rettv);
14129 else
14130 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014132 }
14133 else if (l != NULL)
14134 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 else
14136 {
14137 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 (varnumber_T)(regmatch.startp[0] - str);
14140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014141 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014143 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 }
14145 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014146 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 }
14148
14149theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014150 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 p_cpo = save_cpo;
14152}
14153
14154/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014155 * "match()" function
14156 */
14157 static void
14158f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 typval_T *argvars;
14160 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161{
14162 find_some_match(argvars, rettv, 1);
14163}
14164
14165/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014166 * "matchadd()" function
14167 */
14168 static void
14169f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014170 typval_T *argvars UNUSED;
14171 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014172{
14173#ifdef FEAT_SEARCH_EXTRA
14174 char_u buf[NUMBUFLEN];
14175 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14176 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14177 int prio = 10; /* default priority */
14178 int id = -1;
14179 int error = FALSE;
14180
14181 rettv->vval.v_number = -1;
14182
14183 if (grp == NULL || pat == NULL)
14184 return;
14185 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014186 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014187 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014188 if (argvars[3].v_type != VAR_UNKNOWN)
14189 id = get_tv_number_chk(&argvars[3], &error);
14190 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014191 if (error == TRUE)
14192 return;
14193 if (id >= 1 && id <= 3)
14194 {
14195 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14196 return;
14197 }
14198
14199 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14200#endif
14201}
14202
14203/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014204 * "matcharg()" function
14205 */
14206 static void
14207f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014208 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014209 typval_T *rettv;
14210{
14211 if (rettv_list_alloc(rettv) == OK)
14212 {
14213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014214 int id = get_tv_number(&argvars[0]);
14215 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014216
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014217 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014218 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014219 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14220 {
14221 list_append_string(rettv->vval.v_list,
14222 syn_id2name(m->hlg_id), -1);
14223 list_append_string(rettv->vval.v_list, m->pattern, -1);
14224 }
14225 else
14226 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014227 list_append_string(rettv->vval.v_list, NULL, -1);
14228 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014229 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014230 }
14231#endif
14232 }
14233}
14234
14235/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014236 * "matchdelete()" function
14237 */
14238 static void
14239f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014240 typval_T *argvars UNUSED;
14241 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014242{
14243#ifdef FEAT_SEARCH_EXTRA
14244 rettv->vval.v_number = match_delete(curwin,
14245 (int)get_tv_number(&argvars[0]), TRUE);
14246#endif
14247}
14248
14249/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014250 * "matchend()" function
14251 */
14252 static void
14253f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014254 typval_T *argvars;
14255 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256{
14257 find_some_match(argvars, rettv, 0);
14258}
14259
14260/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014261 * "matchlist()" function
14262 */
14263 static void
14264f_matchlist(argvars, rettv)
14265 typval_T *argvars;
14266 typval_T *rettv;
14267{
14268 find_some_match(argvars, rettv, 3);
14269}
14270
14271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014272 * "matchstr()" function
14273 */
14274 static void
14275f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014278{
14279 find_some_match(argvars, rettv, 2);
14280}
14281
Bram Moolenaar33570922005-01-25 22:26:29 +000014282static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014283
14284 static void
14285max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014288 int domax;
14289{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014290 long n = 0;
14291 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014293
14294 if (argvars[0].v_type == VAR_LIST)
14295 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 list_T *l;
14297 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014298
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014299 l = argvars[0].vval.v_list;
14300 if (l != NULL)
14301 {
14302 li = l->lv_first;
14303 if (li != NULL)
14304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014306 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014307 {
14308 li = li->li_next;
14309 if (li == NULL)
14310 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014312 if (domax ? i > n : i < n)
14313 n = i;
14314 }
14315 }
14316 }
14317 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014318 else if (argvars[0].v_type == VAR_DICT)
14319 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014320 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014321 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014322 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014323 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014324
14325 d = argvars[0].vval.v_dict;
14326 if (d != NULL)
14327 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014328 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014332 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014333 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014335 if (first)
14336 {
14337 n = i;
14338 first = FALSE;
14339 }
14340 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014341 n = i;
14342 }
14343 }
14344 }
14345 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014346 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014347 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014349}
14350
14351/*
14352 * "max()" function
14353 */
14354 static void
14355f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 typval_T *argvars;
14357 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014358{
14359 max_min(argvars, rettv, TRUE);
14360}
14361
14362/*
14363 * "min()" function
14364 */
14365 static void
14366f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014367 typval_T *argvars;
14368 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014369{
14370 max_min(argvars, rettv, FALSE);
14371}
14372
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014373static int mkdir_recurse __ARGS((char_u *dir, int prot));
14374
14375/*
14376 * Create the directory in which "dir" is located, and higher levels when
14377 * needed.
14378 */
14379 static int
14380mkdir_recurse(dir, prot)
14381 char_u *dir;
14382 int prot;
14383{
14384 char_u *p;
14385 char_u *updir;
14386 int r = FAIL;
14387
14388 /* Get end of directory name in "dir".
14389 * We're done when it's "/" or "c:/". */
14390 p = gettail_sep(dir);
14391 if (p <= get_past_head(dir))
14392 return OK;
14393
14394 /* If the directory exists we're done. Otherwise: create it.*/
14395 updir = vim_strnsave(dir, (int)(p - dir));
14396 if (updir == NULL)
14397 return FAIL;
14398 if (mch_isdir(updir))
14399 r = OK;
14400 else if (mkdir_recurse(updir, prot) == OK)
14401 r = vim_mkdir_emsg(updir, prot);
14402 vim_free(updir);
14403 return r;
14404}
14405
14406#ifdef vim_mkdir
14407/*
14408 * "mkdir()" function
14409 */
14410 static void
14411f_mkdir(argvars, rettv)
14412 typval_T *argvars;
14413 typval_T *rettv;
14414{
14415 char_u *dir;
14416 char_u buf[NUMBUFLEN];
14417 int prot = 0755;
14418
14419 rettv->vval.v_number = FAIL;
14420 if (check_restricted() || check_secure())
14421 return;
14422
14423 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014424 if (*dir == NUL)
14425 rettv->vval.v_number = FAIL;
14426 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014427 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014428 if (*gettail(dir) == NUL)
14429 /* remove trailing slashes */
14430 *gettail_sep(dir) = NUL;
14431
14432 if (argvars[1].v_type != VAR_UNKNOWN)
14433 {
14434 if (argvars[2].v_type != VAR_UNKNOWN)
14435 prot = get_tv_number_chk(&argvars[2], NULL);
14436 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14437 mkdir_recurse(dir, prot);
14438 }
14439 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014440 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014441}
14442#endif
14443
Bram Moolenaar0d660222005-01-07 21:51:51 +000014444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 * "mode()" function
14446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014449 typval_T *argvars;
14450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014452 char_u buf[3];
14453
14454 buf[1] = NUL;
14455 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 if (VIsual_active)
14458 {
14459 if (VIsual_select)
14460 buf[0] = VIsual_mode + 's' - 'v';
14461 else
14462 buf[0] = VIsual_mode;
14463 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014464 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014465 || State == CONFIRM)
14466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014468 if (State == ASKMORE)
14469 buf[1] = 'm';
14470 else if (State == CONFIRM)
14471 buf[1] = '?';
14472 }
14473 else if (State == EXTERNCMD)
14474 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 else if (State & INSERT)
14476 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014477#ifdef FEAT_VREPLACE
14478 if (State & VREPLACE_FLAG)
14479 {
14480 buf[0] = 'R';
14481 buf[1] = 'v';
14482 }
14483 else
14484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 if (State & REPLACE_FLAG)
14486 buf[0] = 'R';
14487 else
14488 buf[0] = 'i';
14489 }
14490 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014493 if (exmode_active)
14494 buf[1] = 'v';
14495 }
14496 else if (exmode_active)
14497 {
14498 buf[0] = 'c';
14499 buf[1] = 'e';
14500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014504 if (finish_op)
14505 buf[1] = 'o';
14506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014508 /* Clear out the minor mode when the argument is not a non-zero number or
14509 * non-empty string. */
14510 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014511 buf[1] = NUL;
14512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014513 rettv->vval.v_string = vim_strsave(buf);
14514 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515}
14516
Bram Moolenaar429fa852013-04-15 12:27:36 +020014517#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014518/*
14519 * "mzeval()" function
14520 */
14521 static void
14522f_mzeval(argvars, rettv)
14523 typval_T *argvars;
14524 typval_T *rettv;
14525{
14526 char_u *str;
14527 char_u buf[NUMBUFLEN];
14528
14529 str = get_tv_string_buf(&argvars[0], buf);
14530 do_mzeval(str, rettv);
14531}
Bram Moolenaar75676462013-01-30 14:55:42 +010014532
14533 void
14534mzscheme_call_vim(name, args, rettv)
14535 char_u *name;
14536 typval_T *args;
14537 typval_T *rettv;
14538{
14539 typval_T argvars[3];
14540
14541 argvars[0].v_type = VAR_STRING;
14542 argvars[0].vval.v_string = name;
14543 copy_tv(args, &argvars[1]);
14544 argvars[2].v_type = VAR_UNKNOWN;
14545 f_call(argvars, rettv);
14546 clear_tv(&argvars[1]);
14547}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014548#endif
14549
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551 * "nextnonblank()" function
14552 */
14553 static void
14554f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014555 typval_T *argvars;
14556 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557{
14558 linenr_T lnum;
14559
14560 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563 {
14564 lnum = 0;
14565 break;
14566 }
14567 if (*skipwhite(ml_get(lnum)) != NUL)
14568 break;
14569 }
14570 rettv->vval.v_number = lnum;
14571}
14572
14573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 * "nr2char()" function
14575 */
14576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 typval_T *argvars;
14579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580{
14581 char_u buf[NUMBUFLEN];
14582
14583#ifdef FEAT_MBYTE
14584 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014585 {
14586 int utf8 = 0;
14587
14588 if (argvars[1].v_type != VAR_UNKNOWN)
14589 utf8 = get_tv_number_chk(&argvars[1], NULL);
14590 if (utf8)
14591 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14592 else
14593 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 else
14596#endif
14597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 buf[1] = NUL;
14600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv->v_type = VAR_STRING;
14602 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014603}
14604
14605/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014606 * "or(expr, expr)" function
14607 */
14608 static void
14609f_or(argvars, rettv)
14610 typval_T *argvars;
14611 typval_T *rettv;
14612{
14613 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14614 | get_tv_number_chk(&argvars[1], NULL);
14615}
14616
14617/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014618 * "pathshorten()" function
14619 */
14620 static void
14621f_pathshorten(argvars, rettv)
14622 typval_T *argvars;
14623 typval_T *rettv;
14624{
14625 char_u *p;
14626
14627 rettv->v_type = VAR_STRING;
14628 p = get_tv_string_chk(&argvars[0]);
14629 if (p == NULL)
14630 rettv->vval.v_string = NULL;
14631 else
14632 {
14633 p = vim_strsave(p);
14634 rettv->vval.v_string = p;
14635 if (p != NULL)
14636 shorten_dir(p);
14637 }
14638}
14639
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014640#ifdef FEAT_FLOAT
14641/*
14642 * "pow()" function
14643 */
14644 static void
14645f_pow(argvars, rettv)
14646 typval_T *argvars;
14647 typval_T *rettv;
14648{
14649 float_T fx, fy;
14650
14651 rettv->v_type = VAR_FLOAT;
14652 if (get_float_arg(argvars, &fx) == OK
14653 && get_float_arg(&argvars[1], &fy) == OK)
14654 rettv->vval.v_float = pow(fx, fy);
14655 else
14656 rettv->vval.v_float = 0.0;
14657}
14658#endif
14659
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014660/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014661 * "prevnonblank()" function
14662 */
14663 static void
14664f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 typval_T *argvars;
14666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014667{
14668 linenr_T lnum;
14669
14670 lnum = get_tv_lnum(argvars);
14671 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14672 lnum = 0;
14673 else
14674 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14675 --lnum;
14676 rettv->vval.v_number = lnum;
14677}
14678
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014679#ifdef HAVE_STDARG_H
14680/* This dummy va_list is here because:
14681 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14682 * - locally in the function results in a "used before set" warning
14683 * - using va_start() to initialize it gives "function with fixed args" error */
14684static va_list ap;
14685#endif
14686
Bram Moolenaar8c711452005-01-14 21:53:12 +000014687/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014688 * "printf()" function
14689 */
14690 static void
14691f_printf(argvars, rettv)
14692 typval_T *argvars;
14693 typval_T *rettv;
14694{
14695 rettv->v_type = VAR_STRING;
14696 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014697#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014698 {
14699 char_u buf[NUMBUFLEN];
14700 int len;
14701 char_u *s;
14702 int saved_did_emsg = did_emsg;
14703 char *fmt;
14704
14705 /* Get the required length, allocate the buffer and do it for real. */
14706 did_emsg = FALSE;
14707 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014708 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014709 if (!did_emsg)
14710 {
14711 s = alloc(len + 1);
14712 if (s != NULL)
14713 {
14714 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014715 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014716 }
14717 }
14718 did_emsg |= saved_did_emsg;
14719 }
14720#endif
14721}
14722
14723/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014724 * "pumvisible()" function
14725 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014726 static void
14727f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014728 typval_T *argvars UNUSED;
14729 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014730{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014731#ifdef FEAT_INS_EXPAND
14732 if (pum_visible())
14733 rettv->vval.v_number = 1;
14734#endif
14735}
14736
Bram Moolenaardb913952012-06-29 12:54:53 +020014737#ifdef FEAT_PYTHON3
14738/*
14739 * "py3eval()" function
14740 */
14741 static void
14742f_py3eval(argvars, rettv)
14743 typval_T *argvars;
14744 typval_T *rettv;
14745{
14746 char_u *str;
14747 char_u buf[NUMBUFLEN];
14748
14749 str = get_tv_string_buf(&argvars[0], buf);
14750 do_py3eval(str, rettv);
14751}
14752#endif
14753
14754#ifdef FEAT_PYTHON
14755/*
14756 * "pyeval()" function
14757 */
14758 static void
14759f_pyeval(argvars, rettv)
14760 typval_T *argvars;
14761 typval_T *rettv;
14762{
14763 char_u *str;
14764 char_u buf[NUMBUFLEN];
14765
14766 str = get_tv_string_buf(&argvars[0], buf);
14767 do_pyeval(str, rettv);
14768}
14769#endif
14770
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014771/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014772 * "range()" function
14773 */
14774 static void
14775f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014776 typval_T *argvars;
14777 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014778{
14779 long start;
14780 long end;
14781 long stride = 1;
14782 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014783 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014786 if (argvars[1].v_type == VAR_UNKNOWN)
14787 {
14788 end = start - 1;
14789 start = 0;
14790 }
14791 else
14792 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014795 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014796 }
14797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014798 if (error)
14799 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014800 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014801 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014802 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014803 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014804 else
14805 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014806 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014807 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014808 if (list_append_number(rettv->vval.v_list,
14809 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014810 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811 }
14812}
14813
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014814/*
14815 * "readfile()" function
14816 */
14817 static void
14818f_readfile(argvars, rettv)
14819 typval_T *argvars;
14820 typval_T *rettv;
14821{
14822 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014824 char_u *fname;
14825 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014826 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14827 int io_size = sizeof(buf);
14828 int readlen; /* size of last fread() */
14829 char_u *prev = NULL; /* previously read bytes, if any */
14830 long prevlen = 0; /* length of data in prev */
14831 long prevsize = 0; /* size of prev buffer */
14832 long maxline = MAXLNUM;
14833 long cnt = 0;
14834 char_u *p; /* position in buf */
14835 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014837 if (argvars[1].v_type != VAR_UNKNOWN)
14838 {
14839 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14840 binary = TRUE;
14841 if (argvars[2].v_type != VAR_UNKNOWN)
14842 maxline = get_tv_number(&argvars[2]);
14843 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014844
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014845 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014846 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014847
14848 /* Always open the file in binary mode, library functions have a mind of
14849 * their own about CR-LF conversion. */
14850 fname = get_tv_string(&argvars[0]);
14851 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14852 {
14853 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14854 return;
14855 }
14856
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014857 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014859 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014861 /* This for loop processes what was read, but is also entered at end
14862 * of file so that either:
14863 * - an incomplete line gets written
14864 * - a "binary" file gets an empty line at the end if it ends in a
14865 * newline. */
14866 for (p = buf, start = buf;
14867 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14868 ++p)
14869 {
14870 if (*p == '\n' || readlen <= 0)
14871 {
14872 listitem_T *li;
14873 char_u *s = NULL;
14874 long_u len = p - start;
14875
14876 /* Finished a line. Remove CRs before NL. */
14877 if (readlen > 0 && !binary)
14878 {
14879 while (len > 0 && start[len - 1] == '\r')
14880 --len;
14881 /* removal may cross back to the "prev" string */
14882 if (len == 0)
14883 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14884 --prevlen;
14885 }
14886 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014887 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014888 else
14889 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014890 /* Change "prev" buffer to be the right size. This way
14891 * the bytes are only copied once, and very long lines are
14892 * allocated only once. */
14893 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014894 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014895 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014896 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014897 prev = NULL; /* the list will own the string */
14898 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899 }
14900 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 if (s == NULL)
14902 {
14903 do_outofmem_msg((long_u) prevlen + len + 1);
14904 failed = TRUE;
14905 break;
14906 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014907
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014908 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014909 {
14910 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014912 break;
14913 }
14914 li->li_tv.v_type = VAR_STRING;
14915 li->li_tv.v_lock = 0;
14916 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014917 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014918
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014919 start = p + 1; /* step over newline */
14920 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014921 break;
14922 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014923 else if (*p == NUL)
14924 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014925#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014926 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14927 * when finding the BF and check the previous two bytes. */
14928 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014929 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14931 * + 1, these may be in the "prev" string. */
14932 char_u back1 = p >= buf + 1 ? p[-1]
14933 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14934 char_u back2 = p >= buf + 2 ? p[-2]
14935 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14936 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014937
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014938 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014939 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014940 char_u *dest = p - 2;
14941
14942 /* Usually a BOM is at the beginning of a file, and so at
14943 * the beginning of a line; then we can just step over it.
14944 */
14945 if (start == dest)
14946 start = p + 1;
14947 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014948 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 /* have to shuffle buf to close gap */
14950 int adjust_prevlen = 0;
14951
14952 if (dest < buf)
14953 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014954 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014955 dest = buf;
14956 }
14957 if (readlen > p - buf + 1)
14958 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14959 readlen -= 3 - adjust_prevlen;
14960 prevlen -= adjust_prevlen;
14961 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014962 }
14963 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014964 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014965#endif
14966 } /* for */
14967
14968 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14969 break;
14970 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014971 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014972 /* There's part of a line in buf, store it in "prev". */
14973 if (p - start + prevlen >= prevsize)
14974 {
14975 /* need bigger "prev" buffer */
14976 char_u *newprev;
14977
14978 /* A common use case is ordinary text files and "prev" gets a
14979 * fragment of a line, so the first allocation is made
14980 * small, to avoid repeatedly 'allocing' large and
14981 * 'reallocing' small. */
14982 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014983 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014984 else
14985 {
14986 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014987 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014988 prevsize = grow50pc > growmin ? grow50pc : growmin;
14989 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014990 newprev = prev == NULL ? alloc(prevsize)
14991 : vim_realloc(prev, prevsize);
14992 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014993 {
14994 do_outofmem_msg((long_u)prevsize);
14995 failed = TRUE;
14996 break;
14997 }
14998 prev = newprev;
14999 }
15000 /* Add the line part to end of "prev". */
15001 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015002 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015003 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015004 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015005
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015006 /*
15007 * For a negative line count use only the lines at the end of the file,
15008 * free the rest.
15009 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015010 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015011 while (cnt > -maxline)
15012 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015014 --cnt;
15015 }
15016
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015017 if (failed)
15018 {
15019 list_free(rettv->vval.v_list, TRUE);
15020 /* readfile doc says an empty list is returned on error */
15021 rettv->vval.v_list = list_alloc();
15022 }
15023
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015024 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015025 fclose(fd);
15026}
15027
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015028#if defined(FEAT_RELTIME)
15029static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15030
15031/*
15032 * Convert a List to proftime_T.
15033 * Return FAIL when there is something wrong.
15034 */
15035 static int
15036list2proftime(arg, tm)
15037 typval_T *arg;
15038 proftime_T *tm;
15039{
15040 long n1, n2;
15041 int error = FALSE;
15042
15043 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15044 || arg->vval.v_list->lv_len != 2)
15045 return FAIL;
15046 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15047 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15048# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015049 tm->HighPart = n1;
15050 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015051# else
15052 tm->tv_sec = n1;
15053 tm->tv_usec = n2;
15054# endif
15055 return error ? FAIL : OK;
15056}
15057#endif /* FEAT_RELTIME */
15058
15059/*
15060 * "reltime()" function
15061 */
15062 static void
15063f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015064 typval_T *argvars UNUSED;
15065 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015066{
15067#ifdef FEAT_RELTIME
15068 proftime_T res;
15069 proftime_T start;
15070
15071 if (argvars[0].v_type == VAR_UNKNOWN)
15072 {
15073 /* No arguments: get current time. */
15074 profile_start(&res);
15075 }
15076 else if (argvars[1].v_type == VAR_UNKNOWN)
15077 {
15078 if (list2proftime(&argvars[0], &res) == FAIL)
15079 return;
15080 profile_end(&res);
15081 }
15082 else
15083 {
15084 /* Two arguments: compute the difference. */
15085 if (list2proftime(&argvars[0], &start) == FAIL
15086 || list2proftime(&argvars[1], &res) == FAIL)
15087 return;
15088 profile_sub(&res, &start);
15089 }
15090
15091 if (rettv_list_alloc(rettv) == OK)
15092 {
15093 long n1, n2;
15094
15095# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015096 n1 = res.HighPart;
15097 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015098# else
15099 n1 = res.tv_sec;
15100 n2 = res.tv_usec;
15101# endif
15102 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15103 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15104 }
15105#endif
15106}
15107
15108/*
15109 * "reltimestr()" function
15110 */
15111 static void
15112f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015113 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015114 typval_T *rettv;
15115{
15116#ifdef FEAT_RELTIME
15117 proftime_T tm;
15118#endif
15119
15120 rettv->v_type = VAR_STRING;
15121 rettv->vval.v_string = NULL;
15122#ifdef FEAT_RELTIME
15123 if (list2proftime(&argvars[0], &tm) == OK)
15124 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15125#endif
15126}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015127
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15129static void make_connection __ARGS((void));
15130static int check_connection __ARGS((void));
15131
15132 static void
15133make_connection()
15134{
15135 if (X_DISPLAY == NULL
15136# ifdef FEAT_GUI
15137 && !gui.in_use
15138# endif
15139 )
15140 {
15141 x_force_connect = TRUE;
15142 setup_term_clip();
15143 x_force_connect = FALSE;
15144 }
15145}
15146
15147 static int
15148check_connection()
15149{
15150 make_connection();
15151 if (X_DISPLAY == NULL)
15152 {
15153 EMSG(_("E240: No connection to Vim server"));
15154 return FAIL;
15155 }
15156 return OK;
15157}
15158#endif
15159
15160#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015161static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162
15163 static void
15164remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015165 typval_T *argvars;
15166 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 int expr;
15168{
15169 char_u *server_name;
15170 char_u *keys;
15171 char_u *r = NULL;
15172 char_u buf[NUMBUFLEN];
15173# ifdef WIN32
15174 HWND w;
15175# else
15176 Window w;
15177# endif
15178
15179 if (check_restricted() || check_secure())
15180 return;
15181
15182# ifdef FEAT_X11
15183 if (check_connection() == FAIL)
15184 return;
15185# endif
15186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015187 server_name = get_tv_string_chk(&argvars[0]);
15188 if (server_name == NULL)
15189 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015190 keys = get_tv_string_buf(&argvars[1], buf);
15191# ifdef WIN32
15192 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15193# else
15194 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15195 < 0)
15196# endif
15197 {
15198 if (r != NULL)
15199 EMSG(r); /* sending worked but evaluation failed */
15200 else
15201 EMSG2(_("E241: Unable to send to %s"), server_name);
15202 return;
15203 }
15204
15205 rettv->vval.v_string = r;
15206
15207 if (argvars[2].v_type != VAR_UNKNOWN)
15208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015210 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015213 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015214 v.di_tv.v_type = VAR_STRING;
15215 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 idvar = get_tv_string_chk(&argvars[2]);
15217 if (idvar != NULL)
15218 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015220 }
15221}
15222#endif
15223
15224/*
15225 * "remote_expr()" function
15226 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227 static void
15228f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015229 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015231{
15232 rettv->v_type = VAR_STRING;
15233 rettv->vval.v_string = NULL;
15234#ifdef FEAT_CLIENTSERVER
15235 remote_common(argvars, rettv, TRUE);
15236#endif
15237}
15238
15239/*
15240 * "remote_foreground()" function
15241 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 static void
15243f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015244 typval_T *argvars UNUSED;
15245 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247#ifdef FEAT_CLIENTSERVER
15248# ifdef WIN32
15249 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 {
15251 char_u *server_name = get_tv_string_chk(&argvars[0]);
15252
15253 if (server_name != NULL)
15254 serverForeground(server_name);
15255 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256# else
15257 /* Send a foreground() expression to the server. */
15258 argvars[1].v_type = VAR_STRING;
15259 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15260 argvars[2].v_type = VAR_UNKNOWN;
15261 remote_common(argvars, rettv, TRUE);
15262 vim_free(argvars[1].vval.v_string);
15263# endif
15264#endif
15265}
15266
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267 static void
15268f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015269 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015270 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271{
15272#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 char_u *s = NULL;
15275# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015276 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279
15280 if (check_restricted() || check_secure())
15281 {
15282 rettv->vval.v_number = -1;
15283 return;
15284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285 serverid = get_tv_string_chk(&argvars[0]);
15286 if (serverid == NULL)
15287 {
15288 rettv->vval.v_number = -1;
15289 return; /* type error; errmsg already given */
15290 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015291# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015292 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015293 if (n == 0)
15294 rettv->vval.v_number = -1;
15295 else
15296 {
15297 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15298 rettv->vval.v_number = (s != NULL);
15299 }
15300# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301 if (check_connection() == FAIL)
15302 return;
15303
15304 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015305 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306# endif
15307
15308 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 char_u *retvar;
15311
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 v.di_tv.v_type = VAR_STRING;
15313 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314 retvar = get_tv_string_chk(&argvars[1]);
15315 if (retvar != NULL)
15316 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318 }
15319#else
15320 rettv->vval.v_number = -1;
15321#endif
15322}
15323
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324 static void
15325f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015326 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015327 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328{
15329 char_u *r = NULL;
15330
15331#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015332 char_u *serverid = get_tv_string_chk(&argvars[0]);
15333
15334 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015335 {
15336# ifdef WIN32
15337 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015338 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015340 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341 if (n != 0)
15342 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15343 if (r == NULL)
15344# else
15345 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015347# endif
15348 EMSG(_("E277: Unable to read a server reply"));
15349 }
15350#endif
15351 rettv->v_type = VAR_STRING;
15352 rettv->vval.v_string = r;
15353}
15354
15355/*
15356 * "remote_send()" function
15357 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015358 static void
15359f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015362{
15363 rettv->v_type = VAR_STRING;
15364 rettv->vval.v_string = NULL;
15365#ifdef FEAT_CLIENTSERVER
15366 remote_common(argvars, rettv, FALSE);
15367#endif
15368}
15369
15370/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015371 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015372 */
15373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015375 typval_T *argvars;
15376 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015377{
Bram Moolenaar33570922005-01-25 22:26:29 +000015378 list_T *l;
15379 listitem_T *item, *item2;
15380 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015381 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015382 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015383 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015384 dict_T *d;
15385 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015386 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015387
Bram Moolenaar8c711452005-01-14 21:53:12 +000015388 if (argvars[0].v_type == VAR_DICT)
15389 {
15390 if (argvars[2].v_type != VAR_UNKNOWN)
15391 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015392 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015393 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015395 key = get_tv_string_chk(&argvars[1]);
15396 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015398 di = dict_find(d, key, -1);
15399 if (di == NULL)
15400 EMSG2(_(e_dictkey), key);
15401 else
15402 {
15403 *rettv = di->di_tv;
15404 init_tv(&di->di_tv);
15405 dictitem_remove(d, di);
15406 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015407 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015408 }
15409 }
15410 else if (argvars[0].v_type != VAR_LIST)
15411 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015412 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015413 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015415 int error = FALSE;
15416
15417 idx = get_tv_number_chk(&argvars[1], &error);
15418 if (error)
15419 ; /* type error: do nothing, errmsg already given */
15420 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015421 EMSGN(_(e_listidx), idx);
15422 else
15423 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015424 if (argvars[2].v_type == VAR_UNKNOWN)
15425 {
15426 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015427 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015428 *rettv = item->li_tv;
15429 vim_free(item);
15430 }
15431 else
15432 {
15433 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 end = get_tv_number_chk(&argvars[2], &error);
15435 if (error)
15436 ; /* type error: do nothing */
15437 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015438 EMSGN(_(e_listidx), end);
15439 else
15440 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015441 int cnt = 0;
15442
15443 for (li = item; li != NULL; li = li->li_next)
15444 {
15445 ++cnt;
15446 if (li == item2)
15447 break;
15448 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015449 if (li == NULL) /* didn't find "item2" after "item" */
15450 EMSG(_(e_invrange));
15451 else
15452 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015453 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015454 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015455 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015456 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015457 l->lv_first = item;
15458 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015459 item->li_prev = NULL;
15460 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015461 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015462 }
15463 }
15464 }
15465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015466 }
15467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468}
15469
15470/*
15471 * "rename({from}, {to})" function
15472 */
15473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015475 typval_T *argvars;
15476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477{
15478 char_u buf[NUMBUFLEN];
15479
15480 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15484 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485}
15486
15487/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015488 * "repeat()" function
15489 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015490 static void
15491f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 typval_T *argvars;
15493 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015494{
15495 char_u *p;
15496 int n;
15497 int slen;
15498 int len;
15499 char_u *r;
15500 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015501
15502 n = get_tv_number(&argvars[1]);
15503 if (argvars[0].v_type == VAR_LIST)
15504 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015505 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015506 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015507 if (list_extend(rettv->vval.v_list,
15508 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015509 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015510 }
15511 else
15512 {
15513 p = get_tv_string(&argvars[0]);
15514 rettv->v_type = VAR_STRING;
15515 rettv->vval.v_string = NULL;
15516
15517 slen = (int)STRLEN(p);
15518 len = slen * n;
15519 if (len <= 0)
15520 return;
15521
15522 r = alloc(len + 1);
15523 if (r != NULL)
15524 {
15525 for (i = 0; i < n; i++)
15526 mch_memmove(r + i * slen, p, (size_t)slen);
15527 r[len] = NUL;
15528 }
15529
15530 rettv->vval.v_string = r;
15531 }
15532}
15533
15534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 * "resolve()" function
15536 */
15537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *argvars;
15540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541{
15542 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015543#ifdef HAVE_READLINK
15544 char_u *buf = NULL;
15545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548#ifdef FEAT_SHORTCUT
15549 {
15550 char_u *v = NULL;
15551
15552 v = mch_resolve_shortcut(p);
15553 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 }
15558#else
15559# ifdef HAVE_READLINK
15560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 char_u *cpy;
15562 int len;
15563 char_u *remain = NULL;
15564 char_u *q;
15565 int is_relative_to_current = FALSE;
15566 int has_trailing_pathsep = FALSE;
15567 int limit = 100;
15568
15569 p = vim_strsave(p);
15570
15571 if (p[0] == '.' && (vim_ispathsep(p[1])
15572 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15573 is_relative_to_current = TRUE;
15574
15575 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015576 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015579 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
15582 q = getnextcomp(p);
15583 if (*q != NUL)
15584 {
15585 /* Separate the first path component in "p", and keep the
15586 * remainder (beginning with the path separator). */
15587 remain = vim_strsave(q - 1);
15588 q[-1] = NUL;
15589 }
15590
Bram Moolenaard9462e32011-04-11 21:35:11 +020015591 buf = alloc(MAXPATHL + 1);
15592 if (buf == NULL)
15593 goto fail;
15594
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 for (;;)
15596 {
15597 for (;;)
15598 {
15599 len = readlink((char *)p, (char *)buf, MAXPATHL);
15600 if (len <= 0)
15601 break;
15602 buf[len] = NUL;
15603
15604 if (limit-- == 0)
15605 {
15606 vim_free(p);
15607 vim_free(remain);
15608 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 goto fail;
15611 }
15612
15613 /* Ensure that the result will have a trailing path separator
15614 * if the argument has one. */
15615 if (remain == NULL && has_trailing_pathsep)
15616 add_pathsep(buf);
15617
15618 /* Separate the first path component in the link value and
15619 * concatenate the remainders. */
15620 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15621 if (*q != NUL)
15622 {
15623 if (remain == NULL)
15624 remain = vim_strsave(q - 1);
15625 else
15626 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015627 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 if (cpy != NULL)
15629 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 vim_free(remain);
15631 remain = cpy;
15632 }
15633 }
15634 q[-1] = NUL;
15635 }
15636
15637 q = gettail(p);
15638 if (q > p && *q == NUL)
15639 {
15640 /* Ignore trailing path separator. */
15641 q[-1] = NUL;
15642 q = gettail(p);
15643 }
15644 if (q > p && !mch_isFullName(buf))
15645 {
15646 /* symlink is relative to directory of argument */
15647 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15648 if (cpy != NULL)
15649 {
15650 STRCPY(cpy, p);
15651 STRCPY(gettail(cpy), buf);
15652 vim_free(p);
15653 p = cpy;
15654 }
15655 }
15656 else
15657 {
15658 vim_free(p);
15659 p = vim_strsave(buf);
15660 }
15661 }
15662
15663 if (remain == NULL)
15664 break;
15665
15666 /* Append the first path component of "remain" to "p". */
15667 q = getnextcomp(remain + 1);
15668 len = q - remain - (*q != NUL);
15669 cpy = vim_strnsave(p, STRLEN(p) + len);
15670 if (cpy != NULL)
15671 {
15672 STRNCAT(cpy, remain, len);
15673 vim_free(p);
15674 p = cpy;
15675 }
15676 /* Shorten "remain". */
15677 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015678 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 else
15680 {
15681 vim_free(remain);
15682 remain = NULL;
15683 }
15684 }
15685
15686 /* If the result is a relative path name, make it explicitly relative to
15687 * the current directory if and only if the argument had this form. */
15688 if (!vim_ispathsep(*p))
15689 {
15690 if (is_relative_to_current
15691 && *p != NUL
15692 && !(p[0] == '.'
15693 && (p[1] == NUL
15694 || vim_ispathsep(p[1])
15695 || (p[1] == '.'
15696 && (p[2] == NUL
15697 || vim_ispathsep(p[2]))))))
15698 {
15699 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015700 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 if (cpy != NULL)
15702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 vim_free(p);
15704 p = cpy;
15705 }
15706 }
15707 else if (!is_relative_to_current)
15708 {
15709 /* Strip leading "./". */
15710 q = p;
15711 while (q[0] == '.' && vim_ispathsep(q[1]))
15712 q += 2;
15713 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015714 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 }
15716 }
15717
15718 /* Ensure that the result will have no trailing path separator
15719 * if the argument had none. But keep "/" or "//". */
15720 if (!has_trailing_pathsep)
15721 {
15722 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015723 if (after_pathsep(p, q))
15724 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 }
15726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 }
15729# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731# endif
15732#endif
15733
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015734 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
15736#ifdef HAVE_READLINK
15737fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015738 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741}
15742
15743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 */
15746 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 typval_T *argvars;
15749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750{
Bram Moolenaar33570922005-01-25 22:26:29 +000015751 list_T *l;
15752 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753
Bram Moolenaar0d660222005-01-07 21:51:51 +000015754 if (argvars[0].v_type != VAR_LIST)
15755 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015756 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015757 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015758 {
15759 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015760 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015761 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015762 while (li != NULL)
15763 {
15764 ni = li->li_prev;
15765 list_append(l, li);
15766 li = ni;
15767 }
15768 rettv->vval.v_list = l;
15769 rettv->v_type = VAR_LIST;
15770 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015771 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773}
15774
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015775#define SP_NOMOVE 0x01 /* don't move cursor */
15776#define SP_REPEAT 0x02 /* repeat to find outer pair */
15777#define SP_RETCOUNT 0x04 /* return matchcount */
15778#define SP_SETPCMARK 0x08 /* set previous context mark */
15779#define SP_START 0x10 /* accept match at start position */
15780#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15781#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015782
Bram Moolenaar33570922005-01-25 22:26:29 +000015783static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784
15785/*
15786 * Get flags for a search function.
15787 * Possibly sets "p_ws".
15788 * Returns BACKWARD, FORWARD or zero (for an error).
15789 */
15790 static int
15791get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015792 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 int *flagsp;
15794{
15795 int dir = FORWARD;
15796 char_u *flags;
15797 char_u nbuf[NUMBUFLEN];
15798 int mask;
15799
15800 if (varp->v_type != VAR_UNKNOWN)
15801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015802 flags = get_tv_string_buf_chk(varp, nbuf);
15803 if (flags == NULL)
15804 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805 while (*flags != NUL)
15806 {
15807 switch (*flags)
15808 {
15809 case 'b': dir = BACKWARD; break;
15810 case 'w': p_ws = TRUE; break;
15811 case 'W': p_ws = FALSE; break;
15812 default: mask = 0;
15813 if (flagsp != NULL)
15814 switch (*flags)
15815 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015816 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015817 case 'e': mask = SP_END; break;
15818 case 'm': mask = SP_RETCOUNT; break;
15819 case 'n': mask = SP_NOMOVE; break;
15820 case 'p': mask = SP_SUBPAT; break;
15821 case 'r': mask = SP_REPEAT; break;
15822 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 }
15824 if (mask == 0)
15825 {
15826 EMSG2(_(e_invarg2), flags);
15827 dir = 0;
15828 }
15829 else
15830 *flagsp |= mask;
15831 }
15832 if (dir == 0)
15833 break;
15834 ++flags;
15835 }
15836 }
15837 return dir;
15838}
15839
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015841 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015843 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015844search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015845 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015846 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015847 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015849 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 char_u *pat;
15851 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015852 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 int save_p_ws = p_ws;
15854 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015856 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015857 proftime_T tm;
15858#ifdef FEAT_RELTIME
15859 long time_limit = 0;
15860#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015861 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015862 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015864 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015865 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015866 if (dir == 0)
15867 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015868 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015869 if (flags & SP_START)
15870 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015871 if (flags & SP_END)
15872 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015873
Bram Moolenaar76929292008-01-06 19:07:36 +000015874 /* Optional arguments: line number to stop searching and timeout. */
15875 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015876 {
15877 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15878 if (lnum_stop < 0)
15879 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015880#ifdef FEAT_RELTIME
15881 if (argvars[3].v_type != VAR_UNKNOWN)
15882 {
15883 time_limit = get_tv_number_chk(&argvars[3], NULL);
15884 if (time_limit < 0)
15885 goto theend;
15886 }
15887#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015888 }
15889
Bram Moolenaar76929292008-01-06 19:07:36 +000015890#ifdef FEAT_RELTIME
15891 /* Set the time limit, if there is one. */
15892 profile_setlimit(time_limit, &tm);
15893#endif
15894
Bram Moolenaar231334e2005-07-25 20:46:57 +000015895 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015896 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015897 * Check to make sure only those flags are set.
15898 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15899 * flags cannot be set. Check for that condition also.
15900 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015901 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015902 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015904 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015905 goto theend;
15906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015908 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015909 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015910 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015911 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015913 if (flags & SP_SUBPAT)
15914 retval = subpatnum;
15915 else
15916 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015917 if (flags & SP_SETPCMARK)
15918 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015920 if (match_pos != NULL)
15921 {
15922 /* Store the match cursor position */
15923 match_pos->lnum = pos.lnum;
15924 match_pos->col = pos.col + 1;
15925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 /* "/$" will put the cursor after the end of the line, may need to
15927 * correct that here */
15928 check_cursor();
15929 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015930
15931 /* If 'n' flag is used: restore cursor position. */
15932 if (flags & SP_NOMOVE)
15933 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015934 else
15935 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015936theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015938
15939 return retval;
15940}
15941
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015942#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015943
15944/*
15945 * round() is not in C90, use ceil() or floor() instead.
15946 */
15947 float_T
15948vim_round(f)
15949 float_T f;
15950{
15951 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15952}
15953
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015954/*
15955 * "round({float})" function
15956 */
15957 static void
15958f_round(argvars, rettv)
15959 typval_T *argvars;
15960 typval_T *rettv;
15961{
15962 float_T f;
15963
15964 rettv->v_type = VAR_FLOAT;
15965 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015966 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015967 else
15968 rettv->vval.v_float = 0.0;
15969}
15970#endif
15971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015972/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015973 * "screenattr()" function
15974 */
15975 static void
15976f_screenattr(argvars, rettv)
15977 typval_T *argvars UNUSED;
15978 typval_T *rettv;
15979{
15980 int row;
15981 int col;
15982 int c;
15983
15984 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15985 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15986 if (row < 0 || row >= screen_Rows
15987 || col < 0 || col >= screen_Columns)
15988 c = -1;
15989 else
15990 c = ScreenAttrs[LineOffset[row] + col];
15991 rettv->vval.v_number = c;
15992}
15993
15994/*
15995 * "screenchar()" function
15996 */
15997 static void
15998f_screenchar(argvars, rettv)
15999 typval_T *argvars UNUSED;
16000 typval_T *rettv;
16001{
16002 int row;
16003 int col;
16004 int off;
16005 int c;
16006
16007 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16008 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16009 if (row < 0 || row >= screen_Rows
16010 || col < 0 || col >= screen_Columns)
16011 c = -1;
16012 else
16013 {
16014 off = LineOffset[row] + col;
16015#ifdef FEAT_MBYTE
16016 if (enc_utf8 && ScreenLinesUC[off] != 0)
16017 c = ScreenLinesUC[off];
16018 else
16019#endif
16020 c = ScreenLines[off];
16021 }
16022 rettv->vval.v_number = c;
16023}
16024
16025/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016026 * "screencol()" function
16027 *
16028 * First column is 1 to be consistent with virtcol().
16029 */
16030 static void
16031f_screencol(argvars, rettv)
16032 typval_T *argvars UNUSED;
16033 typval_T *rettv;
16034{
16035 rettv->vval.v_number = screen_screencol() + 1;
16036}
16037
16038/*
16039 * "screenrow()" function
16040 */
16041 static void
16042f_screenrow(argvars, rettv)
16043 typval_T *argvars UNUSED;
16044 typval_T *rettv;
16045{
16046 rettv->vval.v_number = screen_screenrow() + 1;
16047}
16048
16049/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050 * "search()" function
16051 */
16052 static void
16053f_search(argvars, rettv)
16054 typval_T *argvars;
16055 typval_T *rettv;
16056{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016057 int flags = 0;
16058
16059 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016060}
16061
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016063 * "searchdecl()" function
16064 */
16065 static void
16066f_searchdecl(argvars, rettv)
16067 typval_T *argvars;
16068 typval_T *rettv;
16069{
16070 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016071 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016072 int error = FALSE;
16073 char_u *name;
16074
16075 rettv->vval.v_number = 1; /* default: FAIL */
16076
16077 name = get_tv_string_chk(&argvars[0]);
16078 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016079 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016080 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016081 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16082 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16083 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016084 if (!error && name != NULL)
16085 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016086 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016087}
16088
16089/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016090 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016092 static int
16093searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016095 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096{
16097 char_u *spat, *mpat, *epat;
16098 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 int dir;
16101 int flags = 0;
16102 char_u nbuf1[NUMBUFLEN];
16103 char_u nbuf2[NUMBUFLEN];
16104 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016105 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016106 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016107 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016110 spat = get_tv_string_chk(&argvars[0]);
16111 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16112 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16113 if (spat == NULL || mpat == NULL || epat == NULL)
16114 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 /* Handle the optional fourth argument: flags */
16117 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016118 if (dir == 0)
16119 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016120
16121 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016122 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16123 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016124 if ((flags & (SP_END | SP_SUBPAT)) != 0
16125 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016126 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016127 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016128 goto theend;
16129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016131 /* Using 'r' implies 'W', otherwise it doesn't work. */
16132 if (flags & SP_REPEAT)
16133 p_ws = FALSE;
16134
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016136 if (argvars[3].v_type == VAR_UNKNOWN
16137 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 skip = (char_u *)"";
16139 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016142 if (argvars[5].v_type != VAR_UNKNOWN)
16143 {
16144 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16145 if (lnum_stop < 0)
16146 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016147#ifdef FEAT_RELTIME
16148 if (argvars[6].v_type != VAR_UNKNOWN)
16149 {
16150 time_limit = get_tv_number_chk(&argvars[6], NULL);
16151 if (time_limit < 0)
16152 goto theend;
16153 }
16154#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016155 }
16156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 if (skip == NULL)
16158 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016160 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016161 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016162
16163theend:
16164 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016165
16166 return retval;
16167}
16168
16169/*
16170 * "searchpair()" function
16171 */
16172 static void
16173f_searchpair(argvars, rettv)
16174 typval_T *argvars;
16175 typval_T *rettv;
16176{
16177 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16178}
16179
16180/*
16181 * "searchpairpos()" function
16182 */
16183 static void
16184f_searchpairpos(argvars, rettv)
16185 typval_T *argvars;
16186 typval_T *rettv;
16187{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016188 pos_T match_pos;
16189 int lnum = 0;
16190 int col = 0;
16191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016192 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016193 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016194
16195 if (searchpair_cmn(argvars, &match_pos) > 0)
16196 {
16197 lnum = match_pos.lnum;
16198 col = match_pos.col;
16199 }
16200
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016201 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16202 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016203}
16204
16205/*
16206 * Search for a start/middle/end thing.
16207 * Used by searchpair(), see its documentation for the details.
16208 * Returns 0 or -1 for no match,
16209 */
16210 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016211do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16212 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016213 char_u *spat; /* start pattern */
16214 char_u *mpat; /* middle pattern */
16215 char_u *epat; /* end pattern */
16216 int dir; /* BACKWARD or FORWARD */
16217 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016218 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016219 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016220 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016221 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016222{
16223 char_u *save_cpo;
16224 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16225 long retval = 0;
16226 pos_T pos;
16227 pos_T firstpos;
16228 pos_T foundpos;
16229 pos_T save_cursor;
16230 pos_T save_pos;
16231 int n;
16232 int r;
16233 int nest = 1;
16234 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016235 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016236 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016237
16238 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16239 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016240 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016241
Bram Moolenaar76929292008-01-06 19:07:36 +000016242#ifdef FEAT_RELTIME
16243 /* Set the time limit, if there is one. */
16244 profile_setlimit(time_limit, &tm);
16245#endif
16246
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016247 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16248 * start/middle/end (pat3, for the top pair). */
16249 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16250 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16251 if (pat2 == NULL || pat3 == NULL)
16252 goto theend;
16253 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16254 if (*mpat == NUL)
16255 STRCPY(pat3, pat2);
16256 else
16257 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16258 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016259 if (flags & SP_START)
16260 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016261
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 save_cursor = curwin->w_cursor;
16263 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016264 clearpos(&firstpos);
16265 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 pat = pat3;
16267 for (;;)
16268 {
16269 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016270 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16272 /* didn't find it or found the first match again: FAIL */
16273 break;
16274
16275 if (firstpos.lnum == 0)
16276 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016277 if (equalpos(pos, foundpos))
16278 {
16279 /* Found the same position again. Can happen with a pattern that
16280 * has "\zs" at the end and searching backwards. Advance one
16281 * character and try again. */
16282 if (dir == BACKWARD)
16283 decl(&pos);
16284 else
16285 incl(&pos);
16286 }
16287 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016289 /* clear the start flag to avoid getting stuck here */
16290 options &= ~SEARCH_START;
16291
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 /* If the skip pattern matches, ignore this match. */
16293 if (*skip != NUL)
16294 {
16295 save_pos = curwin->w_cursor;
16296 curwin->w_cursor = pos;
16297 r = eval_to_bool(skip, &err, NULL, FALSE);
16298 curwin->w_cursor = save_pos;
16299 if (err)
16300 {
16301 /* Evaluating {skip} caused an error, break here. */
16302 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016303 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 break;
16305 }
16306 if (r)
16307 continue;
16308 }
16309
16310 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16311 {
16312 /* Found end when searching backwards or start when searching
16313 * forward: nested pair. */
16314 ++nest;
16315 pat = pat2; /* nested, don't search for middle */
16316 }
16317 else
16318 {
16319 /* Found end when searching forward or start when searching
16320 * backward: end of (nested) pair; or found middle in outer pair. */
16321 if (--nest == 1)
16322 pat = pat3; /* outer level, search for middle */
16323 }
16324
16325 if (nest == 0)
16326 {
16327 /* Found the match: return matchcount or line number. */
16328 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016329 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016331 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016332 if (flags & SP_SETPCMARK)
16333 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 curwin->w_cursor = pos;
16335 if (!(flags & SP_REPEAT))
16336 break;
16337 nest = 1; /* search for next unmatched */
16338 }
16339 }
16340
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016341 if (match_pos != NULL)
16342 {
16343 /* Store the match cursor position */
16344 match_pos->lnum = curwin->w_cursor.lnum;
16345 match_pos->col = curwin->w_cursor.col + 1;
16346 }
16347
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016349 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 curwin->w_cursor = save_cursor;
16351
16352theend:
16353 vim_free(pat2);
16354 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016355 if (p_cpo == empty_option)
16356 p_cpo = save_cpo;
16357 else
16358 /* Darn, evaluating the {skip} expression changed the value. */
16359 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016360
16361 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362}
16363
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016364/*
16365 * "searchpos()" function
16366 */
16367 static void
16368f_searchpos(argvars, rettv)
16369 typval_T *argvars;
16370 typval_T *rettv;
16371{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016372 pos_T match_pos;
16373 int lnum = 0;
16374 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016375 int n;
16376 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016377
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016378 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016379 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016380
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016381 n = search_cmn(argvars, &match_pos, &flags);
16382 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 {
16384 lnum = match_pos.lnum;
16385 col = match_pos.col;
16386 }
16387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016388 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16389 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016390 if (flags & SP_SUBPAT)
16391 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392}
16393
16394
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 static void
16396f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400#ifdef FEAT_CLIENTSERVER
16401 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 char_u *server = get_tv_string_chk(&argvars[0]);
16403 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 if (server == NULL || reply == NULL)
16407 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408 if (check_restricted() || check_secure())
16409 return;
16410# ifdef FEAT_X11
16411 if (check_connection() == FAIL)
16412 return;
16413# endif
16414
16415 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 EMSG(_("E258: Unable to send to client"));
16418 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420 rettv->vval.v_number = 0;
16421#else
16422 rettv->vval.v_number = -1;
16423#endif
16424}
16425
Bram Moolenaar0d660222005-01-07 21:51:51 +000016426 static void
16427f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016428 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430{
16431 char_u *r = NULL;
16432
16433#ifdef FEAT_CLIENTSERVER
16434# ifdef WIN32
16435 r = serverGetVimNames();
16436# else
16437 make_connection();
16438 if (X_DISPLAY != NULL)
16439 r = serverGetVimNames(X_DISPLAY);
16440# endif
16441#endif
16442 rettv->v_type = VAR_STRING;
16443 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444}
16445
16446/*
16447 * "setbufvar()" function
16448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016450f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016451 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016452 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453{
16454 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 char_u nbuf[NUMBUFLEN];
16459
16460 if (check_restricted() || check_secure())
16461 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16463 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016464 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 varp = &argvars[2];
16466
16467 if (buf != NULL && varname != NULL && varp != NULL)
16468 {
16469 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471
16472 if (*varname == '&')
16473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016474 long numval;
16475 char_u *strval;
16476 int error = FALSE;
16477
Bram Moolenaar071d4272004-06-13 20:20:40 +000016478 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 numval = get_tv_number_chk(varp, &error);
16480 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 if (!error && strval != NULL)
16482 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483 }
16484 else
16485 {
16486 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16487 if (bufvarname != NULL)
16488 {
16489 STRCPY(bufvarname, "b:");
16490 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016491 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 vim_free(bufvarname);
16493 }
16494 }
16495
16496 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499}
16500
16501/*
16502 * "setcmdpos()" function
16503 */
16504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016505f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016506 typval_T *argvars;
16507 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 int pos = (int)get_tv_number(&argvars[0]) - 1;
16510
16511 if (pos >= 0)
16512 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513}
16514
16515/*
16516 * "setline()" function
16517 */
16518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016519f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016520 typval_T *argvars;
16521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522{
16523 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016524 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016525 list_T *l = NULL;
16526 listitem_T *li = NULL;
16527 long added = 0;
16528 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016530 lnum = get_tv_lnum(&argvars[0]);
16531 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016533 l = argvars[1].vval.v_list;
16534 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016536 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016537 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016538
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016539 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016540 for (;;)
16541 {
16542 if (l != NULL)
16543 {
16544 /* list argument, get next string */
16545 if (li == NULL)
16546 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016548 li = li->li_next;
16549 }
16550
16551 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016553 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016554
16555 /* When coming here from Insert mode, sync undo, so that this can be
16556 * undone separately from what was previously inserted. */
16557 if (u_sync_once == 2)
16558 {
16559 u_sync_once = 1; /* notify that u_sync() was called */
16560 u_sync(TRUE);
16561 }
16562
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016563 if (lnum <= curbuf->b_ml.ml_line_count)
16564 {
16565 /* existing line, replace it */
16566 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16567 {
16568 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016569 if (lnum == curwin->w_cursor.lnum)
16570 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016571 rettv->vval.v_number = 0; /* OK */
16572 }
16573 }
16574 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16575 {
16576 /* lnum is one past the last line, append the line */
16577 ++added;
16578 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16579 rettv->vval.v_number = 0; /* OK */
16580 }
16581
16582 if (l == NULL) /* only one string argument */
16583 break;
16584 ++lnum;
16585 }
16586
16587 if (added > 0)
16588 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589}
16590
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016591static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16592
Bram Moolenaar071d4272004-06-13 20:20:40 +000016593/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016594 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016595 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016596 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016597set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016598 win_T *wp UNUSED;
16599 typval_T *list_arg UNUSED;
16600 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016601 typval_T *rettv;
16602{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016603#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016604 char_u *act;
16605 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016606#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016607
Bram Moolenaar2641f772005-03-25 21:58:17 +000016608 rettv->vval.v_number = -1;
16609
16610#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016611 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016612 EMSG(_(e_listreq));
16613 else
16614 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016615 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016616
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016617 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016618 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016619 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 if (act == NULL)
16621 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016622 if (*act == 'a' || *act == 'r')
16623 action = *act;
16624 }
16625
Bram Moolenaar81484f42012-12-05 15:16:47 +010016626 if (l != NULL && set_errorlist(wp, l, action,
16627 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016628 rettv->vval.v_number = 0;
16629 }
16630#endif
16631}
16632
16633/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016634 * "setloclist()" function
16635 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016636 static void
16637f_setloclist(argvars, rettv)
16638 typval_T *argvars;
16639 typval_T *rettv;
16640{
16641 win_T *win;
16642
16643 rettv->vval.v_number = -1;
16644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016645 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016646 if (win != NULL)
16647 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16648}
16649
16650/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016651 * "setmatches()" function
16652 */
16653 static void
16654f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016655 typval_T *argvars UNUSED;
16656 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016657{
16658#ifdef FEAT_SEARCH_EXTRA
16659 list_T *l;
16660 listitem_T *li;
16661 dict_T *d;
16662
16663 rettv->vval.v_number = -1;
16664 if (argvars[0].v_type != VAR_LIST)
16665 {
16666 EMSG(_(e_listreq));
16667 return;
16668 }
16669 if ((l = argvars[0].vval.v_list) != NULL)
16670 {
16671
16672 /* To some extent make sure that we are dealing with a list from
16673 * "getmatches()". */
16674 li = l->lv_first;
16675 while (li != NULL)
16676 {
16677 if (li->li_tv.v_type != VAR_DICT
16678 || (d = li->li_tv.vval.v_dict) == NULL)
16679 {
16680 EMSG(_(e_invarg));
16681 return;
16682 }
16683 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16684 && dict_find(d, (char_u *)"pattern", -1) != NULL
16685 && dict_find(d, (char_u *)"priority", -1) != NULL
16686 && dict_find(d, (char_u *)"id", -1) != NULL))
16687 {
16688 EMSG(_(e_invarg));
16689 return;
16690 }
16691 li = li->li_next;
16692 }
16693
16694 clear_matches(curwin);
16695 li = l->lv_first;
16696 while (li != NULL)
16697 {
16698 d = li->li_tv.vval.v_dict;
16699 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16700 get_dict_string(d, (char_u *)"pattern", FALSE),
16701 (int)get_dict_number(d, (char_u *)"priority"),
16702 (int)get_dict_number(d, (char_u *)"id"));
16703 li = li->li_next;
16704 }
16705 rettv->vval.v_number = 0;
16706 }
16707#endif
16708}
16709
16710/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016711 * "setpos()" function
16712 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016713 static void
16714f_setpos(argvars, rettv)
16715 typval_T *argvars;
16716 typval_T *rettv;
16717{
16718 pos_T pos;
16719 int fnum;
16720 char_u *name;
16721
Bram Moolenaar08250432008-02-13 11:42:46 +000016722 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016723 name = get_tv_string_chk(argvars);
16724 if (name != NULL)
16725 {
16726 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16727 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016728 if (--pos.col < 0)
16729 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016730 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016731 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016732 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016733 if (fnum == curbuf->b_fnum)
16734 {
16735 curwin->w_cursor = pos;
16736 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016737 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016738 }
16739 else
16740 EMSG(_(e_invarg));
16741 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016742 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16743 {
16744 /* set mark */
16745 if (setmark_pos(name[1], &pos, fnum) == OK)
16746 rettv->vval.v_number = 0;
16747 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016748 else
16749 EMSG(_(e_invarg));
16750 }
16751 }
16752}
16753
16754/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016755 * "setqflist()" function
16756 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016757 static void
16758f_setqflist(argvars, rettv)
16759 typval_T *argvars;
16760 typval_T *rettv;
16761{
16762 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16763}
16764
16765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 * "setreg()" function
16767 */
16768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016769f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 typval_T *argvars;
16771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772{
16773 int regname;
16774 char_u *strregname;
16775 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777 int append;
16778 char_u yank_type;
16779 long block_len;
16780
16781 block_len = -1;
16782 yank_type = MAUTO;
16783 append = FALSE;
16784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016785 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016788 if (strregname == NULL)
16789 return; /* type error; errmsg already given */
16790 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 if (regname == 0 || regname == '@')
16792 regname = '"';
16793 else if (regname == '=')
16794 return;
16795
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016798 stropt = get_tv_string_chk(&argvars[2]);
16799 if (stropt == NULL)
16800 return; /* type error */
16801 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 switch (*stropt)
16803 {
16804 case 'a': case 'A': /* append */
16805 append = TRUE;
16806 break;
16807 case 'v': case 'c': /* character-wise selection */
16808 yank_type = MCHAR;
16809 break;
16810 case 'V': case 'l': /* line-wise selection */
16811 yank_type = MLINE;
16812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 case 'b': case Ctrl_V: /* block-wise selection */
16814 yank_type = MBLOCK;
16815 if (VIM_ISDIGIT(stropt[1]))
16816 {
16817 ++stropt;
16818 block_len = getdigits(&stropt) - 1;
16819 --stropt;
16820 }
16821 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 }
16823 }
16824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016825 strval = get_tv_string_chk(&argvars[1]);
16826 if (strval != NULL)
16827 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016832/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016833 * "settabvar()" function
16834 */
16835 static void
16836f_settabvar(argvars, rettv)
16837 typval_T *argvars;
16838 typval_T *rettv;
16839{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016840#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016841 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016842 tabpage_T *tp;
16843#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016844 char_u *varname, *tabvarname;
16845 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016846
16847 rettv->vval.v_number = 0;
16848
16849 if (check_restricted() || check_secure())
16850 return;
16851
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016852#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016853 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016854#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016855 varname = get_tv_string_chk(&argvars[1]);
16856 varp = &argvars[2];
16857
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016858 if (varname != NULL && varp != NULL
16859#ifdef FEAT_WINDOWS
16860 && tp != NULL
16861#endif
16862 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016863 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016864#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016865 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016866 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016867#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016868
16869 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16870 if (tabvarname != NULL)
16871 {
16872 STRCPY(tabvarname, "t:");
16873 STRCPY(tabvarname + 2, varname);
16874 set_var(tabvarname, varp, TRUE);
16875 vim_free(tabvarname);
16876 }
16877
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016878#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016879 /* Restore current tabpage */
16880 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016881 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016882#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016883 }
16884}
16885
16886/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016887 * "settabwinvar()" function
16888 */
16889 static void
16890f_settabwinvar(argvars, rettv)
16891 typval_T *argvars;
16892 typval_T *rettv;
16893{
16894 setwinvar(argvars, rettv, 1);
16895}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896
16897/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016898 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016902 typval_T *argvars;
16903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016904{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016905 setwinvar(argvars, rettv, 0);
16906}
16907
16908/*
16909 * "setwinvar()" and "settabwinvar()" functions
16910 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016911
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016912 static void
16913setwinvar(argvars, rettv, off)
16914 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016915 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016916 int off;
16917{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918 win_T *win;
16919#ifdef FEAT_WINDOWS
16920 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016921 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922#endif
16923 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016924 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016925 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016926 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927
16928 if (check_restricted() || check_secure())
16929 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016930
16931#ifdef FEAT_WINDOWS
16932 if (off == 1)
16933 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16934 else
16935 tp = curtab;
16936#endif
16937 win = find_win_by_nr(&argvars[off], tp);
16938 varname = get_tv_string_chk(&argvars[off + 1]);
16939 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940
16941 if (win != NULL && varname != NULL && varp != NULL)
16942 {
16943#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016944 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016945 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946#endif
16947
16948 if (*varname == '&')
16949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 long numval;
16951 char_u *strval;
16952 int error = FALSE;
16953
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 numval = get_tv_number_chk(varp, &error);
16956 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 if (!error && strval != NULL)
16958 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 }
16960 else
16961 {
16962 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16963 if (winvarname != NULL)
16964 {
16965 STRCPY(winvarname, "w:");
16966 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016967 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968 vim_free(winvarname);
16969 }
16970 }
16971
16972#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016973 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974#endif
16975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976}
16977
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016978#ifdef FEAT_CRYPT
16979/*
16980 * "sha256({string})" function
16981 */
16982 static void
16983f_sha256(argvars, rettv)
16984 typval_T *argvars;
16985 typval_T *rettv;
16986{
16987 char_u *p;
16988
16989 p = get_tv_string(&argvars[0]);
16990 rettv->vval.v_string = vim_strsave(
16991 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16992 rettv->v_type = VAR_STRING;
16993}
16994#endif /* FEAT_CRYPT */
16995
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016997 * "shellescape({string})" function
16998 */
16999 static void
17000f_shellescape(argvars, rettv)
17001 typval_T *argvars;
17002 typval_T *rettv;
17003{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017004 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017005 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017006 rettv->v_type = VAR_STRING;
17007}
17008
17009/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017010 * shiftwidth() function
17011 */
17012 static void
17013f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017014 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017015 typval_T *rettv;
17016{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017017 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017018}
17019
17020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 */
17023 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 typval_T *argvars;
17026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 p = get_tv_string(&argvars[0]);
17031 rettv->vval.v_string = vim_strsave(p);
17032 simplify_filename(rettv->vval.v_string); /* simplify in place */
17033 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034}
17035
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017036#ifdef FEAT_FLOAT
17037/*
17038 * "sin()" function
17039 */
17040 static void
17041f_sin(argvars, rettv)
17042 typval_T *argvars;
17043 typval_T *rettv;
17044{
17045 float_T f;
17046
17047 rettv->v_type = VAR_FLOAT;
17048 if (get_float_arg(argvars, &f) == OK)
17049 rettv->vval.v_float = sin(f);
17050 else
17051 rettv->vval.v_float = 0.0;
17052}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017053
17054/*
17055 * "sinh()" function
17056 */
17057 static void
17058f_sinh(argvars, rettv)
17059 typval_T *argvars;
17060 typval_T *rettv;
17061{
17062 float_T f;
17063
17064 rettv->v_type = VAR_FLOAT;
17065 if (get_float_arg(argvars, &f) == OK)
17066 rettv->vval.v_float = sinh(f);
17067 else
17068 rettv->vval.v_float = 0.0;
17069}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017070#endif
17071
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072static int
17073#ifdef __BORLANDC__
17074 _RTLENTRYF
17075#endif
17076 item_compare __ARGS((const void *s1, const void *s2));
17077static int
17078#ifdef __BORLANDC__
17079 _RTLENTRYF
17080#endif
17081 item_compare2 __ARGS((const void *s1, const void *s2));
17082
17083static int item_compare_ic;
17084static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017085static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017086static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017087static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088#define ITEM_COMPARE_FAIL 999
17089
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017091 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 static int
17094#ifdef __BORLANDC__
17095_RTLENTRYF
17096#endif
17097item_compare(s1, s2)
17098 const void *s1;
17099 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 char_u *p1, *p2;
17102 char_u *tofree1, *tofree2;
17103 int res;
17104 char_u numbuf1[NUMBUFLEN];
17105 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017107 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17108 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017109 if (p1 == NULL)
17110 p1 = (char_u *)"";
17111 if (p2 == NULL)
17112 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 if (item_compare_ic)
17114 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 res = STRCMP(p1, p2);
17117 vim_free(tofree1);
17118 vim_free(tofree2);
17119 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120}
17121
17122 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123#ifdef __BORLANDC__
17124_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126item_compare2(s1, s2)
17127 const void *s1;
17128 const void *s2;
17129{
17130 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017131 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017132 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 /* shortcut after failure in previous call; compare all items equal */
17136 if (item_compare_func_err)
17137 return 0;
17138
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017139 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17140 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017141 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17142 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143
17144 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017145 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017146 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17147 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148 clear_tv(&argv[0]);
17149 clear_tv(&argv[1]);
17150
17151 if (res == FAIL)
17152 res = ITEM_COMPARE_FAIL;
17153 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017154 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17155 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017156 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 clear_tv(&rettv);
17158 return res;
17159}
17160
17161/*
17162 * "sort({list})" function
17163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017165do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017166 typval_T *argvars;
17167 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017168 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169{
Bram Moolenaar33570922005-01-25 22:26:29 +000017170 list_T *l;
17171 listitem_T *li;
17172 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 long len;
17174 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017177 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 else
17179 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017181 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017182 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183 return;
17184 rettv->vval.v_list = l;
17185 rettv->v_type = VAR_LIST;
17186 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188 len = list_len(l);
17189 if (len <= 1)
17190 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaar0d660222005-01-07 21:51:51 +000017192 item_compare_ic = FALSE;
17193 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017194 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 if (argvars[1].v_type != VAR_UNKNOWN)
17196 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017197 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017199 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200 else
17201 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017202 int error = FALSE;
17203
17204 i = get_tv_number_chk(&argvars[1], &error);
17205 if (error)
17206 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017207 if (i == 1)
17208 item_compare_ic = TRUE;
17209 else
17210 item_compare_func = get_tv_string(&argvars[1]);
17211 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017212
17213 if (argvars[2].v_type != VAR_UNKNOWN)
17214 {
17215 /* optional third argument: {dict} */
17216 if (argvars[2].v_type != VAR_DICT)
17217 {
17218 EMSG(_(e_dictreq));
17219 return;
17220 }
17221 item_compare_selfdict = argvars[2].vval.v_dict;
17222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017226 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227 if (ptrs == NULL)
17228 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229
Bram Moolenaar327aa022014-03-25 18:24:23 +010017230 i = 0;
17231 if (sort)
17232 {
17233 /* sort(): ptrs will be the list to sort */
17234 for (li = l->lv_first; li != NULL; li = li->li_next)
17235 ptrs[i++] = li;
17236
17237 item_compare_func_err = FALSE;
17238 /* test the compare function */
17239 if (item_compare_func != NULL
17240 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017242 EMSG(_("E702: Sort compare function failed"));
17243 else
17244 {
17245 /* Sort the array with item pointers. */
17246 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17247 item_compare_func == NULL ? item_compare : item_compare2);
17248
17249 if (!item_compare_func_err)
17250 {
17251 /* Clear the List and append the items in sorted order. */
17252 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17253 l->lv_len = 0;
17254 for (i = 0; i < len; ++i)
17255 list_append(l, ptrs[i]);
17256 }
17257 }
17258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017261 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17262
17263 /* f_uniq(): ptrs will be a stack of items to remove */
17264 item_compare_func_err = FALSE;
17265 item_compare_func_ptr = item_compare_func
17266 ? item_compare2 : item_compare;
17267
17268 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17269 li = li->li_next)
17270 {
17271 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17272 == 0)
17273 ptrs[i++] = li;
17274 if (item_compare_func_err)
17275 {
17276 EMSG(_("E882: Uniq compare function failed"));
17277 break;
17278 }
17279 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017281 if (!item_compare_func_err)
17282 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017283 while (--i >= 0)
17284 {
17285 li = ptrs[i]->li_next;
17286 ptrs[i]->li_next = li->li_next;
17287 if (li->li_next != NULL)
17288 li->li_next->li_prev = ptrs[i];
17289 else
17290 l->lv_last = ptrs[i];
17291 list_fix_watch(l, li);
17292 listitem_free(li);
17293 l->lv_len--;
17294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017295 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296 }
17297
17298 vim_free(ptrs);
17299 }
17300}
17301
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017302/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017303 * "sort({list})" function
17304 */
17305 static void
17306f_sort(argvars, rettv)
17307 typval_T *argvars;
17308 typval_T *rettv;
17309{
17310 do_sort_uniq(argvars, rettv, TRUE);
17311}
17312
17313/*
17314 * "uniq({list})" function
17315 */
17316 static void
17317f_uniq(argvars, rettv)
17318 typval_T *argvars;
17319 typval_T *rettv;
17320{
17321 do_sort_uniq(argvars, rettv, FALSE);
17322}
17323
17324/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017325 * "soundfold({word})" function
17326 */
17327 static void
17328f_soundfold(argvars, rettv)
17329 typval_T *argvars;
17330 typval_T *rettv;
17331{
17332 char_u *s;
17333
17334 rettv->v_type = VAR_STRING;
17335 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017336#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017337 rettv->vval.v_string = eval_soundfold(s);
17338#else
17339 rettv->vval.v_string = vim_strsave(s);
17340#endif
17341}
17342
17343/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017344 * "spellbadword()" function
17345 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017346 static void
17347f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017348 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017349 typval_T *rettv;
17350{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017351 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017352 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017353 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017354
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017355 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017356 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017357
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017358#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017359 if (argvars[0].v_type == VAR_UNKNOWN)
17360 {
17361 /* Find the start and length of the badly spelled word. */
17362 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17363 if (len != 0)
17364 word = ml_get_cursor();
17365 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017366 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017367 {
17368 char_u *str = get_tv_string_chk(&argvars[0]);
17369 int capcol = -1;
17370
17371 if (str != NULL)
17372 {
17373 /* Check the argument for spelling. */
17374 while (*str != NUL)
17375 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017376 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017377 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017378 {
17379 word = str;
17380 break;
17381 }
17382 str += len;
17383 }
17384 }
17385 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017386#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017387
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017388 list_append_string(rettv->vval.v_list, word, len);
17389 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017390 attr == HLF_SPB ? "bad" :
17391 attr == HLF_SPR ? "rare" :
17392 attr == HLF_SPL ? "local" :
17393 attr == HLF_SPC ? "caps" :
17394 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017395}
17396
17397/*
17398 * "spellsuggest()" function
17399 */
17400 static void
17401f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017402 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017403 typval_T *rettv;
17404{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017405#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017406 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017407 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017408 int maxcount;
17409 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017410 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017411 listitem_T *li;
17412 int need_capital = FALSE;
17413#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017414
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017415 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017416 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017417
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017418#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017419 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017420 {
17421 str = get_tv_string(&argvars[0]);
17422 if (argvars[1].v_type != VAR_UNKNOWN)
17423 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017424 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017425 if (maxcount <= 0)
17426 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017427 if (argvars[2].v_type != VAR_UNKNOWN)
17428 {
17429 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17430 if (typeerr)
17431 return;
17432 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017433 }
17434 else
17435 maxcount = 25;
17436
Bram Moolenaar4770d092006-01-12 23:22:24 +000017437 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017438
17439 for (i = 0; i < ga.ga_len; ++i)
17440 {
17441 str = ((char_u **)ga.ga_data)[i];
17442
17443 li = listitem_alloc();
17444 if (li == NULL)
17445 vim_free(str);
17446 else
17447 {
17448 li->li_tv.v_type = VAR_STRING;
17449 li->li_tv.v_lock = 0;
17450 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017451 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017452 }
17453 }
17454 ga_clear(&ga);
17455 }
17456#endif
17457}
17458
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017460f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 typval_T *argvars;
17462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463{
17464 char_u *str;
17465 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017466 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017467 regmatch_T regmatch;
17468 char_u patbuf[NUMBUFLEN];
17469 char_u *save_cpo;
17470 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017471 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017472 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017473 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017474
17475 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17476 save_cpo = p_cpo;
17477 p_cpo = (char_u *)"";
17478
17479 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017480 if (argvars[1].v_type != VAR_UNKNOWN)
17481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017482 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17483 if (pat == NULL)
17484 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017485 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017487 }
17488 if (pat == NULL || *pat == NUL)
17489 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017490
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017491 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017493 if (typeerr)
17494 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
Bram Moolenaar0d660222005-01-07 21:51:51 +000017496 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17497 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017499 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017500 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017501 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017502 if (*str == NUL)
17503 match = FALSE; /* empty item at the end */
17504 else
17505 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506 if (match)
17507 end = regmatch.startp[0];
17508 else
17509 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017510 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17511 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017512 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017513 if (list_append_string(rettv->vval.v_list, str,
17514 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017516 }
17517 if (!match)
17518 break;
17519 /* Advance to just after the match. */
17520 if (regmatch.endp[0] > str)
17521 col = 0;
17522 else
17523 {
17524 /* Don't get stuck at the same match. */
17525#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017526 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017527#else
17528 col = 1;
17529#endif
17530 }
17531 str = regmatch.endp[0];
17532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533
Bram Moolenaar473de612013-06-08 18:19:48 +020017534 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536
Bram Moolenaar0d660222005-01-07 21:51:51 +000017537 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538}
17539
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017540#ifdef FEAT_FLOAT
17541/*
17542 * "sqrt()" function
17543 */
17544 static void
17545f_sqrt(argvars, rettv)
17546 typval_T *argvars;
17547 typval_T *rettv;
17548{
17549 float_T f;
17550
17551 rettv->v_type = VAR_FLOAT;
17552 if (get_float_arg(argvars, &f) == OK)
17553 rettv->vval.v_float = sqrt(f);
17554 else
17555 rettv->vval.v_float = 0.0;
17556}
17557
17558/*
17559 * "str2float()" function
17560 */
17561 static void
17562f_str2float(argvars, rettv)
17563 typval_T *argvars;
17564 typval_T *rettv;
17565{
17566 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17567
17568 if (*p == '+')
17569 p = skipwhite(p + 1);
17570 (void)string2float(p, &rettv->vval.v_float);
17571 rettv->v_type = VAR_FLOAT;
17572}
17573#endif
17574
Bram Moolenaar2c932302006-03-18 21:42:09 +000017575/*
17576 * "str2nr()" function
17577 */
17578 static void
17579f_str2nr(argvars, rettv)
17580 typval_T *argvars;
17581 typval_T *rettv;
17582{
17583 int base = 10;
17584 char_u *p;
17585 long n;
17586
17587 if (argvars[1].v_type != VAR_UNKNOWN)
17588 {
17589 base = get_tv_number(&argvars[1]);
17590 if (base != 8 && base != 10 && base != 16)
17591 {
17592 EMSG(_(e_invarg));
17593 return;
17594 }
17595 }
17596
17597 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017598 if (*p == '+')
17599 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017600 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17601 rettv->vval.v_number = n;
17602}
17603
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604#ifdef HAVE_STRFTIME
17605/*
17606 * "strftime({format}[, {time}])" function
17607 */
17608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017609f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017610 typval_T *argvars;
17611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
17613 char_u result_buf[256];
17614 struct tm *curtime;
17615 time_t seconds;
17616 char_u *p;
17617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017618 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017621 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 seconds = time(NULL);
17623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 curtime = localtime(&seconds);
17626 /* MSVC returns NULL for an invalid value of seconds. */
17627 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 else
17630 {
17631# ifdef FEAT_MBYTE
17632 vimconv_T conv;
17633 char_u *enc;
17634
17635 conv.vc_type = CONV_NONE;
17636 enc = enc_locale();
17637 convert_setup(&conv, p_enc, enc);
17638 if (conv.vc_type != CONV_NONE)
17639 p = string_convert(&conv, p, NULL);
17640# endif
17641 if (p != NULL)
17642 (void)strftime((char *)result_buf, sizeof(result_buf),
17643 (char *)p, curtime);
17644 else
17645 result_buf[0] = NUL;
17646
17647# ifdef FEAT_MBYTE
17648 if (conv.vc_type != CONV_NONE)
17649 vim_free(p);
17650 convert_setup(&conv, enc, p_enc);
17651 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 else
17654# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656
17657# ifdef FEAT_MBYTE
17658 /* Release conversion descriptors */
17659 convert_setup(&conv, NULL, NULL);
17660 vim_free(enc);
17661# endif
17662 }
17663}
17664#endif
17665
17666/*
17667 * "stridx()" function
17668 */
17669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017671 typval_T *argvars;
17672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673{
17674 char_u buf[NUMBUFLEN];
17675 char_u *needle;
17676 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017679 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 needle = get_tv_string_chk(&argvars[1]);
17682 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017684 if (needle == NULL || haystack == NULL)
17685 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 if (argvars[2].v_type != VAR_UNKNOWN)
17688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017689 int error = FALSE;
17690
17691 start_idx = get_tv_number_chk(&argvars[2], &error);
17692 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017693 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017694 if (start_idx >= 0)
17695 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017696 }
17697
17698 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17699 if (pos != NULL)
17700 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
17703/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017704 * "string()" function
17705 */
17706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017707f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017708 typval_T *argvars;
17709 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017710{
17711 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017712 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017715 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017716 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017717 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017718 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719}
17720
17721/*
17722 * "strlen()" function
17723 */
17724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017725f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 typval_T *argvars;
17727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729 rettv->vval.v_number = (varnumber_T)(STRLEN(
17730 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731}
17732
17733/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017734 * "strchars()" function
17735 */
17736 static void
17737f_strchars(argvars, rettv)
17738 typval_T *argvars;
17739 typval_T *rettv;
17740{
17741 char_u *s = get_tv_string(&argvars[0]);
17742#ifdef FEAT_MBYTE
17743 varnumber_T len = 0;
17744
17745 while (*s != NUL)
17746 {
17747 mb_cptr2char_adv(&s);
17748 ++len;
17749 }
17750 rettv->vval.v_number = len;
17751#else
17752 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17753#endif
17754}
17755
17756/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017757 * "strdisplaywidth()" function
17758 */
17759 static void
17760f_strdisplaywidth(argvars, rettv)
17761 typval_T *argvars;
17762 typval_T *rettv;
17763{
17764 char_u *s = get_tv_string(&argvars[0]);
17765 int col = 0;
17766
17767 if (argvars[1].v_type != VAR_UNKNOWN)
17768 col = get_tv_number(&argvars[1]);
17769
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017770 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017771}
17772
17773/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017774 * "strwidth()" function
17775 */
17776 static void
17777f_strwidth(argvars, rettv)
17778 typval_T *argvars;
17779 typval_T *rettv;
17780{
17781 char_u *s = get_tv_string(&argvars[0]);
17782
17783 rettv->vval.v_number = (varnumber_T)(
17784#ifdef FEAT_MBYTE
17785 mb_string2cells(s, -1)
17786#else
17787 STRLEN(s)
17788#endif
17789 );
17790}
17791
17792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 * "strpart()" function
17794 */
17795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017797 typval_T *argvars;
17798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799{
17800 char_u *p;
17801 int n;
17802 int len;
17803 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017804 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 slen = (int)STRLEN(p);
17808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 n = get_tv_number_chk(&argvars[1], &error);
17810 if (error)
17811 len = 0;
17812 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814 else
17815 len = slen - n; /* default len: all bytes that are available. */
17816
17817 /*
17818 * Only return the overlap between the specified part and the actual
17819 * string.
17820 */
17821 if (n < 0)
17822 {
17823 len += n;
17824 n = 0;
17825 }
17826 else if (n > slen)
17827 n = slen;
17828 if (len < 0)
17829 len = 0;
17830 else if (n + len > slen)
17831 len = slen - n;
17832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 rettv->v_type = VAR_STRING;
17834 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835}
17836
17837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017838 * "strridx()" function
17839 */
17840 static void
17841f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 typval_T *argvars;
17843 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844{
17845 char_u buf[NUMBUFLEN];
17846 char_u *needle;
17847 char_u *haystack;
17848 char_u *rest;
17849 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017850 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017852 needle = get_tv_string_chk(&argvars[1]);
17853 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017854
17855 rettv->vval.v_number = -1;
17856 if (needle == NULL || haystack == NULL)
17857 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017858
17859 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017860 if (argvars[2].v_type != VAR_UNKNOWN)
17861 {
17862 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017863 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017864 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017865 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017866 }
17867 else
17868 end_idx = haystack_len;
17869
Bram Moolenaar0d660222005-01-07 21:51:51 +000017870 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017871 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017872 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017873 lastmatch = haystack + end_idx;
17874 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017875 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017876 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877 for (rest = haystack; *rest != '\0'; ++rest)
17878 {
17879 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017880 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881 break;
17882 lastmatch = rest;
17883 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017884 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017885
17886 if (lastmatch == NULL)
17887 rettv->vval.v_number = -1;
17888 else
17889 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17890}
17891
17892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 * "strtrans()" function
17894 */
17895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017897 typval_T *argvars;
17898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017900 rettv->v_type = VAR_STRING;
17901 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902}
17903
17904/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017905 * "submatch()" function
17906 */
17907 static void
17908f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 typval_T *argvars;
17910 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017911{
Bram Moolenaar41571762014-04-02 19:00:58 +020017912 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017913 int no;
17914 int retList = 0;
17915
17916 no = (int)get_tv_number_chk(&argvars[0], &error);
17917 if (error)
17918 return;
17919 error = FALSE;
17920 if (argvars[1].v_type != VAR_UNKNOWN)
17921 retList = get_tv_number_chk(&argvars[1], &error);
17922 if (error)
17923 return;
17924
17925 if (retList == 0)
17926 {
17927 rettv->v_type = VAR_STRING;
17928 rettv->vval.v_string = reg_submatch(no);
17929 }
17930 else
17931 {
17932 rettv->v_type = VAR_LIST;
17933 rettv->vval.v_list = reg_submatch_list(no);
17934 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017935}
17936
17937/*
17938 * "substitute()" function
17939 */
17940 static void
17941f_substitute(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{
17945 char_u patbuf[NUMBUFLEN];
17946 char_u subbuf[NUMBUFLEN];
17947 char_u flagsbuf[NUMBUFLEN];
17948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017949 char_u *str = get_tv_string_chk(&argvars[0]);
17950 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17951 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17952 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17953
Bram Moolenaar0d660222005-01-07 21:51:51 +000017954 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017955 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17956 rettv->vval.v_string = NULL;
17957 else
17958 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017959}
17960
17961/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017962 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017965f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968{
17969 int id = 0;
17970#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017971 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 long col;
17973 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017974 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017976 lnum = get_tv_lnum(argvars); /* -1 on type error */
17977 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17978 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017980 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017981 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017982 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983#endif
17984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017985 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986}
17987
17988/*
17989 * "synIDattr(id, what [, mode])" function
17990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017992f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017993 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995{
17996 char_u *p = NULL;
17997#ifdef FEAT_SYN_HL
17998 int id;
17999 char_u *what;
18000 char_u *mode;
18001 char_u modebuf[NUMBUFLEN];
18002 int modec;
18003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004 id = get_tv_number(&argvars[0]);
18005 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018010 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 modec = 0; /* replace invalid with current */
18012 }
18013 else
18014 {
18015#ifdef FEAT_GUI
18016 if (gui.in_use)
18017 modec = 'g';
18018 else
18019#endif
18020 if (t_colors > 1)
18021 modec = 'c';
18022 else
18023 modec = 't';
18024 }
18025
18026
18027 switch (TOLOWER_ASC(what[0]))
18028 {
18029 case 'b':
18030 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18031 p = highlight_color(id, what, modec);
18032 else /* bold */
18033 p = highlight_has_attr(id, HL_BOLD, modec);
18034 break;
18035
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018036 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 p = highlight_color(id, what, modec);
18038 break;
18039
18040 case 'i':
18041 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18042 p = highlight_has_attr(id, HL_INVERSE, modec);
18043 else /* italic */
18044 p = highlight_has_attr(id, HL_ITALIC, modec);
18045 break;
18046
18047 case 'n': /* name */
18048 p = get_highlight_name(NULL, id - 1);
18049 break;
18050
18051 case 'r': /* reverse */
18052 p = highlight_has_attr(id, HL_INVERSE, modec);
18053 break;
18054
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018055 case 's':
18056 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18057 p = highlight_color(id, what, modec);
18058 else /* standout */
18059 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 break;
18061
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018062 case 'u':
18063 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18064 /* underline */
18065 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18066 else
18067 /* undercurl */
18068 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 break;
18070 }
18071
18072 if (p != NULL)
18073 p = vim_strsave(p);
18074#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075 rettv->v_type = VAR_STRING;
18076 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
18079/*
18080 * "synIDtrans(id)" function
18081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018083f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018084 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086{
18087 int id;
18088
18089#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091
18092 if (id > 0)
18093 id = syn_get_final_id(id);
18094 else
18095#endif
18096 id = 0;
18097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018098 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099}
18100
18101/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018102 * "synconcealed(lnum, col)" function
18103 */
18104 static void
18105f_synconcealed(argvars, rettv)
18106 typval_T *argvars UNUSED;
18107 typval_T *rettv;
18108{
18109#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18110 long lnum;
18111 long col;
18112 int syntax_flags = 0;
18113 int cchar;
18114 int matchid = 0;
18115 char_u str[NUMBUFLEN];
18116#endif
18117
18118 rettv->v_type = VAR_LIST;
18119 rettv->vval.v_list = NULL;
18120
18121#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18122 lnum = get_tv_lnum(argvars); /* -1 on type error */
18123 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18124
18125 vim_memset(str, NUL, sizeof(str));
18126
18127 if (rettv_list_alloc(rettv) != FAIL)
18128 {
18129 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18130 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18131 && curwin->w_p_cole > 0)
18132 {
18133 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18134 syntax_flags = get_syntax_info(&matchid);
18135
18136 /* get the conceal character */
18137 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18138 {
18139 cchar = syn_get_sub_char();
18140 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18141 cchar = lcs_conceal;
18142 if (cchar != NUL)
18143 {
18144# ifdef FEAT_MBYTE
18145 if (has_mbyte)
18146 (*mb_char2bytes)(cchar, str);
18147 else
18148# endif
18149 str[0] = cchar;
18150 }
18151 }
18152 }
18153
18154 list_append_number(rettv->vval.v_list,
18155 (syntax_flags & HL_CONCEAL) != 0);
18156 /* -1 to auto-determine strlen */
18157 list_append_string(rettv->vval.v_list, str, -1);
18158 list_append_number(rettv->vval.v_list, matchid);
18159 }
18160#endif
18161}
18162
18163/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018164 * "synstack(lnum, col)" function
18165 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018166 static void
18167f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018168 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018169 typval_T *rettv;
18170{
18171#ifdef FEAT_SYN_HL
18172 long lnum;
18173 long col;
18174 int i;
18175 int id;
18176#endif
18177
18178 rettv->v_type = VAR_LIST;
18179 rettv->vval.v_list = NULL;
18180
18181#ifdef FEAT_SYN_HL
18182 lnum = get_tv_lnum(argvars); /* -1 on type error */
18183 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18184
18185 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018186 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018187 && rettv_list_alloc(rettv) != FAIL)
18188 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018189 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018190 for (i = 0; ; ++i)
18191 {
18192 id = syn_get_stack_item(i);
18193 if (id < 0)
18194 break;
18195 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18196 break;
18197 }
18198 }
18199#endif
18200}
18201
18202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 * "system()" function
18204 */
18205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018206f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018207 typval_T *argvars;
18208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018210 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018212 char_u *infile = NULL;
18213 char_u buf[NUMBUFLEN];
18214 int err = FALSE;
18215 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018217 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018218 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018219
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018220 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018221 {
18222 /*
18223 * Write the string to a temp file, to be used for input of the shell
18224 * command.
18225 */
18226 if ((infile = vim_tempname('i')) == NULL)
18227 {
18228 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018229 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018230 }
18231
18232 fd = mch_fopen((char *)infile, WRITEBIN);
18233 if (fd == NULL)
18234 {
18235 EMSG2(_(e_notopen), infile);
18236 goto done;
18237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018238 p = get_tv_string_buf_chk(&argvars[1], buf);
18239 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018240 {
18241 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018242 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018243 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018244 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18245 err = TRUE;
18246 if (fclose(fd) != 0)
18247 err = TRUE;
18248 if (err)
18249 {
18250 EMSG(_("E677: Error writing temp file"));
18251 goto done;
18252 }
18253 }
18254
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018255 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18256 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018257
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258#ifdef USE_CR
18259 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018260 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 {
18262 char_u *s;
18263
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018264 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 {
18266 if (*s == CAR)
18267 *s = NL;
18268 }
18269 }
18270#else
18271# ifdef USE_CRNL
18272 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018273 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 {
18275 char_u *s, *d;
18276
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018277 d = res;
18278 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 {
18280 if (s[0] == CAR && s[1] == NL)
18281 ++s;
18282 *d++ = *s;
18283 }
18284 *d = NUL;
18285 }
18286# endif
18287#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018288
18289done:
18290 if (infile != NULL)
18291 {
18292 mch_remove(infile);
18293 vim_free(infile);
18294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018295 rettv->v_type = VAR_STRING;
18296 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297}
18298
18299/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018300 * "tabpagebuflist()" function
18301 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018302 static void
18303f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018304 typval_T *argvars UNUSED;
18305 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018306{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018307#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018308 tabpage_T *tp;
18309 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018310
18311 if (argvars[0].v_type == VAR_UNKNOWN)
18312 wp = firstwin;
18313 else
18314 {
18315 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18316 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018317 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018318 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018319 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018320 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018321 for (; wp != NULL; wp = wp->w_next)
18322 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018323 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018324 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018325 }
18326#endif
18327}
18328
18329
18330/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018331 * "tabpagenr()" function
18332 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018333 static void
18334f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018335 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018336 typval_T *rettv;
18337{
18338 int nr = 1;
18339#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018340 char_u *arg;
18341
18342 if (argvars[0].v_type != VAR_UNKNOWN)
18343 {
18344 arg = get_tv_string_chk(&argvars[0]);
18345 nr = 0;
18346 if (arg != NULL)
18347 {
18348 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018349 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018350 else
18351 EMSG2(_(e_invexpr2), arg);
18352 }
18353 }
18354 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018355 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018356#endif
18357 rettv->vval.v_number = nr;
18358}
18359
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018360
18361#ifdef FEAT_WINDOWS
18362static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18363
18364/*
18365 * Common code for tabpagewinnr() and winnr().
18366 */
18367 static int
18368get_winnr(tp, argvar)
18369 tabpage_T *tp;
18370 typval_T *argvar;
18371{
18372 win_T *twin;
18373 int nr = 1;
18374 win_T *wp;
18375 char_u *arg;
18376
18377 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18378 if (argvar->v_type != VAR_UNKNOWN)
18379 {
18380 arg = get_tv_string_chk(argvar);
18381 if (arg == NULL)
18382 nr = 0; /* type error; errmsg already given */
18383 else if (STRCMP(arg, "$") == 0)
18384 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18385 else if (STRCMP(arg, "#") == 0)
18386 {
18387 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18388 if (twin == NULL)
18389 nr = 0;
18390 }
18391 else
18392 {
18393 EMSG2(_(e_invexpr2), arg);
18394 nr = 0;
18395 }
18396 }
18397
18398 if (nr > 0)
18399 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18400 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018401 {
18402 if (wp == NULL)
18403 {
18404 /* didn't find it in this tabpage */
18405 nr = 0;
18406 break;
18407 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018408 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018409 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018410 return nr;
18411}
18412#endif
18413
18414/*
18415 * "tabpagewinnr()" function
18416 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018417 static void
18418f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018419 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018420 typval_T *rettv;
18421{
18422 int nr = 1;
18423#ifdef FEAT_WINDOWS
18424 tabpage_T *tp;
18425
18426 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18427 if (tp == NULL)
18428 nr = 0;
18429 else
18430 nr = get_winnr(tp, &argvars[1]);
18431#endif
18432 rettv->vval.v_number = nr;
18433}
18434
18435
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018436/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018437 * "tagfiles()" function
18438 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018439 static void
18440f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018441 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018442 typval_T *rettv;
18443{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018444 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018445 tagname_T tn;
18446 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018449 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018450 fname = alloc(MAXPATHL);
18451 if (fname == NULL)
18452 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018454 for (first = TRUE; ; first = FALSE)
18455 if (get_tagfname(&tn, first, fname) == FAIL
18456 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018457 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018458 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018459 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018460}
18461
18462/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018463 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018464 */
18465 static void
18466f_taglist(argvars, rettv)
18467 typval_T *argvars;
18468 typval_T *rettv;
18469{
18470 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018471
18472 tag_pattern = get_tv_string(&argvars[0]);
18473
18474 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018475 if (*tag_pattern == NUL)
18476 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018477
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018478 if (rettv_list_alloc(rettv) == OK)
18479 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018480}
18481
18482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 * "tempname()" function
18484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018486f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018487 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489{
18490 static int x = 'A';
18491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018492 rettv->v_type = VAR_STRING;
18493 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494
18495 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18496 * names. Skip 'I' and 'O', they are used for shell redirection. */
18497 do
18498 {
18499 if (x == 'Z')
18500 x = '0';
18501 else if (x == '9')
18502 x = 'A';
18503 else
18504 {
18505#ifdef EBCDIC
18506 if (x == 'I')
18507 x = 'J';
18508 else if (x == 'R')
18509 x = 'S';
18510 else
18511#endif
18512 ++x;
18513 }
18514 } while (x == 'I' || x == 'O');
18515}
18516
18517/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018518 * "test(list)" function: Just checking the walls...
18519 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018520 static void
18521f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018522 typval_T *argvars UNUSED;
18523 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018524{
18525 /* Used for unit testing. Change the code below to your liking. */
18526#if 0
18527 listitem_T *li;
18528 list_T *l;
18529 char_u *bad, *good;
18530
18531 if (argvars[0].v_type != VAR_LIST)
18532 return;
18533 l = argvars[0].vval.v_list;
18534 if (l == NULL)
18535 return;
18536 li = l->lv_first;
18537 if (li == NULL)
18538 return;
18539 bad = get_tv_string(&li->li_tv);
18540 li = li->li_next;
18541 if (li == NULL)
18542 return;
18543 good = get_tv_string(&li->li_tv);
18544 rettv->vval.v_number = test_edit_score(bad, good);
18545#endif
18546}
18547
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018548#ifdef FEAT_FLOAT
18549/*
18550 * "tan()" function
18551 */
18552 static void
18553f_tan(argvars, rettv)
18554 typval_T *argvars;
18555 typval_T *rettv;
18556{
18557 float_T f;
18558
18559 rettv->v_type = VAR_FLOAT;
18560 if (get_float_arg(argvars, &f) == OK)
18561 rettv->vval.v_float = tan(f);
18562 else
18563 rettv->vval.v_float = 0.0;
18564}
18565
18566/*
18567 * "tanh()" function
18568 */
18569 static void
18570f_tanh(argvars, rettv)
18571 typval_T *argvars;
18572 typval_T *rettv;
18573{
18574 float_T f;
18575
18576 rettv->v_type = VAR_FLOAT;
18577 if (get_float_arg(argvars, &f) == OK)
18578 rettv->vval.v_float = tanh(f);
18579 else
18580 rettv->vval.v_float = 0.0;
18581}
18582#endif
18583
Bram Moolenaard52d9742005-08-21 22:20:28 +000018584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 * "tolower(string)" function
18586 */
18587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018589 typval_T *argvars;
18590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591{
18592 char_u *p;
18593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 p = vim_strsave(get_tv_string(&argvars[0]));
18595 rettv->v_type = VAR_STRING;
18596 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
18598 if (p != NULL)
18599 while (*p != NUL)
18600 {
18601#ifdef FEAT_MBYTE
18602 int l;
18603
18604 if (enc_utf8)
18605 {
18606 int c, lc;
18607
18608 c = utf_ptr2char(p);
18609 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018610 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611 /* TODO: reallocate string when byte count changes. */
18612 if (utf_char2len(lc) == l)
18613 utf_char2bytes(lc, p);
18614 p += l;
18615 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018616 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 p += l; /* skip multi-byte character */
18618 else
18619#endif
18620 {
18621 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18622 ++p;
18623 }
18624 }
18625}
18626
18627/*
18628 * "toupper(string)" function
18629 */
18630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018631f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018632 typval_T *argvars;
18633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018636 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637}
18638
18639/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018640 * "tr(string, fromstr, tostr)" function
18641 */
18642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018644 typval_T *argvars;
18645 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018646{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018647 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018648 char_u *fromstr;
18649 char_u *tostr;
18650 char_u *p;
18651#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018652 int inlen;
18653 int fromlen;
18654 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018655 int idx;
18656 char_u *cpstr;
18657 int cplen;
18658 int first = TRUE;
18659#endif
18660 char_u buf[NUMBUFLEN];
18661 char_u buf2[NUMBUFLEN];
18662 garray_T ga;
18663
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018664 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018665 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18666 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018667
18668 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 rettv->v_type = VAR_STRING;
18670 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018671 if (fromstr == NULL || tostr == NULL)
18672 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018673 ga_init2(&ga, (int)sizeof(char), 80);
18674
18675#ifdef FEAT_MBYTE
18676 if (!has_mbyte)
18677#endif
18678 /* not multi-byte: fromstr and tostr must be the same length */
18679 if (STRLEN(fromstr) != STRLEN(tostr))
18680 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018681#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018682error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018683#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018684 EMSG2(_(e_invarg2), fromstr);
18685 ga_clear(&ga);
18686 return;
18687 }
18688
18689 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018690 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018691 {
18692#ifdef FEAT_MBYTE
18693 if (has_mbyte)
18694 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018695 inlen = (*mb_ptr2len)(in_str);
18696 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018697 cplen = inlen;
18698 idx = 0;
18699 for (p = fromstr; *p != NUL; p += fromlen)
18700 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018701 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018702 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018703 {
18704 for (p = tostr; *p != NUL; p += tolen)
18705 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018706 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018707 if (idx-- == 0)
18708 {
18709 cplen = tolen;
18710 cpstr = p;
18711 break;
18712 }
18713 }
18714 if (*p == NUL) /* tostr is shorter than fromstr */
18715 goto error;
18716 break;
18717 }
18718 ++idx;
18719 }
18720
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018721 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018722 {
18723 /* Check that fromstr and tostr have the same number of
18724 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018725 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018726 first = FALSE;
18727 for (p = tostr; *p != NUL; p += tolen)
18728 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018729 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018730 --idx;
18731 }
18732 if (idx != 0)
18733 goto error;
18734 }
18735
18736 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018737 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018738 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018739
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018740 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018741 }
18742 else
18743#endif
18744 {
18745 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018746 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018747 if (p != NULL)
18748 ga_append(&ga, tostr[p - fromstr]);
18749 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018750 ga_append(&ga, *in_str);
18751 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018752 }
18753 }
18754
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018755 /* add a terminating NUL */
18756 ga_grow(&ga, 1);
18757 ga_append(&ga, NUL);
18758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018759 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018760}
18761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018762#ifdef FEAT_FLOAT
18763/*
18764 * "trunc({float})" function
18765 */
18766 static void
18767f_trunc(argvars, rettv)
18768 typval_T *argvars;
18769 typval_T *rettv;
18770{
18771 float_T f;
18772
18773 rettv->v_type = VAR_FLOAT;
18774 if (get_float_arg(argvars, &f) == OK)
18775 /* trunc() is not in C90, use floor() or ceil() instead. */
18776 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18777 else
18778 rettv->vval.v_float = 0.0;
18779}
18780#endif
18781
Bram Moolenaar8299df92004-07-10 09:47:34 +000018782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 * "type(expr)" function
18784 */
18785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018787 typval_T *argvars;
18788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018790 int n;
18791
18792 switch (argvars[0].v_type)
18793 {
18794 case VAR_NUMBER: n = 0; break;
18795 case VAR_STRING: n = 1; break;
18796 case VAR_FUNC: n = 2; break;
18797 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018798 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018799#ifdef FEAT_FLOAT
18800 case VAR_FLOAT: n = 5; break;
18801#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018802 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18803 }
18804 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805}
18806
18807/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018808 * "undofile(name)" function
18809 */
18810 static void
18811f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018812 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018813 typval_T *rettv;
18814{
18815 rettv->v_type = VAR_STRING;
18816#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018817 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018818 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018819
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018820 if (*fname == NUL)
18821 {
18822 /* If there is no file name there will be no undo file. */
18823 rettv->vval.v_string = NULL;
18824 }
18825 else
18826 {
18827 char_u *ffname = FullName_save(fname, FALSE);
18828
18829 if (ffname != NULL)
18830 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18831 vim_free(ffname);
18832 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018833 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018834#else
18835 rettv->vval.v_string = NULL;
18836#endif
18837}
18838
18839/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018840 * "undotree()" function
18841 */
18842 static void
18843f_undotree(argvars, rettv)
18844 typval_T *argvars UNUSED;
18845 typval_T *rettv;
18846{
18847 if (rettv_dict_alloc(rettv) == OK)
18848 {
18849 dict_T *dict = rettv->vval.v_dict;
18850 list_T *list;
18851
Bram Moolenaar730cde92010-06-27 05:18:54 +020018852 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018853 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018854 dict_add_nr_str(dict, "save_last",
18855 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018856 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18857 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018858 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018859
18860 list = list_alloc();
18861 if (list != NULL)
18862 {
18863 u_eval_tree(curbuf->b_u_oldhead, list);
18864 dict_add_list(dict, "entries", list);
18865 }
18866 }
18867}
18868
18869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018870 * "values(dict)" function
18871 */
18872 static void
18873f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018874 typval_T *argvars;
18875 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018876{
18877 dict_list(argvars, rettv, 1);
18878}
18879
18880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 * "virtcol(string)" function
18882 */
18883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018884f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 typval_T *argvars;
18886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
18888 colnr_T vcol = 0;
18889 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018890 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018892 fp = var2fpos(&argvars[0], FALSE, &fnum);
18893 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18894 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895 {
18896 getvvcol(curwin, fp, NULL, NULL, &vcol);
18897 ++vcol;
18898 }
18899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018900 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901}
18902
18903/*
18904 * "visualmode()" function
18905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018907f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018908 typval_T *argvars UNUSED;
18909 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 char_u str[2];
18912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018913 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914 str[0] = curbuf->b_visual_mode_eval;
18915 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018916 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917
18918 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018919 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921}
18922
18923/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018924 * "wildmenumode()" function
18925 */
18926 static void
18927f_wildmenumode(argvars, rettv)
18928 typval_T *argvars UNUSED;
18929 typval_T *rettv UNUSED;
18930{
18931#ifdef FEAT_WILDMENU
18932 if (wild_menu_showing)
18933 rettv->vval.v_number = 1;
18934#endif
18935}
18936
18937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 * "winbufnr(nr)" function
18939 */
18940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018941f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018942 typval_T *argvars;
18943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944{
18945 win_T *wp;
18946
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018947 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018949 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018951 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952}
18953
18954/*
18955 * "wincol()" function
18956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018958f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018959 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
18962 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964}
18965
18966/*
18967 * "winheight(nr)" function
18968 */
18969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018971 typval_T *argvars;
18972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973{
18974 win_T *wp;
18975
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018976 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981}
18982
18983/*
18984 * "winline()" function
18985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990{
18991 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018992 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993}
18994
18995/*
18996 * "winnr()" function
18997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018999f_winnr(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 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019004
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019006 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019008 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009}
19010
19011/*
19012 * "winrestcmd()" function
19013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019015f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018{
19019#ifdef FEAT_WINDOWS
19020 win_T *wp;
19021 int winnr = 1;
19022 garray_T ga;
19023 char_u buf[50];
19024
19025 ga_init2(&ga, (int)sizeof(char), 70);
19026 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19027 {
19028 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19029 ga_concat(&ga, buf);
19030# ifdef FEAT_VERTSPLIT
19031 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19032 ga_concat(&ga, buf);
19033# endif
19034 ++winnr;
19035 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019036 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019038 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019040 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019042 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043}
19044
19045/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019046 * "winrestview()" function
19047 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019048 static void
19049f_winrestview(argvars, rettv)
19050 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019051 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019052{
19053 dict_T *dict;
19054
19055 if (argvars[0].v_type != VAR_DICT
19056 || (dict = argvars[0].vval.v_dict) == NULL)
19057 EMSG(_(e_invarg));
19058 else
19059 {
19060 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19061 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19062#ifdef FEAT_VIRTUALEDIT
19063 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19064#endif
19065 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019066 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019067
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019068 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019069#ifdef FEAT_DIFF
19070 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19071#endif
19072 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19073 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19074
19075 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019076 win_new_height(curwin, curwin->w_height);
19077# ifdef FEAT_VERTSPLIT
19078 win_new_width(curwin, W_WIDTH(curwin));
19079# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019080 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019081
19082 if (curwin->w_topline == 0)
19083 curwin->w_topline = 1;
19084 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19085 curwin->w_topline = curbuf->b_ml.ml_line_count;
19086#ifdef FEAT_DIFF
19087 check_topfill(curwin, TRUE);
19088#endif
19089 }
19090}
19091
19092/*
19093 * "winsaveview()" function
19094 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019095 static void
19096f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019097 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019098 typval_T *rettv;
19099{
19100 dict_T *dict;
19101
Bram Moolenaara800b422010-06-27 01:15:55 +020019102 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019103 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019104 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019105
19106 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19107 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19108#ifdef FEAT_VIRTUALEDIT
19109 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19110#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019111 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019112 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19113
19114 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19115#ifdef FEAT_DIFF
19116 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19117#endif
19118 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19119 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19120}
19121
19122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 * "winwidth(nr)" function
19124 */
19125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019126f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019127 typval_T *argvars;
19128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129{
19130 win_T *wp;
19131
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019132 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 else
19136#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019137 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140#endif
19141}
19142
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019144 * "writefile()" function
19145 */
19146 static void
19147f_writefile(argvars, rettv)
19148 typval_T *argvars;
19149 typval_T *rettv;
19150{
19151 int binary = FALSE;
19152 char_u *fname;
19153 FILE *fd;
19154 listitem_T *li;
19155 char_u *s;
19156 int ret = 0;
19157 int c;
19158
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019159 if (check_restricted() || check_secure())
19160 return;
19161
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019162 if (argvars[0].v_type != VAR_LIST)
19163 {
19164 EMSG2(_(e_listarg), "writefile()");
19165 return;
19166 }
19167 if (argvars[0].vval.v_list == NULL)
19168 return;
19169
19170 if (argvars[2].v_type != VAR_UNKNOWN
19171 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19172 binary = TRUE;
19173
19174 /* Always open the file in binary mode, library functions have a mind of
19175 * their own about CR-LF conversion. */
19176 fname = get_tv_string(&argvars[1]);
19177 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19178 {
19179 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19180 ret = -1;
19181 }
19182 else
19183 {
19184 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19185 li = li->li_next)
19186 {
19187 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19188 {
19189 if (*s == '\n')
19190 c = putc(NUL, fd);
19191 else
19192 c = putc(*s, fd);
19193 if (c == EOF)
19194 {
19195 ret = -1;
19196 break;
19197 }
19198 }
19199 if (!binary || li->li_next != NULL)
19200 if (putc('\n', fd) == EOF)
19201 {
19202 ret = -1;
19203 break;
19204 }
19205 if (ret < 0)
19206 {
19207 EMSG(_(e_write));
19208 break;
19209 }
19210 }
19211 fclose(fd);
19212 }
19213
19214 rettv->vval.v_number = ret;
19215}
19216
19217/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019218 * "xor(expr, expr)" function
19219 */
19220 static void
19221f_xor(argvars, rettv)
19222 typval_T *argvars;
19223 typval_T *rettv;
19224{
19225 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19226 ^ get_tv_number_chk(&argvars[1], NULL);
19227}
19228
19229
19230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019232 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 */
19234 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019235var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019236 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019237 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019238 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019240 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019241 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019242 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243
Bram Moolenaara5525202006-03-02 22:52:09 +000019244 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019245 if (varp->v_type == VAR_LIST)
19246 {
19247 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019248 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019249 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019250 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019251
19252 l = varp->vval.v_list;
19253 if (l == NULL)
19254 return NULL;
19255
19256 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019257 pos.lnum = list_find_nr(l, 0L, &error);
19258 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019259 return NULL; /* invalid line number */
19260
19261 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019262 pos.col = list_find_nr(l, 1L, &error);
19263 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019264 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019265 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019266
19267 /* We accept "$" for the column number: last column. */
19268 li = list_find(l, 1L);
19269 if (li != NULL && li->li_tv.v_type == VAR_STRING
19270 && li->li_tv.vval.v_string != NULL
19271 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19272 pos.col = len + 1;
19273
Bram Moolenaara5525202006-03-02 22:52:09 +000019274 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019275 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019276 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019277 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019278
Bram Moolenaara5525202006-03-02 22:52:09 +000019279#ifdef FEAT_VIRTUALEDIT
19280 /* Get the virtual offset. Defaults to zero. */
19281 pos.coladd = list_find_nr(l, 2L, &error);
19282 if (error)
19283 pos.coladd = 0;
19284#endif
19285
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019286 return &pos;
19287 }
19288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019289 name = get_tv_string_chk(varp);
19290 if (name == NULL)
19291 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019292 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019294 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19295 {
19296 if (VIsual_active)
19297 return &VIsual;
19298 return &curwin->w_cursor;
19299 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019300 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019302 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19304 return NULL;
19305 return pp;
19306 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019307
19308#ifdef FEAT_VIRTUALEDIT
19309 pos.coladd = 0;
19310#endif
19311
Bram Moolenaar477933c2007-07-17 14:32:23 +000019312 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019313 {
19314 pos.col = 0;
19315 if (name[1] == '0') /* "w0": first visible line */
19316 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019317 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019318 pos.lnum = curwin->w_topline;
19319 return &pos;
19320 }
19321 else if (name[1] == '$') /* "w$": last visible line */
19322 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019323 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019324 pos.lnum = curwin->w_botline - 1;
19325 return &pos;
19326 }
19327 }
19328 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019330 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 {
19332 pos.lnum = curbuf->b_ml.ml_line_count;
19333 pos.col = 0;
19334 }
19335 else
19336 {
19337 pos.lnum = curwin->w_cursor.lnum;
19338 pos.col = (colnr_T)STRLEN(ml_get_curline());
19339 }
19340 return &pos;
19341 }
19342 return NULL;
19343}
19344
19345/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019346 * Convert list in "arg" into a position and optional file number.
19347 * When "fnump" is NULL there is no file number, only 3 items.
19348 * Note that the column is passed on as-is, the caller may want to decrement
19349 * it to use 1 for the first column.
19350 * Return FAIL when conversion is not possible, doesn't check the position for
19351 * validity.
19352 */
19353 static int
19354list2fpos(arg, posp, fnump)
19355 typval_T *arg;
19356 pos_T *posp;
19357 int *fnump;
19358{
19359 list_T *l = arg->vval.v_list;
19360 long i = 0;
19361 long n;
19362
Bram Moolenaarbde35262006-07-23 20:12:24 +000019363 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19364 * when "fnump" isn't NULL and "coladd" is optional. */
19365 if (arg->v_type != VAR_LIST
19366 || l == NULL
19367 || l->lv_len < (fnump == NULL ? 2 : 3)
19368 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019369 return FAIL;
19370
19371 if (fnump != NULL)
19372 {
19373 n = list_find_nr(l, i++, NULL); /* fnum */
19374 if (n < 0)
19375 return FAIL;
19376 if (n == 0)
19377 n = curbuf->b_fnum; /* current buffer */
19378 *fnump = n;
19379 }
19380
19381 n = list_find_nr(l, i++, NULL); /* lnum */
19382 if (n < 0)
19383 return FAIL;
19384 posp->lnum = n;
19385
19386 n = list_find_nr(l, i++, NULL); /* col */
19387 if (n < 0)
19388 return FAIL;
19389 posp->col = n;
19390
19391#ifdef FEAT_VIRTUALEDIT
19392 n = list_find_nr(l, i, NULL);
19393 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019394 posp->coladd = 0;
19395 else
19396 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019397#endif
19398
19399 return OK;
19400}
19401
19402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 * Get the length of an environment variable name.
19404 * Advance "arg" to the first character after the name.
19405 * Return 0 for error.
19406 */
19407 static int
19408get_env_len(arg)
19409 char_u **arg;
19410{
19411 char_u *p;
19412 int len;
19413
19414 for (p = *arg; vim_isIDc(*p); ++p)
19415 ;
19416 if (p == *arg) /* no name found */
19417 return 0;
19418
19419 len = (int)(p - *arg);
19420 *arg = p;
19421 return len;
19422}
19423
19424/*
19425 * Get the length of the name of a function or internal variable.
19426 * "arg" is advanced to the first non-white character after the name.
19427 * Return 0 if something is wrong.
19428 */
19429 static int
19430get_id_len(arg)
19431 char_u **arg;
19432{
19433 char_u *p;
19434 int len;
19435
19436 /* Find the end of the name. */
19437 for (p = *arg; eval_isnamec(*p); ++p)
19438 ;
19439 if (p == *arg) /* no name found */
19440 return 0;
19441
19442 len = (int)(p - *arg);
19443 *arg = skipwhite(p);
19444
19445 return len;
19446}
19447
19448/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019449 * Get the length of the name of a variable or function.
19450 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019452 * Return -1 if curly braces expansion failed.
19453 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 * If the name contains 'magic' {}'s, expand them and return the
19455 * expanded name in an allocated string via 'alias' - caller must free.
19456 */
19457 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019458get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 char_u **arg;
19460 char_u **alias;
19461 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019462 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463{
19464 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 char_u *p;
19466 char_u *expr_start;
19467 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468
19469 *alias = NULL; /* default to no alias */
19470
19471 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19472 && (*arg)[2] == (int)KE_SNR)
19473 {
19474 /* hard coded <SNR>, already translated */
19475 *arg += 3;
19476 return get_id_len(arg) + 3;
19477 }
19478 len = eval_fname_script(*arg);
19479 if (len > 0)
19480 {
19481 /* literal "<SID>", "s:" or "<SNR>" */
19482 *arg += len;
19483 }
19484
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019486 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019488 p = find_name_end(*arg, &expr_start, &expr_end,
19489 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 if (expr_start != NULL)
19491 {
19492 char_u *temp_string;
19493
19494 if (!evaluate)
19495 {
19496 len += (int)(p - *arg);
19497 *arg = skipwhite(p);
19498 return len;
19499 }
19500
19501 /*
19502 * Include any <SID> etc in the expanded string:
19503 * Thus the -len here.
19504 */
19505 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19506 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019507 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 *alias = temp_string;
19509 *arg = skipwhite(p);
19510 return (int)STRLEN(temp_string);
19511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512
19513 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019514 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 EMSG2(_(e_invexpr2), *arg);
19516
19517 return len;
19518}
19519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019520/*
19521 * Find the end of a variable or function name, taking care of magic braces.
19522 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19523 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019524 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019525 * Return a pointer to just after the name. Equal to "arg" if there is no
19526 * valid name.
19527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019529find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 char_u *arg;
19531 char_u **expr_start;
19532 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019533 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535 int mb_nest = 0;
19536 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 char_u *p;
19538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 *expr_start = NULL;
19542 *expr_end = NULL;
19543 }
19544
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019545 /* Quick check for valid starting character. */
19546 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19547 return arg;
19548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019549 for (p = arg; *p != NUL
19550 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019551 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019552 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019554 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019555 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019556 if (*p == '\'')
19557 {
19558 /* skip over 'string' to avoid counting [ and ] inside it. */
19559 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19560 ;
19561 if (*p == NUL)
19562 break;
19563 }
19564 else if (*p == '"')
19565 {
19566 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19567 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19568 if (*p == '\\' && p[1] != NUL)
19569 ++p;
19570 if (*p == NUL)
19571 break;
19572 }
19573
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019574 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019576 if (*p == '[')
19577 ++br_nest;
19578 else if (*p == ']')
19579 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019582 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019584 if (*p == '{')
19585 {
19586 mb_nest++;
19587 if (expr_start != NULL && *expr_start == NULL)
19588 *expr_start = p;
19589 }
19590 else if (*p == '}')
19591 {
19592 mb_nest--;
19593 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19594 *expr_end = p;
19595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 }
19598
19599 return p;
19600}
19601
19602/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019603 * Expands out the 'magic' {}'s in a variable/function name.
19604 * Note that this can call itself recursively, to deal with
19605 * constructs like foo{bar}{baz}{bam}
19606 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19607 * "in_start" ^
19608 * "expr_start" ^
19609 * "expr_end" ^
19610 * "in_end" ^
19611 *
19612 * Returns a new allocated string, which the caller must free.
19613 * Returns NULL for failure.
19614 */
19615 static char_u *
19616make_expanded_name(in_start, expr_start, expr_end, in_end)
19617 char_u *in_start;
19618 char_u *expr_start;
19619 char_u *expr_end;
19620 char_u *in_end;
19621{
19622 char_u c1;
19623 char_u *retval = NULL;
19624 char_u *temp_result;
19625 char_u *nextcmd = NULL;
19626
19627 if (expr_end == NULL || in_end == NULL)
19628 return NULL;
19629 *expr_start = NUL;
19630 *expr_end = NUL;
19631 c1 = *in_end;
19632 *in_end = NUL;
19633
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019634 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019635 if (temp_result != NULL && nextcmd == NULL)
19636 {
19637 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19638 + (in_end - expr_end) + 1));
19639 if (retval != NULL)
19640 {
19641 STRCPY(retval, in_start);
19642 STRCAT(retval, temp_result);
19643 STRCAT(retval, expr_end + 1);
19644 }
19645 }
19646 vim_free(temp_result);
19647
19648 *in_end = c1; /* put char back for error messages */
19649 *expr_start = '{';
19650 *expr_end = '}';
19651
19652 if (retval != NULL)
19653 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019654 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019655 if (expr_start != NULL)
19656 {
19657 /* Further expansion! */
19658 temp_result = make_expanded_name(retval, expr_start,
19659 expr_end, temp_result);
19660 vim_free(retval);
19661 retval = temp_result;
19662 }
19663 }
19664
19665 return retval;
19666}
19667
19668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019670 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 */
19672 static int
19673eval_isnamec(c)
19674 int c;
19675{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019676 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19677}
19678
19679/*
19680 * Return TRUE if character "c" can be used as the first character in a
19681 * variable or function name (excluding '{' and '}').
19682 */
19683 static int
19684eval_isnamec1(c)
19685 int c;
19686{
19687 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688}
19689
19690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 * Set number v: variable to "val".
19692 */
19693 void
19694set_vim_var_nr(idx, val)
19695 int idx;
19696 long val;
19697{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019698 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699}
19700
19701/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019702 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 */
19704 long
19705get_vim_var_nr(idx)
19706 int idx;
19707{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019708 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019711/*
19712 * Get string v: variable value. Uses a static buffer, can only be used once.
19713 */
19714 char_u *
19715get_vim_var_str(idx)
19716 int idx;
19717{
19718 return get_tv_string(&vimvars[idx].vv_tv);
19719}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019720
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019722 * Get List v: variable value. Caller must take care of reference count when
19723 * needed.
19724 */
19725 list_T *
19726get_vim_var_list(idx)
19727 int idx;
19728{
19729 return vimvars[idx].vv_list;
19730}
19731
19732/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019733 * Set v:char to character "c".
19734 */
19735 void
19736set_vim_var_char(c)
19737 int c;
19738{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019739 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019740
19741#ifdef FEAT_MBYTE
19742 if (has_mbyte)
19743 buf[(*mb_char2bytes)(c, buf)] = NUL;
19744 else
19745#endif
19746 {
19747 buf[0] = c;
19748 buf[1] = NUL;
19749 }
19750 set_vim_var_string(VV_CHAR, buf, -1);
19751}
19752
19753/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019754 * Set v:count to "count" and v:count1 to "count1".
19755 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 */
19757 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019758set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 long count;
19760 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019761 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019763 if (set_prevcount)
19764 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019765 vimvars[VV_COUNT].vv_nr = count;
19766 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767}
19768
19769/*
19770 * Set string v: variable to a copy of "val".
19771 */
19772 void
19773set_vim_var_string(idx, val, len)
19774 int idx;
19775 char_u *val;
19776 int len; /* length of "val" to use or -1 (whole string) */
19777{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019778 /* Need to do this (at least) once, since we can't initialize a union.
19779 * Will always be invoked when "v:progname" is set. */
19780 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19781
Bram Moolenaare9a41262005-01-15 22:18:47 +000019782 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019784 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019786 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019788 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789}
19790
19791/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019792 * Set List v: variable to "val".
19793 */
19794 void
19795set_vim_var_list(idx, val)
19796 int idx;
19797 list_T *val;
19798{
19799 list_unref(vimvars[idx].vv_list);
19800 vimvars[idx].vv_list = val;
19801 if (val != NULL)
19802 ++val->lv_refcount;
19803}
19804
19805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 * Set v:register if needed.
19807 */
19808 void
19809set_reg_var(c)
19810 int c;
19811{
19812 char_u regname;
19813
19814 if (c == 0 || c == ' ')
19815 regname = '"';
19816 else
19817 regname = c;
19818 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019819 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 set_vim_var_string(VV_REG, &regname, 1);
19821}
19822
19823/*
19824 * Get or set v:exception. If "oldval" == NULL, return the current value.
19825 * Otherwise, restore the value to "oldval" and return NULL.
19826 * Must always be called in pairs to save and restore v:exception! Does not
19827 * take care of memory allocations.
19828 */
19829 char_u *
19830v_exception(oldval)
19831 char_u *oldval;
19832{
19833 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019834 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
Bram Moolenaare9a41262005-01-15 22:18:47 +000019836 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 return NULL;
19838}
19839
19840/*
19841 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19842 * Otherwise, restore the value to "oldval" and return NULL.
19843 * Must always be called in pairs to save and restore v:throwpoint! Does not
19844 * take care of memory allocations.
19845 */
19846 char_u *
19847v_throwpoint(oldval)
19848 char_u *oldval;
19849{
19850 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019851 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852
Bram Moolenaare9a41262005-01-15 22:18:47 +000019853 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 return NULL;
19855}
19856
19857#if defined(FEAT_AUTOCMD) || defined(PROTO)
19858/*
19859 * Set v:cmdarg.
19860 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19861 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19862 * Must always be called in pairs!
19863 */
19864 char_u *
19865set_cmdarg(eap, oldarg)
19866 exarg_T *eap;
19867 char_u *oldarg;
19868{
19869 char_u *oldval;
19870 char_u *newval;
19871 unsigned len;
19872
Bram Moolenaare9a41262005-01-15 22:18:47 +000019873 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019874 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019876 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019877 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019878 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 }
19880
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019881 if (eap->force_bin == FORCE_BIN)
19882 len = 6;
19883 else if (eap->force_bin == FORCE_NOBIN)
19884 len = 8;
19885 else
19886 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019887
19888 if (eap->read_edit)
19889 len += 7;
19890
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019891 if (eap->force_ff != 0)
19892 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19893# ifdef FEAT_MBYTE
19894 if (eap->force_enc != 0)
19895 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019896 if (eap->bad_char != 0)
19897 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019898# endif
19899
19900 newval = alloc(len + 1);
19901 if (newval == NULL)
19902 return NULL;
19903
19904 if (eap->force_bin == FORCE_BIN)
19905 sprintf((char *)newval, " ++bin");
19906 else if (eap->force_bin == FORCE_NOBIN)
19907 sprintf((char *)newval, " ++nobin");
19908 else
19909 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019910
19911 if (eap->read_edit)
19912 STRCAT(newval, " ++edit");
19913
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019914 if (eap->force_ff != 0)
19915 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19916 eap->cmd + eap->force_ff);
19917# ifdef FEAT_MBYTE
19918 if (eap->force_enc != 0)
19919 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19920 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019921 if (eap->bad_char == BAD_KEEP)
19922 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19923 else if (eap->bad_char == BAD_DROP)
19924 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19925 else if (eap->bad_char != 0)
19926 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019927# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019928 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019929 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930}
19931#endif
19932
19933/*
19934 * Get the value of internal variable "name".
19935 * Return OK or FAIL.
19936 */
19937 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019938get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 char_u *name;
19940 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019942 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019943 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944{
19945 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 typval_T *tv = NULL;
19947 typval_T atv;
19948 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950
19951 /* truncate the name, so that we can use strcmp() */
19952 cc = name[len];
19953 name[len] = NUL;
19954
19955 /*
19956 * Check for "b:changedtick".
19957 */
19958 if (STRCMP(name, "b:changedtick") == 0)
19959 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019960 atv.v_type = VAR_NUMBER;
19961 atv.vval.v_number = curbuf->b_changedtick;
19962 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 }
19964
19965 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 * Check for user-defined variables.
19967 */
19968 else
19969 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019970 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 }
19974
Bram Moolenaare9a41262005-01-15 22:18:47 +000019975 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019977 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 ret = FAIL;
19980 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019982 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983
19984 name[len] = cc;
19985
19986 return ret;
19987}
19988
19989/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019990 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19991 * Also handle function call with Funcref variable: func(expr)
19992 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19993 */
19994 static int
19995handle_subscript(arg, rettv, evaluate, verbose)
19996 char_u **arg;
19997 typval_T *rettv;
19998 int evaluate; /* do more than finding the end */
19999 int verbose; /* give error messages */
20000{
20001 int ret = OK;
20002 dict_T *selfdict = NULL;
20003 char_u *s;
20004 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020005 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020006
20007 while (ret == OK
20008 && (**arg == '['
20009 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020010 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020011 && !vim_iswhite(*(*arg - 1)))
20012 {
20013 if (**arg == '(')
20014 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020015 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020016 if (evaluate)
20017 {
20018 functv = *rettv;
20019 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020020
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020021 /* Invoke the function. Recursive! */
20022 s = functv.vval.v_string;
20023 }
20024 else
20025 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020026 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020027 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20028 &len, evaluate, selfdict);
20029
20030 /* Clear the funcref afterwards, so that deleting it while
20031 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020032 if (evaluate)
20033 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020034
20035 /* Stop the expression evaluation when immediately aborting on
20036 * error, or when an interrupt occurred or an exception was thrown
20037 * but not caught. */
20038 if (aborting())
20039 {
20040 if (ret == OK)
20041 clear_tv(rettv);
20042 ret = FAIL;
20043 }
20044 dict_unref(selfdict);
20045 selfdict = NULL;
20046 }
20047 else /* **arg == '[' || **arg == '.' */
20048 {
20049 dict_unref(selfdict);
20050 if (rettv->v_type == VAR_DICT)
20051 {
20052 selfdict = rettv->vval.v_dict;
20053 if (selfdict != NULL)
20054 ++selfdict->dv_refcount;
20055 }
20056 else
20057 selfdict = NULL;
20058 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20059 {
20060 clear_tv(rettv);
20061 ret = FAIL;
20062 }
20063 }
20064 }
20065 dict_unref(selfdict);
20066 return ret;
20067}
20068
20069/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020070 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020071 * value).
20072 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020074alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020075{
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077}
20078
20079/*
20080 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 * The string "s" must have been allocated, it is consumed.
20082 * Return NULL for out of memory, the variable otherwise.
20083 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020084 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 char_u *s;
20087{
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020090 rettv = alloc_tv();
20091 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020093 rettv->v_type = VAR_STRING;
20094 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 }
20096 else
20097 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020102 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020104 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107{
20108 if (varp != NULL)
20109 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020110 switch (varp->v_type)
20111 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020112 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020113 func_unref(varp->vval.v_string);
20114 /*FALLTHROUGH*/
20115 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020116 vim_free(varp->vval.v_string);
20117 break;
20118 case VAR_LIST:
20119 list_unref(varp->vval.v_list);
20120 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020121 case VAR_DICT:
20122 dict_unref(varp->vval.v_dict);
20123 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020124 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020125#ifdef FEAT_FLOAT
20126 case VAR_FLOAT:
20127#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020128 case VAR_UNKNOWN:
20129 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020131 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020132 break;
20133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 vim_free(varp);
20135 }
20136}
20137
20138/*
20139 * Free the memory for a variable value and set the value to NULL or 0.
20140 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020141 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020142clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020143 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144{
20145 if (varp != NULL)
20146 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020147 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020149 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020150 func_unref(varp->vval.v_string);
20151 /*FALLTHROUGH*/
20152 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020153 vim_free(varp->vval.v_string);
20154 varp->vval.v_string = NULL;
20155 break;
20156 case VAR_LIST:
20157 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020158 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020159 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020160 case VAR_DICT:
20161 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020162 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020163 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020165 varp->vval.v_number = 0;
20166 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020167#ifdef FEAT_FLOAT
20168 case VAR_FLOAT:
20169 varp->vval.v_float = 0.0;
20170 break;
20171#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 case VAR_UNKNOWN:
20173 break;
20174 default:
20175 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020177 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 }
20179}
20180
20181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 * Set the value of a variable to NULL without freeing items.
20183 */
20184 static void
20185init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020187{
20188 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020189 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190}
20191
20192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 * Get the number value of a variable.
20194 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020195 * For incompatible types, return 0.
20196 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20197 * caller of incompatible types: it sets *denote to TRUE if "denote"
20198 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 */
20200 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020201get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020204 int error = FALSE;
20205
20206 return get_tv_number_chk(varp, &error); /* return 0L on error */
20207}
20208
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020209 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020210get_tv_number_chk(varp, denote)
20211 typval_T *varp;
20212 int *denote;
20213{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020214 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020216 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020218 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020219 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020220#ifdef FEAT_FLOAT
20221 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020222 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020223 break;
20224#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020225 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020226 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020227 break;
20228 case VAR_STRING:
20229 if (varp->vval.v_string != NULL)
20230 vim_str2nr(varp->vval.v_string, NULL, NULL,
20231 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020232 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020233 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020234 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020235 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020236 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020237 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020238 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020239 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020240 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020243 if (denote == NULL) /* useful for values that must be unsigned */
20244 n = -1;
20245 else
20246 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020247 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248}
20249
20250/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020251 * Get the lnum from the first argument.
20252 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020253 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 */
20255 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258{
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 linenr_T lnum;
20261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020262 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 if (lnum == 0) /* no valid number, try using line() */
20264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020265 rettv.v_type = VAR_NUMBER;
20266 f_line(argvars, &rettv);
20267 lnum = rettv.vval.v_number;
20268 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 }
20270 return lnum;
20271}
20272
20273/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020274 * Get the lnum from the first argument.
20275 * Also accepts "$", then "buf" is used.
20276 * Returns 0 on error.
20277 */
20278 static linenr_T
20279get_tv_lnum_buf(argvars, buf)
20280 typval_T *argvars;
20281 buf_T *buf;
20282{
20283 if (argvars[0].v_type == VAR_STRING
20284 && argvars[0].vval.v_string != NULL
20285 && argvars[0].vval.v_string[0] == '$'
20286 && buf != NULL)
20287 return buf->b_ml.ml_line_count;
20288 return get_tv_number_chk(&argvars[0], NULL);
20289}
20290
20291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 * Get the string value of a variable.
20293 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020294 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20295 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 * If the String variable has never been set, return an empty string.
20297 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020298 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20299 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 */
20301 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020302get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304{
20305 static char_u mybuf[NUMBUFLEN];
20306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020307 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308}
20309
20310 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020311get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020313 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020315 char_u *res = get_tv_string_buf_chk(varp, buf);
20316
20317 return res != NULL ? res : (char_u *)"";
20318}
20319
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020320 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020321get_tv_string_chk(varp)
20322 typval_T *varp;
20323{
20324 static char_u mybuf[NUMBUFLEN];
20325
20326 return get_tv_string_buf_chk(varp, mybuf);
20327}
20328
20329 static char_u *
20330get_tv_string_buf_chk(varp, buf)
20331 typval_T *varp;
20332 char_u *buf;
20333{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020334 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020336 case VAR_NUMBER:
20337 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20338 return buf;
20339 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020340 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020341 break;
20342 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020343 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020344 break;
20345 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020346 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020347 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020348#ifdef FEAT_FLOAT
20349 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020350 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020351 break;
20352#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020353 case VAR_STRING:
20354 if (varp->vval.v_string != NULL)
20355 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020356 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020357 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020361 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362}
20363
20364/*
20365 * Find variable "name" in the list of variables.
20366 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020367 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020368 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020371 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020372find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020374 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020375 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020378 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379
Bram Moolenaara7043832005-01-21 11:56:39 +000020380 ht = find_var_ht(name, &varname);
20381 if (htp != NULL)
20382 *htp = ht;
20383 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020385 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386}
20387
20388/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020389 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020390 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020393find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020395 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020396 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020397 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020398{
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 hashitem_T *hi;
20400
20401 if (*varname == NUL)
20402 {
20403 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020404 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020405 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020406 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 case 'g': return &globvars_var;
20408 case 'v': return &vimvars_var;
20409 case 'b': return &curbuf->b_bufvar;
20410 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020411#ifdef FEAT_WINDOWS
20412 case 't': return &curtab->tp_winvar;
20413#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020414 case 'l': return current_funccal == NULL
20415 ? NULL : &current_funccal->l_vars_var;
20416 case 'a': return current_funccal == NULL
20417 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 }
20419 return NULL;
20420 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020421
20422 hi = hash_find(ht, varname);
20423 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020424 {
20425 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020426 * worked find the variable again. Don't auto-load a script if it was
20427 * loaded already, otherwise it would be loaded every time when
20428 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020429 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020430 {
20431 /* Note: script_autoload() may make "hi" invalid. It must either
20432 * be obtained again or not used. */
20433 if (!script_autoload(varname, FALSE) || aborting())
20434 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020435 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020436 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020437 if (HASHITEM_EMPTY(hi))
20438 return NULL;
20439 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020441}
20442
20443/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020445 * Set "varname" to the start of name without ':'.
20446 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020448find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 char_u *name;
20450 char_u **varname;
20451{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020452 hashitem_T *hi;
20453
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 if (name[1] != ':')
20455 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020456 /* The name must not start with a colon or #. */
20457 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 return NULL;
20459 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020460
20461 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020462 hi = hash_find(&compat_hashtab, name);
20463 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020464 return &compat_hashtab;
20465
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 return &globvarht; /* global variable */
20468 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 }
20470 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020471 if (*name == 'g') /* global variable */
20472 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020473 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20474 */
20475 if (vim_strchr(name + 2, ':') != NULL
20476 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020477 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020479 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020481 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020482#ifdef FEAT_WINDOWS
20483 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020484 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020485#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 if (*name == 'v') /* v: variable */
20487 return &vimvarht;
20488 if (*name == 'a' && current_funccal != NULL) /* function argument */
20489 return &current_funccal->l_avars.dv_hashtab;
20490 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20491 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 if (*name == 's' /* script variable */
20493 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20494 return &SCRIPT_VARS(current_SID);
20495 return NULL;
20496}
20497
20498/*
20499 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020500 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 * Returns NULL when it doesn't exist.
20502 */
20503 char_u *
20504get_var_value(name)
20505 char_u *name;
20506{
Bram Moolenaar33570922005-01-25 22:26:29 +000020507 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020509 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 if (v == NULL)
20511 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020512 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513}
20514
20515/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020516 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 * sourcing this script and when executing functions defined in the script.
20518 */
20519 void
20520new_script_vars(id)
20521 scid_T id;
20522{
Bram Moolenaara7043832005-01-21 11:56:39 +000020523 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 hashtab_T *ht;
20525 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020526
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20528 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020529 /* Re-allocating ga_data means that an ht_array pointing to
20530 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020531 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020532 for (i = 1; i <= ga_scripts.ga_len; ++i)
20533 {
20534 ht = &SCRIPT_VARS(i);
20535 if (ht->ht_mask == HT_INIT_SIZE - 1)
20536 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020537 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020538 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020539 }
20540
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 while (ga_scripts.ga_len < id)
20542 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020543 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020544 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020545 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547 }
20548 }
20549}
20550
20551/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20553 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 */
20555 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020556init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020557 dict_T *dict;
20558 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020559 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020562 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020563 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020564 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020565 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020566 dict_var->di_tv.vval.v_dict = dict;
20567 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020568 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020569 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20570 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020574 * Unreference a dictionary initialized by init_var_dict().
20575 */
20576 void
20577unref_var_dict(dict)
20578 dict_T *dict;
20579{
20580 /* Now the dict needs to be freed if no one else is using it, go back to
20581 * normal reference counting. */
20582 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20583 dict_unref(dict);
20584}
20585
20586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020588 * Frees all allocated variables and the value they contain.
20589 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 */
20591 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020592vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020593 hashtab_T *ht;
20594{
20595 vars_clear_ext(ht, TRUE);
20596}
20597
20598/*
20599 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20600 */
20601 static void
20602vars_clear_ext(ht, free_val)
20603 hashtab_T *ht;
20604 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605{
Bram Moolenaara7043832005-01-21 11:56:39 +000020606 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020607 hashitem_T *hi;
20608 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609
Bram Moolenaar33570922005-01-25 22:26:29 +000020610 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020611 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020612 for (hi = ht->ht_array; todo > 0; ++hi)
20613 {
20614 if (!HASHITEM_EMPTY(hi))
20615 {
20616 --todo;
20617
Bram Moolenaar33570922005-01-25 22:26:29 +000020618 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020619 * ht_array might change then. hash_clear() takes care of it
20620 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 v = HI2DI(hi);
20622 if (free_val)
20623 clear_tv(&v->di_tv);
20624 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20625 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020626 }
20627 }
20628 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020629 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630}
20631
Bram Moolenaara7043832005-01-21 11:56:39 +000020632/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020633 * Delete a variable from hashtab "ht" at item "hi".
20634 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020637delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020638 hashtab_T *ht;
20639 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640{
Bram Moolenaar33570922005-01-25 22:26:29 +000020641 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020642
20643 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020644 clear_tv(&di->di_tv);
20645 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646}
20647
20648/*
20649 * List the value of one internal variable.
20650 */
20651 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020652list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020653 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020655 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020657 char_u *tofree;
20658 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020659 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020660
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020661 current_copyID += COPYID_INC;
20662 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020664 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020665 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666}
20667
Bram Moolenaar071d4272004-06-13 20:20:40 +000020668 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020669list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 char_u *prefix;
20671 char_u *name;
20672 int type;
20673 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020674 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675{
Bram Moolenaar31859182007-08-14 20:41:13 +000020676 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20677 msg_start();
20678 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 if (name != NULL) /* "a:" vars don't have a name stored */
20680 msg_puts(name);
20681 msg_putchar(' ');
20682 msg_advance(22);
20683 if (type == VAR_NUMBER)
20684 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 else if (type == VAR_FUNC)
20686 msg_putchar('*');
20687 else if (type == VAR_LIST)
20688 {
20689 msg_putchar('[');
20690 if (*string == '[')
20691 ++string;
20692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020693 else if (type == VAR_DICT)
20694 {
20695 msg_putchar('{');
20696 if (*string == '{')
20697 ++string;
20698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 else
20700 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020701
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020703
20704 if (type == VAR_FUNC)
20705 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020706 if (*first)
20707 {
20708 msg_clr_eos();
20709 *first = FALSE;
20710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711}
20712
20713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 * If the variable already exists, the value is updated.
20716 * Otherwise the variable is created.
20717 */
20718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020719set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020721 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723{
Bram Moolenaar33570922005-01-25 22:26:29 +000020724 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020728 ht = find_var_ht(name, &varname);
20729 if (ht == NULL || *varname == NUL)
20730 {
20731 EMSG2(_(e_illvar), name);
20732 return;
20733 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020734 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020735
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020736 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20737 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020738
Bram Moolenaar33570922005-01-25 22:26:29 +000020739 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020741 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020742 if (var_check_ro(v->di_flags, name)
20743 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020744 return;
20745 if (v->di_tv.v_type != tv->v_type
20746 && !((v->di_tv.v_type == VAR_STRING
20747 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020748 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020749 || tv->v_type == VAR_NUMBER))
20750#ifdef FEAT_FLOAT
20751 && !((v->di_tv.v_type == VAR_NUMBER
20752 || v->di_tv.v_type == VAR_FLOAT)
20753 && (tv->v_type == VAR_NUMBER
20754 || tv->v_type == VAR_FLOAT))
20755#endif
20756 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020757 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020758 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759 return;
20760 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020761
20762 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020763 * Handle setting internal v: variables separately: we don't change
20764 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020765 */
20766 if (ht == &vimvarht)
20767 {
20768 if (v->di_tv.v_type == VAR_STRING)
20769 {
20770 vim_free(v->di_tv.vval.v_string);
20771 if (copy || tv->v_type != VAR_STRING)
20772 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20773 else
20774 {
20775 /* Take over the string to avoid an extra alloc/free. */
20776 v->di_tv.vval.v_string = tv->vval.v_string;
20777 tv->vval.v_string = NULL;
20778 }
20779 }
20780 else if (v->di_tv.v_type != VAR_NUMBER)
20781 EMSG2(_(e_intern2), "set_var()");
20782 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020783 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020784 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020785 if (STRCMP(varname, "searchforward") == 0)
20786 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020787#ifdef FEAT_SEARCH_EXTRA
20788 else if (STRCMP(varname, "hlsearch") == 0)
20789 {
20790 no_hlsearch = !v->di_tv.vval.v_number;
20791 redraw_all_later(SOME_VALID);
20792 }
20793#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020794 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 return;
20796 }
20797
20798 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 }
20800 else /* add a new variable */
20801 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020802 /* Can't add "v:" variable. */
20803 if (ht == &vimvarht)
20804 {
20805 EMSG2(_(e_illvar), name);
20806 return;
20807 }
20808
Bram Moolenaar92124a32005-06-17 22:03:40 +000020809 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020810 if (!valid_varname(varname))
20811 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020812
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020813 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20814 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020815 if (v == NULL)
20816 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020817 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020818 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020820 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 return;
20822 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020823 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020825
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020826 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020827 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020828 else
20829 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020830 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020831 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834}
20835
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020836/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020837 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020838 * Also give an error message.
20839 */
20840 static int
20841var_check_ro(flags, name)
20842 int flags;
20843 char_u *name;
20844{
20845 if (flags & DI_FLAGS_RO)
20846 {
20847 EMSG2(_(e_readonlyvar), name);
20848 return TRUE;
20849 }
20850 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20851 {
20852 EMSG2(_(e_readonlysbx), name);
20853 return TRUE;
20854 }
20855 return FALSE;
20856}
20857
20858/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020859 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20860 * Also give an error message.
20861 */
20862 static int
20863var_check_fixed(flags, name)
20864 int flags;
20865 char_u *name;
20866{
20867 if (flags & DI_FLAGS_FIX)
20868 {
20869 EMSG2(_("E795: Cannot delete variable %s"), name);
20870 return TRUE;
20871 }
20872 return FALSE;
20873}
20874
20875/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020876 * Check if a funcref is assigned to a valid variable name.
20877 * Return TRUE and give an error if not.
20878 */
20879 static int
20880var_check_func_name(name, new_var)
20881 char_u *name; /* points to start of variable name */
20882 int new_var; /* TRUE when creating the variable */
20883{
20884 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20885 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20886 ? name[2] : name[0]))
20887 {
20888 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20889 name);
20890 return TRUE;
20891 }
20892 /* Don't allow hiding a function. When "v" is not NULL we might be
20893 * assigning another function to the same var, the type is checked
20894 * below. */
20895 if (new_var && function_exists(name))
20896 {
20897 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20898 name);
20899 return TRUE;
20900 }
20901 return FALSE;
20902}
20903
20904/*
20905 * Check if a variable name is valid.
20906 * Return FALSE and give an error if not.
20907 */
20908 static int
20909valid_varname(varname)
20910 char_u *varname;
20911{
20912 char_u *p;
20913
20914 for (p = varname; *p != NUL; ++p)
20915 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20916 && *p != AUTOLOAD_CHAR)
20917 {
20918 EMSG2(_(e_illvar), varname);
20919 return FALSE;
20920 }
20921 return TRUE;
20922}
20923
20924/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020925 * Return TRUE if typeval "tv" is set to be locked (immutable).
20926 * Also give an error message, using "name".
20927 */
20928 static int
20929tv_check_lock(lock, name)
20930 int lock;
20931 char_u *name;
20932{
20933 if (lock & VAR_LOCKED)
20934 {
20935 EMSG2(_("E741: Value is locked: %s"),
20936 name == NULL ? (char_u *)_("Unknown") : name);
20937 return TRUE;
20938 }
20939 if (lock & VAR_FIXED)
20940 {
20941 EMSG2(_("E742: Cannot change value of %s"),
20942 name == NULL ? (char_u *)_("Unknown") : name);
20943 return TRUE;
20944 }
20945 return FALSE;
20946}
20947
20948/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020950 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020951 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020952 * It is OK for "from" and "to" to point to the same item. This is used to
20953 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020954 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020955 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020956copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 typval_T *from;
20958 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020960 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020961 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020962 switch (from->v_type)
20963 {
20964 case VAR_NUMBER:
20965 to->vval.v_number = from->vval.v_number;
20966 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020967#ifdef FEAT_FLOAT
20968 case VAR_FLOAT:
20969 to->vval.v_float = from->vval.v_float;
20970 break;
20971#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020972 case VAR_STRING:
20973 case VAR_FUNC:
20974 if (from->vval.v_string == NULL)
20975 to->vval.v_string = NULL;
20976 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020977 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020978 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020979 if (from->v_type == VAR_FUNC)
20980 func_ref(to->vval.v_string);
20981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020982 break;
20983 case VAR_LIST:
20984 if (from->vval.v_list == NULL)
20985 to->vval.v_list = NULL;
20986 else
20987 {
20988 to->vval.v_list = from->vval.v_list;
20989 ++to->vval.v_list->lv_refcount;
20990 }
20991 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020992 case VAR_DICT:
20993 if (from->vval.v_dict == NULL)
20994 to->vval.v_dict = NULL;
20995 else
20996 {
20997 to->vval.v_dict = from->vval.v_dict;
20998 ++to->vval.v_dict->dv_refcount;
20999 }
21000 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021001 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021002 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021003 break;
21004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005}
21006
21007/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021008 * Make a copy of an item.
21009 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021010 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21011 * reference to an already copied list/dict can be used.
21012 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021013 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021014 static int
21015item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021016 typval_T *from;
21017 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021019 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021020{
21021 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021022 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021023
Bram Moolenaar33570922005-01-25 22:26:29 +000021024 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021025 {
21026 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021027 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021028 }
21029 ++recurse;
21030
21031 switch (from->v_type)
21032 {
21033 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021034#ifdef FEAT_FLOAT
21035 case VAR_FLOAT:
21036#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021037 case VAR_STRING:
21038 case VAR_FUNC:
21039 copy_tv(from, to);
21040 break;
21041 case VAR_LIST:
21042 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021043 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021044 if (from->vval.v_list == NULL)
21045 to->vval.v_list = NULL;
21046 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21047 {
21048 /* use the copy made earlier */
21049 to->vval.v_list = from->vval.v_list->lv_copylist;
21050 ++to->vval.v_list->lv_refcount;
21051 }
21052 else
21053 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21054 if (to->vval.v_list == NULL)
21055 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021056 break;
21057 case VAR_DICT:
21058 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021059 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021060 if (from->vval.v_dict == NULL)
21061 to->vval.v_dict = NULL;
21062 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21063 {
21064 /* use the copy made earlier */
21065 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21066 ++to->vval.v_dict->dv_refcount;
21067 }
21068 else
21069 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21070 if (to->vval.v_dict == NULL)
21071 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021072 break;
21073 default:
21074 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021075 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021076 }
21077 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021078 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021079}
21080
21081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 * ":echo expr1 ..." print each argument separated with a space, add a
21083 * newline at the end.
21084 * ":echon expr1 ..." print each argument plain.
21085 */
21086 void
21087ex_echo(eap)
21088 exarg_T *eap;
21089{
21090 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021092 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 char_u *p;
21094 int needclr = TRUE;
21095 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021096 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097
21098 if (eap->skip)
21099 ++emsg_skip;
21100 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21101 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021102 /* If eval1() causes an error message the text from the command may
21103 * still need to be cleared. E.g., "echo 22,44". */
21104 need_clr_eos = needclr;
21105
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021107 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 {
21109 /*
21110 * Report the invalid expression unless the expression evaluation
21111 * has been cancelled due to an aborting error, an interrupt, or an
21112 * exception.
21113 */
21114 if (!aborting())
21115 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021116 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 break;
21118 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021119 need_clr_eos = FALSE;
21120
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 if (!eap->skip)
21122 {
21123 if (atstart)
21124 {
21125 atstart = FALSE;
21126 /* Call msg_start() after eval1(), evaluating the expression
21127 * may cause a message to appear. */
21128 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021129 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021130 /* Mark the saved text as finishing the line, so that what
21131 * follows is displayed on a new line when scrolling back
21132 * at the more prompt. */
21133 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 }
21137 else if (eap->cmdidx == CMD_echo)
21138 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021139 current_copyID += COPYID_INC;
21140 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021141 if (p != NULL)
21142 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021144 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021146 if (*p != TAB && needclr)
21147 {
21148 /* remove any text still there from the command */
21149 msg_clr_eos();
21150 needclr = FALSE;
21151 }
21152 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 }
21154 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021155 {
21156#ifdef FEAT_MBYTE
21157 if (has_mbyte)
21158 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021159 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021160
21161 (void)msg_outtrans_len_attr(p, i, echo_attr);
21162 p += i - 1;
21163 }
21164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021166 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021169 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 arg = skipwhite(arg);
21173 }
21174 eap->nextcmd = check_nextcmd(arg);
21175
21176 if (eap->skip)
21177 --emsg_skip;
21178 else
21179 {
21180 /* remove text that may still be there from the command */
21181 if (needclr)
21182 msg_clr_eos();
21183 if (eap->cmdidx == CMD_echo)
21184 msg_end();
21185 }
21186}
21187
21188/*
21189 * ":echohl {name}".
21190 */
21191 void
21192ex_echohl(eap)
21193 exarg_T *eap;
21194{
21195 int id;
21196
21197 id = syn_name2id(eap->arg);
21198 if (id == 0)
21199 echo_attr = 0;
21200 else
21201 echo_attr = syn_id2attr(id);
21202}
21203
21204/*
21205 * ":execute expr1 ..." execute the result of an expression.
21206 * ":echomsg expr1 ..." Print a message
21207 * ":echoerr expr1 ..." Print an error
21208 * Each gets spaces around each argument and a newline at the end for
21209 * echo commands
21210 */
21211 void
21212ex_execute(eap)
21213 exarg_T *eap;
21214{
21215 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021216 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217 int ret = OK;
21218 char_u *p;
21219 garray_T ga;
21220 int len;
21221 int save_did_emsg;
21222
21223 ga_init2(&ga, 1, 80);
21224
21225 if (eap->skip)
21226 ++emsg_skip;
21227 while (*arg != NUL && *arg != '|' && *arg != '\n')
21228 {
21229 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021230 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 {
21232 /*
21233 * Report the invalid expression unless the expression evaluation
21234 * has been cancelled due to an aborting error, an interrupt, or an
21235 * exception.
21236 */
21237 if (!aborting())
21238 EMSG2(_(e_invexpr2), p);
21239 ret = FAIL;
21240 break;
21241 }
21242
21243 if (!eap->skip)
21244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246 len = (int)STRLEN(p);
21247 if (ga_grow(&ga, len + 2) == FAIL)
21248 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021249 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 ret = FAIL;
21251 break;
21252 }
21253 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 ga.ga_len += len;
21257 }
21258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 arg = skipwhite(arg);
21261 }
21262
21263 if (ret != FAIL && ga.ga_data != NULL)
21264 {
21265 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021268 out_flush();
21269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 else if (eap->cmdidx == CMD_echoerr)
21271 {
21272 /* We don't want to abort following commands, restore did_emsg. */
21273 save_did_emsg = did_emsg;
21274 EMSG((char_u *)ga.ga_data);
21275 if (!force_abort)
21276 did_emsg = save_did_emsg;
21277 }
21278 else if (eap->cmdidx == CMD_execute)
21279 do_cmdline((char_u *)ga.ga_data,
21280 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21281 }
21282
21283 ga_clear(&ga);
21284
21285 if (eap->skip)
21286 --emsg_skip;
21287
21288 eap->nextcmd = check_nextcmd(arg);
21289}
21290
21291/*
21292 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21293 * "arg" points to the "&" or '+' when called, to "option" when returning.
21294 * Returns NULL when no option name found. Otherwise pointer to the char
21295 * after the option name.
21296 */
21297 static char_u *
21298find_option_end(arg, opt_flags)
21299 char_u **arg;
21300 int *opt_flags;
21301{
21302 char_u *p = *arg;
21303
21304 ++p;
21305 if (*p == 'g' && p[1] == ':')
21306 {
21307 *opt_flags = OPT_GLOBAL;
21308 p += 2;
21309 }
21310 else if (*p == 'l' && p[1] == ':')
21311 {
21312 *opt_flags = OPT_LOCAL;
21313 p += 2;
21314 }
21315 else
21316 *opt_flags = 0;
21317
21318 if (!ASCII_ISALPHA(*p))
21319 return NULL;
21320 *arg = p;
21321
21322 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21323 p += 4; /* termcap option */
21324 else
21325 while (ASCII_ISALPHA(*p))
21326 ++p;
21327 return p;
21328}
21329
21330/*
21331 * ":function"
21332 */
21333 void
21334ex_function(eap)
21335 exarg_T *eap;
21336{
21337 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021338 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 int j;
21340 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021342 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 char_u *name = NULL;
21344 char_u *p;
21345 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021346 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 garray_T newargs;
21348 garray_T newlines;
21349 int varargs = FALSE;
21350 int mustend = FALSE;
21351 int flags = 0;
21352 ufunc_T *fp;
21353 int indent;
21354 int nesting;
21355 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021356 dictitem_T *v;
21357 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021358 static int func_nr = 0; /* number for nameless function */
21359 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021360 hashtab_T *ht;
21361 int todo;
21362 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021363 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364
21365 /*
21366 * ":function" without argument: list functions.
21367 */
21368 if (ends_excmd(*eap->arg))
21369 {
21370 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021371 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021372 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021373 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021374 {
21375 if (!HASHITEM_EMPTY(hi))
21376 {
21377 --todo;
21378 fp = HI2UF(hi);
21379 if (!isdigit(*fp->uf_name))
21380 list_func_head(fp, FALSE);
21381 }
21382 }
21383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 eap->nextcmd = check_nextcmd(eap->arg);
21385 return;
21386 }
21387
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021388 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021389 * ":function /pat": list functions matching pattern.
21390 */
21391 if (*eap->arg == '/')
21392 {
21393 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21394 if (!eap->skip)
21395 {
21396 regmatch_T regmatch;
21397
21398 c = *p;
21399 *p = NUL;
21400 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21401 *p = c;
21402 if (regmatch.regprog != NULL)
21403 {
21404 regmatch.rm_ic = p_ic;
21405
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021406 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021407 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21408 {
21409 if (!HASHITEM_EMPTY(hi))
21410 {
21411 --todo;
21412 fp = HI2UF(hi);
21413 if (!isdigit(*fp->uf_name)
21414 && vim_regexec(&regmatch, fp->uf_name, 0))
21415 list_func_head(fp, FALSE);
21416 }
21417 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021418 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021419 }
21420 }
21421 if (*p == '/')
21422 ++p;
21423 eap->nextcmd = check_nextcmd(p);
21424 return;
21425 }
21426
21427 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021428 * Get the function name. There are these situations:
21429 * func normal function name
21430 * "name" == func, "fudi.fd_dict" == NULL
21431 * dict.func new dictionary entry
21432 * "name" == NULL, "fudi.fd_dict" set,
21433 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21434 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021435 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021436 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21437 * dict.func existing dict entry that's not a Funcref
21438 * "name" == NULL, "fudi.fd_dict" set,
21439 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021442 name = trans_function_name(&p, eap->skip, 0, &fudi);
21443 paren = (vim_strchr(p, '(') != NULL);
21444 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 {
21446 /*
21447 * Return on an invalid expression in braces, unless the expression
21448 * evaluation has been cancelled due to an aborting error, an
21449 * interrupt, or an exception.
21450 */
21451 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021452 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021453 if (!eap->skip && fudi.fd_newkey != NULL)
21454 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021455 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 else
21459 eap->skip = TRUE;
21460 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021461
Bram Moolenaar071d4272004-06-13 20:20:40 +000021462 /* An error in a function call during evaluation of an expression in magic
21463 * braces should not cause the function not to be defined. */
21464 saved_did_emsg = did_emsg;
21465 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466
21467 /*
21468 * ":function func" with only function name: list function.
21469 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021470 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 {
21472 if (!ends_excmd(*skipwhite(p)))
21473 {
21474 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021475 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 }
21477 eap->nextcmd = check_nextcmd(p);
21478 if (eap->nextcmd != NULL)
21479 *p = NUL;
21480 if (!eap->skip && !got_int)
21481 {
21482 fp = find_func(name);
21483 if (fp != NULL)
21484 {
21485 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021486 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021488 if (FUNCLINE(fp, j) == NULL)
21489 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 msg_putchar('\n');
21491 msg_outnum((long)(j + 1));
21492 if (j < 9)
21493 msg_putchar(' ');
21494 if (j < 99)
21495 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021496 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 out_flush(); /* show a line at a time */
21498 ui_breakcheck();
21499 }
21500 if (!got_int)
21501 {
21502 msg_putchar('\n');
21503 msg_puts((char_u *)" endfunction");
21504 }
21505 }
21506 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021507 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021509 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 }
21511
21512 /*
21513 * ":function name(arg1, arg2)" Define function.
21514 */
21515 p = skipwhite(p);
21516 if (*p != '(')
21517 {
21518 if (!eap->skip)
21519 {
21520 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021521 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 }
21523 /* attempt to continue by skipping some text */
21524 if (vim_strchr(p, '(') != NULL)
21525 p = vim_strchr(p, '(');
21526 }
21527 p = skipwhite(p + 1);
21528
21529 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21530 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21531
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021532 if (!eap->skip)
21533 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021534 /* Check the name of the function. Unless it's a dictionary function
21535 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021536 if (name != NULL)
21537 arg = name;
21538 else
21539 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021540 if (arg != NULL && (fudi.fd_di == NULL
21541 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021542 {
21543 if (*arg == K_SPECIAL)
21544 j = 3;
21545 else
21546 j = 0;
21547 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21548 : eval_isnamec(arg[j])))
21549 ++j;
21550 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021551 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021552 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021553 /* Disallow using the g: dict. */
21554 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21555 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021556 }
21557
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 /*
21559 * Isolate the arguments: "arg1, arg2, ...)"
21560 */
21561 while (*p != ')')
21562 {
21563 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21564 {
21565 varargs = TRUE;
21566 p += 3;
21567 mustend = TRUE;
21568 }
21569 else
21570 {
21571 arg = p;
21572 while (ASCII_ISALNUM(*p) || *p == '_')
21573 ++p;
21574 if (arg == p || isdigit(*arg)
21575 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21576 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21577 {
21578 if (!eap->skip)
21579 EMSG2(_("E125: Illegal argument: %s"), arg);
21580 break;
21581 }
21582 if (ga_grow(&newargs, 1) == FAIL)
21583 goto erret;
21584 c = *p;
21585 *p = NUL;
21586 arg = vim_strsave(arg);
21587 if (arg == NULL)
21588 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021589
21590 /* Check for duplicate argument name. */
21591 for (i = 0; i < newargs.ga_len; ++i)
21592 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21593 {
21594 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021595 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021596 goto erret;
21597 }
21598
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21600 *p = c;
21601 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 if (*p == ',')
21603 ++p;
21604 else
21605 mustend = TRUE;
21606 }
21607 p = skipwhite(p);
21608 if (mustend && *p != ')')
21609 {
21610 if (!eap->skip)
21611 EMSG2(_(e_invarg2), eap->arg);
21612 break;
21613 }
21614 }
21615 ++p; /* skip the ')' */
21616
Bram Moolenaare9a41262005-01-15 22:18:47 +000021617 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 for (;;)
21619 {
21620 p = skipwhite(p);
21621 if (STRNCMP(p, "range", 5) == 0)
21622 {
21623 flags |= FC_RANGE;
21624 p += 5;
21625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021626 else if (STRNCMP(p, "dict", 4) == 0)
21627 {
21628 flags |= FC_DICT;
21629 p += 4;
21630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 else if (STRNCMP(p, "abort", 5) == 0)
21632 {
21633 flags |= FC_ABORT;
21634 p += 5;
21635 }
21636 else
21637 break;
21638 }
21639
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021640 /* When there is a line break use what follows for the function body.
21641 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21642 if (*p == '\n')
21643 line_arg = p + 1;
21644 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 EMSG(_(e_trailing));
21646
21647 /*
21648 * Read the body of the function, until ":endfunction" is found.
21649 */
21650 if (KeyTyped)
21651 {
21652 /* Check if the function already exists, don't let the user type the
21653 * whole function before telling him it doesn't work! For a script we
21654 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655 if (!eap->skip && !eap->forceit)
21656 {
21657 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21658 EMSG(_(e_funcdict));
21659 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021660 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021663 if (!eap->skip && did_emsg)
21664 goto erret;
21665
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 msg_putchar('\n'); /* don't overwrite the function name */
21667 cmdline_row = msg_row;
21668 }
21669
21670 indent = 2;
21671 nesting = 0;
21672 for (;;)
21673 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021674 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021675 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021676 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021677 saved_wait_return = FALSE;
21678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021680 sourcing_lnum_off = sourcing_lnum;
21681
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021682 if (line_arg != NULL)
21683 {
21684 /* Use eap->arg, split up in parts by line breaks. */
21685 theline = line_arg;
21686 p = vim_strchr(theline, '\n');
21687 if (p == NULL)
21688 line_arg += STRLEN(line_arg);
21689 else
21690 {
21691 *p = NUL;
21692 line_arg = p + 1;
21693 }
21694 }
21695 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 theline = getcmdline(':', 0L, indent);
21697 else
21698 theline = eap->getline(':', eap->cookie, indent);
21699 if (KeyTyped)
21700 lines_left = Rows - 1;
21701 if (theline == NULL)
21702 {
21703 EMSG(_("E126: Missing :endfunction"));
21704 goto erret;
21705 }
21706
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021707 /* Detect line continuation: sourcing_lnum increased more than one. */
21708 if (sourcing_lnum > sourcing_lnum_off + 1)
21709 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21710 else
21711 sourcing_lnum_off = 0;
21712
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 if (skip_until != NULL)
21714 {
21715 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21716 * don't check for ":endfunc". */
21717 if (STRCMP(theline, skip_until) == 0)
21718 {
21719 vim_free(skip_until);
21720 skip_until = NULL;
21721 }
21722 }
21723 else
21724 {
21725 /* skip ':' and blanks*/
21726 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21727 ;
21728
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021729 /* Check for "endfunction". */
21730 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021732 if (line_arg == NULL)
21733 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 break;
21735 }
21736
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021737 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 * at "end". */
21739 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21740 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021741 else if (STRNCMP(p, "if", 2) == 0
21742 || STRNCMP(p, "wh", 2) == 0
21743 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 || STRNCMP(p, "try", 3) == 0)
21745 indent += 2;
21746
21747 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021748 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021750 if (*p == '!')
21751 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 p += eval_fname_script(p);
21753 if (ASCII_ISALPHA(*p))
21754 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021755 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 if (*skipwhite(p) == '(')
21757 {
21758 ++nesting;
21759 indent += 2;
21760 }
21761 }
21762 }
21763
21764 /* Check for ":append" or ":insert". */
21765 p = skip_range(p, NULL);
21766 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21767 || (p[0] == 'i'
21768 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21769 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21770 skip_until = vim_strsave((char_u *)".");
21771
21772 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21773 arg = skipwhite(skiptowhite(p));
21774 if (arg[0] == '<' && arg[1] =='<'
21775 && ((p[0] == 'p' && p[1] == 'y'
21776 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21777 || (p[0] == 'p' && p[1] == 'e'
21778 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21779 || (p[0] == 't' && p[1] == 'c'
21780 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021781 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21782 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21784 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021785 || (p[0] == 'm' && p[1] == 'z'
21786 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787 ))
21788 {
21789 /* ":python <<" continues until a dot, like ":append" */
21790 p = skipwhite(arg + 2);
21791 if (*p == NUL)
21792 skip_until = vim_strsave((char_u *)".");
21793 else
21794 skip_until = vim_strsave(p);
21795 }
21796 }
21797
21798 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021799 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021800 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021801 if (line_arg == NULL)
21802 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021804 }
21805
21806 /* Copy the line to newly allocated memory. get_one_sourceline()
21807 * allocates 250 bytes per line, this saves 80% on average. The cost
21808 * is an extra alloc/free. */
21809 p = vim_strsave(theline);
21810 if (p != NULL)
21811 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021812 if (line_arg == NULL)
21813 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021814 theline = p;
21815 }
21816
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021817 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21818
21819 /* Add NULL lines for continuation lines, so that the line count is
21820 * equal to the index in the growarray. */
21821 while (sourcing_lnum_off-- > 0)
21822 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021823
21824 /* Check for end of eap->arg. */
21825 if (line_arg != NULL && *line_arg == NUL)
21826 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 }
21828
21829 /* Don't define the function when skipping commands or when an error was
21830 * detected. */
21831 if (eap->skip || did_emsg)
21832 goto erret;
21833
21834 /*
21835 * If there are no errors, add the function
21836 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021838 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021839 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021841 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021842 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021843 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021844 goto erret;
21845 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021846
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 fp = find_func(name);
21848 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021850 if (!eap->forceit)
21851 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021852 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021853 goto erret;
21854 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021855 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021856 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021857 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021858 name);
21859 goto erret;
21860 }
21861 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 ga_clear_strings(&(fp->uf_args));
21863 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021864 vim_free(name);
21865 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 }
21868 else
21869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021870 char numbuf[20];
21871
21872 fp = NULL;
21873 if (fudi.fd_newkey == NULL && !eap->forceit)
21874 {
21875 EMSG(_(e_funcdict));
21876 goto erret;
21877 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021878 if (fudi.fd_di == NULL)
21879 {
21880 /* Can't add a function to a locked dictionary */
21881 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21882 goto erret;
21883 }
21884 /* Can't change an existing function if it is locked */
21885 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21886 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021887
21888 /* Give the function a sequential number. Can only be used with a
21889 * Funcref! */
21890 vim_free(name);
21891 sprintf(numbuf, "%d", ++func_nr);
21892 name = vim_strsave((char_u *)numbuf);
21893 if (name == NULL)
21894 goto erret;
21895 }
21896
21897 if (fp == NULL)
21898 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021899 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021900 {
21901 int slen, plen;
21902 char_u *scriptname;
21903
21904 /* Check that the autoload name matches the script name. */
21905 j = FAIL;
21906 if (sourcing_name != NULL)
21907 {
21908 scriptname = autoload_name(name);
21909 if (scriptname != NULL)
21910 {
21911 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021912 plen = (int)STRLEN(p);
21913 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021914 if (slen > plen && fnamecmp(p,
21915 sourcing_name + slen - plen) == 0)
21916 j = OK;
21917 vim_free(scriptname);
21918 }
21919 }
21920 if (j == FAIL)
21921 {
21922 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21923 goto erret;
21924 }
21925 }
21926
21927 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 if (fp == NULL)
21929 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021930
21931 if (fudi.fd_dict != NULL)
21932 {
21933 if (fudi.fd_di == NULL)
21934 {
21935 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021936 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021937 if (fudi.fd_di == NULL)
21938 {
21939 vim_free(fp);
21940 goto erret;
21941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021942 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21943 {
21944 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021945 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021946 goto erret;
21947 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021948 }
21949 else
21950 /* overwrite existing dict entry */
21951 clear_tv(&fudi.fd_di->di_tv);
21952 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021953 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021954 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021955 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021956
21957 /* behave like "dict" was used */
21958 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021959 }
21960
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021962 STRCPY(fp->uf_name, name);
21963 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021965 fp->uf_args = newargs;
21966 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021967#ifdef FEAT_PROFILE
21968 fp->uf_tml_count = NULL;
21969 fp->uf_tml_total = NULL;
21970 fp->uf_tml_self = NULL;
21971 fp->uf_profiling = FALSE;
21972 if (prof_def_func())
21973 func_do_profile(fp);
21974#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021975 fp->uf_varargs = varargs;
21976 fp->uf_flags = flags;
21977 fp->uf_calls = 0;
21978 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021979 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980
21981erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 ga_clear_strings(&newargs);
21983 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021984ret_free:
21985 vim_free(skip_until);
21986 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021989 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990}
21991
21992/*
21993 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021994 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021996 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021997 * TFN_INT: internal function name OK
21998 * TFN_QUIET: be quiet
21999 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 * Advances "pp" to just after the function name (if no error).
22001 */
22002 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022003trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 char_u **pp;
22005 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022006 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022009 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 char_u *start;
22011 char_u *end;
22012 int lead;
22013 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022015 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022016
22017 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022018 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022020
22021 /* Check for hard coded <SNR>: already translated function ID (from a user
22022 * command). */
22023 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22024 && (*pp)[2] == (int)KE_SNR)
22025 {
22026 *pp += 3;
22027 len = get_id_len(pp) + 3;
22028 return vim_strnsave(start, len);
22029 }
22030
22031 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22032 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022034 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022036
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022037 /* Note that TFN_ flags use the same values as GLV_ flags. */
22038 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022039 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022040 if (end == start)
22041 {
22042 if (!skip)
22043 EMSG(_("E129: Function name required"));
22044 goto theend;
22045 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022046 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022047 {
22048 /*
22049 * Report an invalid expression in braces, unless the expression
22050 * evaluation has been cancelled due to an aborting error, an
22051 * interrupt, or an exception.
22052 */
22053 if (!aborting())
22054 {
22055 if (end != NULL)
22056 EMSG2(_(e_invarg2), start);
22057 }
22058 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022059 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022060 goto theend;
22061 }
22062
22063 if (lv.ll_tv != NULL)
22064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022065 if (fdp != NULL)
22066 {
22067 fdp->fd_dict = lv.ll_dict;
22068 fdp->fd_newkey = lv.ll_newkey;
22069 lv.ll_newkey = NULL;
22070 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022071 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022072 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22073 {
22074 name = vim_strsave(lv.ll_tv->vval.v_string);
22075 *pp = end;
22076 }
22077 else
22078 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022079 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22080 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022081 EMSG(_(e_funcref));
22082 else
22083 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022084 name = NULL;
22085 }
22086 goto theend;
22087 }
22088
22089 if (lv.ll_name == NULL)
22090 {
22091 /* Error found, but continue after the function name. */
22092 *pp = end;
22093 goto theend;
22094 }
22095
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022096 /* Check if the name is a Funcref. If so, use the value. */
22097 if (lv.ll_exp_name != NULL)
22098 {
22099 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022100 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022101 if (name == lv.ll_exp_name)
22102 name = NULL;
22103 }
22104 else
22105 {
22106 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022107 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022108 if (name == *pp)
22109 name = NULL;
22110 }
22111 if (name != NULL)
22112 {
22113 name = vim_strsave(name);
22114 *pp = end;
22115 goto theend;
22116 }
22117
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022118 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022119 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022120 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022121 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22122 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22123 {
22124 /* When there was "s:" already or the name expanded to get a
22125 * leading "s:" then remove it. */
22126 lv.ll_name += 2;
22127 len -= 2;
22128 lead = 2;
22129 }
22130 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022131 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022132 {
22133 if (lead == 2) /* skip over "s:" */
22134 lv.ll_name += 2;
22135 len = (int)(end - lv.ll_name);
22136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022137
22138 /*
22139 * Copy the function name to allocated memory.
22140 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22141 * Accept <SNR>123_name() outside a script.
22142 */
22143 if (skip)
22144 lead = 0; /* do nothing */
22145 else if (lead > 0)
22146 {
22147 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022148 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22149 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022150 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022151 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022152 if (current_SID <= 0)
22153 {
22154 EMSG(_(e_usingsid));
22155 goto theend;
22156 }
22157 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22158 lead += (int)STRLEN(sid_buf);
22159 }
22160 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022161 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022162 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022163 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022164 goto theend;
22165 }
22166 name = alloc((unsigned)(len + lead + 1));
22167 if (name != NULL)
22168 {
22169 if (lead > 0)
22170 {
22171 name[0] = K_SPECIAL;
22172 name[1] = KS_EXTRA;
22173 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022174 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022175 STRCPY(name + 3, sid_buf);
22176 }
22177 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22178 name[len + lead] = NUL;
22179 }
22180 *pp = end;
22181
22182theend:
22183 clear_lval(&lv);
22184 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185}
22186
22187/*
22188 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22189 * Return 2 if "p" starts with "s:".
22190 * Return 0 otherwise.
22191 */
22192 static int
22193eval_fname_script(p)
22194 char_u *p;
22195{
22196 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22197 || STRNICMP(p + 1, "SNR>", 4) == 0))
22198 return 5;
22199 if (p[0] == 's' && p[1] == ':')
22200 return 2;
22201 return 0;
22202}
22203
22204/*
22205 * Return TRUE if "p" starts with "<SID>" or "s:".
22206 * Only works if eval_fname_script() returned non-zero for "p"!
22207 */
22208 static int
22209eval_fname_sid(p)
22210 char_u *p;
22211{
22212 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22213}
22214
22215/*
22216 * List the head of the function: "name(arg1, arg2)".
22217 */
22218 static void
22219list_func_head(fp, indent)
22220 ufunc_T *fp;
22221 int indent;
22222{
22223 int j;
22224
22225 msg_start();
22226 if (indent)
22227 MSG_PUTS(" ");
22228 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022229 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 {
22231 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022232 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 }
22234 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022237 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 {
22239 if (j)
22240 MSG_PUTS(", ");
22241 msg_puts(FUNCARG(fp, j));
22242 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022243 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 {
22245 if (j)
22246 MSG_PUTS(", ");
22247 MSG_PUTS("...");
22248 }
22249 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022250 if (fp->uf_flags & FC_ABORT)
22251 MSG_PUTS(" abort");
22252 if (fp->uf_flags & FC_RANGE)
22253 MSG_PUTS(" range");
22254 if (fp->uf_flags & FC_DICT)
22255 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022256 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022257 if (p_verbose > 0)
22258 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259}
22260
22261/*
22262 * Find a function by name, return pointer to it in ufuncs.
22263 * Return NULL for unknown function.
22264 */
22265 static ufunc_T *
22266find_func(name)
22267 char_u *name;
22268{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022269 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022271 hi = hash_find(&func_hashtab, name);
22272 if (!HASHITEM_EMPTY(hi))
22273 return HI2UF(hi);
22274 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275}
22276
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022277#if defined(EXITFREE) || defined(PROTO)
22278 void
22279free_all_functions()
22280{
22281 hashitem_T *hi;
22282
22283 /* Need to start all over every time, because func_free() may change the
22284 * hash table. */
22285 while (func_hashtab.ht_used > 0)
22286 for (hi = func_hashtab.ht_array; ; ++hi)
22287 if (!HASHITEM_EMPTY(hi))
22288 {
22289 func_free(HI2UF(hi));
22290 break;
22291 }
22292}
22293#endif
22294
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022295 int
22296translated_function_exists(name)
22297 char_u *name;
22298{
22299 if (builtin_function(name))
22300 return find_internal_func(name) >= 0;
22301 return find_func(name) != NULL;
22302}
22303
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022304/*
22305 * Return TRUE if a function "name" exists.
22306 */
22307 static int
22308function_exists(name)
22309 char_u *name;
22310{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022311 char_u *nm = name;
22312 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 int n = FALSE;
22314
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022315 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22316 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022317 nm = skipwhite(nm);
22318
22319 /* Only accept "funcname", "funcname ", "funcname (..." and
22320 * "funcname(...", not "funcname!...". */
22321 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022322 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022323 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 return n;
22325}
22326
Bram Moolenaara1544c02013-05-30 12:35:52 +020022327 char_u *
22328get_expanded_name(name, check)
22329 char_u *name;
22330 int check;
22331{
22332 char_u *nm = name;
22333 char_u *p;
22334
22335 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22336
22337 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022338 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022339 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022340
Bram Moolenaara1544c02013-05-30 12:35:52 +020022341 vim_free(p);
22342 return NULL;
22343}
22344
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022345/*
22346 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022347 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022348 */
22349 static int
22350builtin_function(name)
22351 char_u *name;
22352{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022353 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22354 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022355}
22356
Bram Moolenaar05159a02005-02-26 23:04:13 +000022357#if defined(FEAT_PROFILE) || defined(PROTO)
22358/*
22359 * Start profiling function "fp".
22360 */
22361 static void
22362func_do_profile(fp)
22363 ufunc_T *fp;
22364{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022365 int len = fp->uf_lines.ga_len;
22366
22367 if (len == 0)
22368 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022369 fp->uf_tm_count = 0;
22370 profile_zero(&fp->uf_tm_self);
22371 profile_zero(&fp->uf_tm_total);
22372 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022373 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022374 if (fp->uf_tml_total == NULL)
22375 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022376 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022377 if (fp->uf_tml_self == NULL)
22378 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022379 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022380 fp->uf_tml_idx = -1;
22381 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22382 || fp->uf_tml_self == NULL)
22383 return; /* out of memory */
22384
22385 fp->uf_profiling = TRUE;
22386}
22387
22388/*
22389 * Dump the profiling results for all functions in file "fd".
22390 */
22391 void
22392func_dump_profile(fd)
22393 FILE *fd;
22394{
22395 hashitem_T *hi;
22396 int todo;
22397 ufunc_T *fp;
22398 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022399 ufunc_T **sorttab;
22400 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022401
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022402 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022403 if (todo == 0)
22404 return; /* nothing to dump */
22405
Bram Moolenaar73830342005-02-28 22:48:19 +000022406 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22407
Bram Moolenaar05159a02005-02-26 23:04:13 +000022408 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22409 {
22410 if (!HASHITEM_EMPTY(hi))
22411 {
22412 --todo;
22413 fp = HI2UF(hi);
22414 if (fp->uf_profiling)
22415 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022416 if (sorttab != NULL)
22417 sorttab[st_len++] = fp;
22418
Bram Moolenaar05159a02005-02-26 23:04:13 +000022419 if (fp->uf_name[0] == K_SPECIAL)
22420 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22421 else
22422 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22423 if (fp->uf_tm_count == 1)
22424 fprintf(fd, "Called 1 time\n");
22425 else
22426 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22427 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22428 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22429 fprintf(fd, "\n");
22430 fprintf(fd, "count total (s) self (s)\n");
22431
22432 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22433 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022434 if (FUNCLINE(fp, i) == NULL)
22435 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022436 prof_func_line(fd, fp->uf_tml_count[i],
22437 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022438 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22439 }
22440 fprintf(fd, "\n");
22441 }
22442 }
22443 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022444
22445 if (sorttab != NULL && st_len > 0)
22446 {
22447 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22448 prof_total_cmp);
22449 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22450 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22451 prof_self_cmp);
22452 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22453 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022454
22455 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022456}
Bram Moolenaar73830342005-02-28 22:48:19 +000022457
22458 static void
22459prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22460 FILE *fd;
22461 ufunc_T **sorttab;
22462 int st_len;
22463 char *title;
22464 int prefer_self; /* when equal print only self time */
22465{
22466 int i;
22467 ufunc_T *fp;
22468
22469 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22470 fprintf(fd, "count total (s) self (s) function\n");
22471 for (i = 0; i < 20 && i < st_len; ++i)
22472 {
22473 fp = sorttab[i];
22474 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22475 prefer_self);
22476 if (fp->uf_name[0] == K_SPECIAL)
22477 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22478 else
22479 fprintf(fd, " %s()\n", fp->uf_name);
22480 }
22481 fprintf(fd, "\n");
22482}
22483
22484/*
22485 * Print the count and times for one function or function line.
22486 */
22487 static void
22488prof_func_line(fd, count, total, self, prefer_self)
22489 FILE *fd;
22490 int count;
22491 proftime_T *total;
22492 proftime_T *self;
22493 int prefer_self; /* when equal print only self time */
22494{
22495 if (count > 0)
22496 {
22497 fprintf(fd, "%5d ", count);
22498 if (prefer_self && profile_equal(total, self))
22499 fprintf(fd, " ");
22500 else
22501 fprintf(fd, "%s ", profile_msg(total));
22502 if (!prefer_self && profile_equal(total, self))
22503 fprintf(fd, " ");
22504 else
22505 fprintf(fd, "%s ", profile_msg(self));
22506 }
22507 else
22508 fprintf(fd, " ");
22509}
22510
22511/*
22512 * Compare function for total time sorting.
22513 */
22514 static int
22515#ifdef __BORLANDC__
22516_RTLENTRYF
22517#endif
22518prof_total_cmp(s1, s2)
22519 const void *s1;
22520 const void *s2;
22521{
22522 ufunc_T *p1, *p2;
22523
22524 p1 = *(ufunc_T **)s1;
22525 p2 = *(ufunc_T **)s2;
22526 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22527}
22528
22529/*
22530 * Compare function for self time sorting.
22531 */
22532 static int
22533#ifdef __BORLANDC__
22534_RTLENTRYF
22535#endif
22536prof_self_cmp(s1, s2)
22537 const void *s1;
22538 const void *s2;
22539{
22540 ufunc_T *p1, *p2;
22541
22542 p1 = *(ufunc_T **)s1;
22543 p2 = *(ufunc_T **)s2;
22544 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22545}
22546
Bram Moolenaar05159a02005-02-26 23:04:13 +000022547#endif
22548
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022549/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022550 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022551 * Return TRUE if a package was loaded.
22552 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022553 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022554script_autoload(name, reload)
22555 char_u *name;
22556 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022557{
22558 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022559 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022560 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022561 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022562
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022563 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022564 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022565 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022566 return FALSE;
22567
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022568 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022569
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022570 /* Find the name in the list of previously loaded package names. Skip
22571 * "autoload/", it's always the same. */
22572 for (i = 0; i < ga_loaded.ga_len; ++i)
22573 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22574 break;
22575 if (!reload && i < ga_loaded.ga_len)
22576 ret = FALSE; /* was loaded already */
22577 else
22578 {
22579 /* Remember the name if it wasn't loaded already. */
22580 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22581 {
22582 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22583 tofree = NULL;
22584 }
22585
22586 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022587 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022588 ret = TRUE;
22589 }
22590
22591 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022592 return ret;
22593}
22594
22595/*
22596 * Return the autoload script name for a function or variable name.
22597 * Returns NULL when out of memory.
22598 */
22599 static char_u *
22600autoload_name(name)
22601 char_u *name;
22602{
22603 char_u *p;
22604 char_u *scriptname;
22605
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022606 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022607 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22608 if (scriptname == NULL)
22609 return FALSE;
22610 STRCPY(scriptname, "autoload/");
22611 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022612 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022613 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022614 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022615 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022616 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022617}
22618
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22620
22621/*
22622 * Function given to ExpandGeneric() to obtain the list of user defined
22623 * function names.
22624 */
22625 char_u *
22626get_user_func_name(xp, idx)
22627 expand_T *xp;
22628 int idx;
22629{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022630 static long_u done;
22631 static hashitem_T *hi;
22632 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633
22634 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022636 done = 0;
22637 hi = func_hashtab.ht_array;
22638 }
22639 if (done < func_hashtab.ht_used)
22640 {
22641 if (done++ > 0)
22642 ++hi;
22643 while (HASHITEM_EMPTY(hi))
22644 ++hi;
22645 fp = HI2UF(hi);
22646
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022647 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022648 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022649
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022650 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22651 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652
22653 cat_func_name(IObuff, fp);
22654 if (xp->xp_context != EXPAND_USER_FUNC)
22655 {
22656 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022657 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 STRCAT(IObuff, ")");
22659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 return IObuff;
22661 }
22662 return NULL;
22663}
22664
22665#endif /* FEAT_CMDL_COMPL */
22666
22667/*
22668 * Copy the function name of "fp" to buffer "buf".
22669 * "buf" must be able to hold the function name plus three bytes.
22670 * Takes care of script-local function names.
22671 */
22672 static void
22673cat_func_name(buf, fp)
22674 char_u *buf;
22675 ufunc_T *fp;
22676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022677 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 {
22679 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022680 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 }
22682 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022683 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684}
22685
22686/*
22687 * ":delfunction {name}"
22688 */
22689 void
22690ex_delfunction(eap)
22691 exarg_T *eap;
22692{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022693 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 char_u *p;
22695 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
22698 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022699 name = trans_function_name(&p, eap->skip, 0, &fudi);
22700 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022702 {
22703 if (fudi.fd_dict != NULL && !eap->skip)
22704 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 if (!ends_excmd(*skipwhite(p)))
22708 {
22709 vim_free(name);
22710 EMSG(_(e_trailing));
22711 return;
22712 }
22713 eap->nextcmd = check_nextcmd(p);
22714 if (eap->nextcmd != NULL)
22715 *p = NUL;
22716
22717 if (!eap->skip)
22718 fp = find_func(name);
22719 vim_free(name);
22720
22721 if (!eap->skip)
22722 {
22723 if (fp == NULL)
22724 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022725 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 return;
22727 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022728 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 {
22730 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22731 return;
22732 }
22733
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022734 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022736 /* Delete the dict item that refers to the function, it will
22737 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022738 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 else
22741 func_free(fp);
22742 }
22743}
22744
22745/*
22746 * Free a function and remove it from the list of functions.
22747 */
22748 static void
22749func_free(fp)
22750 ufunc_T *fp;
22751{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022752 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022753
22754 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022755 ga_clear_strings(&(fp->uf_args));
22756 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022757#ifdef FEAT_PROFILE
22758 vim_free(fp->uf_tml_count);
22759 vim_free(fp->uf_tml_total);
22760 vim_free(fp->uf_tml_self);
22761#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022762
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022763 /* remove the function from the function hashtable */
22764 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22765 if (HASHITEM_EMPTY(hi))
22766 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022767 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022768 hash_remove(&func_hashtab, hi);
22769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022770 vim_free(fp);
22771}
22772
22773/*
22774 * Unreference a Function: decrement the reference count and free it when it
22775 * becomes zero. Only for numbered functions.
22776 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022777 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022778func_unref(name)
22779 char_u *name;
22780{
22781 ufunc_T *fp;
22782
22783 if (name != NULL && isdigit(*name))
22784 {
22785 fp = find_func(name);
22786 if (fp == NULL)
22787 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022788 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 {
22790 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 * when "uf_calls" becomes zero. */
22792 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022793 func_free(fp);
22794 }
22795 }
22796}
22797
22798/*
22799 * Count a reference to a Function.
22800 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022801 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022802func_ref(name)
22803 char_u *name;
22804{
22805 ufunc_T *fp;
22806
22807 if (name != NULL && isdigit(*name))
22808 {
22809 fp = find_func(name);
22810 if (fp == NULL)
22811 EMSG2(_(e_intern2), "func_ref()");
22812 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022813 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 }
22815}
22816
22817/*
22818 * Call a user function.
22819 */
22820 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022821call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 ufunc_T *fp; /* pointer to function */
22823 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 typval_T *argvars; /* arguments */
22825 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 linenr_T firstline; /* first line of range */
22827 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022828 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829{
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 char_u *save_sourcing_name;
22831 linenr_T save_sourcing_lnum;
22832 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022833 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 int save_did_emsg;
22835 static int depth = 0;
22836 dictitem_T *v;
22837 int fixvar_idx = 0; /* index in fixvar[] */
22838 int i;
22839 int ai;
22840 char_u numbuf[NUMBUFLEN];
22841 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022842#ifdef FEAT_PROFILE
22843 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022844 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846
22847 /* If depth of calling is getting too high, don't execute the function */
22848 if (depth >= p_mfd)
22849 {
22850 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022851 rettv->v_type = VAR_NUMBER;
22852 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 return;
22854 }
22855 ++depth;
22856
22857 line_breakcheck(); /* check for CTRL-C hit */
22858
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022859 fc = (funccall_T *)alloc(sizeof(funccall_T));
22860 fc->caller = current_funccal;
22861 current_funccal = fc;
22862 fc->func = fp;
22863 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022864 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022865 fc->linenr = 0;
22866 fc->returned = FALSE;
22867 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022869 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22870 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022873 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22875 * each argument variable and saves a lot of time.
22876 */
22877 /*
22878 * Init l: variables.
22879 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022880 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022881 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022882 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022883 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22884 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022885 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022886 name = v->di_key;
22887 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022889 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022891 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022892 v->di_tv.vval.v_dict = selfdict;
22893 ++selfdict->dv_refcount;
22894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022895
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 /*
22897 * Init a: variables.
22898 * Set a:0 to "argcount".
22899 * Set a:000 to a list with room for the "..." arguments.
22900 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022901 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022902 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022904 /* Use "name" to avoid a warning from some compiler that checks the
22905 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022906 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022907 name = v->di_key;
22908 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022910 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022911 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022912 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022913 v->di_tv.vval.v_list = &fc->l_varlist;
22914 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22915 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22916 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022917
22918 /*
22919 * Set a:firstline to "firstline" and a:lastline to "lastline".
22920 * Set a:name to named arguments.
22921 * Set a:N to the "..." arguments.
22922 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022923 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022924 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022926 (varnumber_T)lastline);
22927 for (i = 0; i < argcount; ++i)
22928 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022929 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022930 if (ai < 0)
22931 /* named argument a:name */
22932 name = FUNCARG(fp, i);
22933 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022934 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 /* "..." argument a:1, a:2, etc. */
22936 sprintf((char *)numbuf, "%d", ai + 1);
22937 name = numbuf;
22938 }
22939 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22940 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022941 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022942 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22943 }
22944 else
22945 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022946 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22947 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 if (v == NULL)
22949 break;
22950 v->di_flags = DI_FLAGS_RO;
22951 }
22952 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022953 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022954
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022955 /* Note: the values are copied directly to avoid alloc/free.
22956 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022957 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022958 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022959
22960 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22961 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022962 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22963 fc->l_listitems[ai].li_tv = argvars[i];
22964 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022965 }
22966 }
22967
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 /* Don't redraw while executing the function. */
22969 ++RedrawingDisabled;
22970 save_sourcing_name = sourcing_name;
22971 save_sourcing_lnum = sourcing_lnum;
22972 sourcing_lnum = 1;
22973 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022974 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 if (sourcing_name != NULL)
22976 {
22977 if (save_sourcing_name != NULL
22978 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22979 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22980 else
22981 STRCPY(sourcing_name, "function ");
22982 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22983
22984 if (p_verbose >= 12)
22985 {
22986 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022987 verbose_enter_scroll();
22988
Bram Moolenaar555b2802005-05-19 21:08:39 +000022989 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 if (p_verbose >= 14)
22991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022993 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022994 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022995 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996
22997 msg_puts((char_u *)"(");
22998 for (i = 0; i < argcount; ++i)
22999 {
23000 if (i > 0)
23001 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023002 if (argvars[i].v_type == VAR_NUMBER)
23003 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 else
23005 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023006 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23007 if (s != NULL)
23008 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023009 if (vim_strsize(s) > MSG_BUF_CLEN)
23010 {
23011 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23012 s = buf;
23013 }
23014 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023015 vim_free(tofree);
23016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 }
23018 }
23019 msg_puts((char_u *)")");
23020 }
23021 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023022
23023 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 --no_wait_return;
23025 }
23026 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023027#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023028 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023029 {
23030 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23031 func_do_profile(fp);
23032 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023033 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023034 {
23035 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023036 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023037 profile_zero(&fp->uf_tm_children);
23038 }
23039 script_prof_save(&wait_start);
23040 }
23041#endif
23042
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023044 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 save_did_emsg = did_emsg;
23046 did_emsg = FALSE;
23047
23048 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023049 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23051
23052 --RedrawingDisabled;
23053
23054 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023055 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023057 clear_tv(rettv);
23058 rettv->v_type = VAR_NUMBER;
23059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 }
23061
Bram Moolenaar05159a02005-02-26 23:04:13 +000023062#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023063 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023064 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023065 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023066 profile_end(&call_start);
23067 profile_sub_wait(&wait_start, &call_start);
23068 profile_add(&fp->uf_tm_total, &call_start);
23069 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023070 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023071 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023072 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23073 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023074 }
23075 }
23076#endif
23077
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 /* when being verbose, mention the return value */
23079 if (p_verbose >= 12)
23080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023082 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023085 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023086 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023087 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023088 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023091 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023092 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023093 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023094 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023095
Bram Moolenaar555b2802005-05-19 21:08:39 +000023096 /* The value may be very long. Skip the middle part, so that we
23097 * have some idea how it starts and ends. smsg() would always
23098 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023099 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023100 if (s != NULL)
23101 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023102 if (vim_strsize(s) > MSG_BUF_CLEN)
23103 {
23104 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23105 s = buf;
23106 }
23107 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023108 vim_free(tofree);
23109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 }
23111 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023112
23113 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 --no_wait_return;
23115 }
23116
23117 vim_free(sourcing_name);
23118 sourcing_name = save_sourcing_name;
23119 sourcing_lnum = save_sourcing_lnum;
23120 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023121#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023122 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023123 script_prof_restore(&wait_start);
23124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125
23126 if (p_verbose >= 12 && sourcing_name != NULL)
23127 {
23128 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023129 verbose_enter_scroll();
23130
Bram Moolenaar555b2802005-05-19 21:08:39 +000023131 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023133
23134 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 --no_wait_return;
23136 }
23137
23138 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023139 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023141
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023142 /* If the a:000 list and the l: and a: dicts are not referenced we can
23143 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023144 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23145 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23146 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23147 {
23148 free_funccal(fc, FALSE);
23149 }
23150 else
23151 {
23152 hashitem_T *hi;
23153 listitem_T *li;
23154 int todo;
23155
23156 /* "fc" is still in use. This can happen when returning "a:000" or
23157 * assigning "l:" to a global variable.
23158 * Link "fc" in the list for garbage collection later. */
23159 fc->caller = previous_funccal;
23160 previous_funccal = fc;
23161
23162 /* Make a copy of the a: variables, since we didn't do that above. */
23163 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23164 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23165 {
23166 if (!HASHITEM_EMPTY(hi))
23167 {
23168 --todo;
23169 v = HI2DI(hi);
23170 copy_tv(&v->di_tv, &v->di_tv);
23171 }
23172 }
23173
23174 /* Make a copy of the a:000 items, since we didn't do that above. */
23175 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23176 copy_tv(&li->li_tv, &li->li_tv);
23177 }
23178}
23179
23180/*
23181 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023182 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023183 */
23184 static int
23185can_free_funccal(fc, copyID)
23186 funccall_T *fc;
23187 int copyID;
23188{
23189 return (fc->l_varlist.lv_copyID != copyID
23190 && fc->l_vars.dv_copyID != copyID
23191 && fc->l_avars.dv_copyID != copyID);
23192}
23193
23194/*
23195 * Free "fc" and what it contains.
23196 */
23197 static void
23198free_funccal(fc, free_val)
23199 funccall_T *fc;
23200 int free_val; /* a: vars were allocated */
23201{
23202 listitem_T *li;
23203
23204 /* The a: variables typevals may not have been allocated, only free the
23205 * allocated variables. */
23206 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23207
23208 /* free all l: variables */
23209 vars_clear(&fc->l_vars.dv_hashtab);
23210
23211 /* Free the a:000 variables if they were allocated. */
23212 if (free_val)
23213 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23214 clear_tv(&li->li_tv);
23215
23216 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217}
23218
23219/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023220 * Add a number variable "name" to dict "dp" with value "nr".
23221 */
23222 static void
23223add_nr_var(dp, v, name, nr)
23224 dict_T *dp;
23225 dictitem_T *v;
23226 char *name;
23227 varnumber_T nr;
23228{
23229 STRCPY(v->di_key, name);
23230 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23231 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23232 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023233 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 v->di_tv.vval.v_number = nr;
23235}
23236
23237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 * ":return [expr]"
23239 */
23240 void
23241ex_return(eap)
23242 exarg_T *eap;
23243{
23244 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 int returning = FALSE;
23247
23248 if (current_funccal == NULL)
23249 {
23250 EMSG(_("E133: :return not inside a function"));
23251 return;
23252 }
23253
23254 if (eap->skip)
23255 ++emsg_skip;
23256
23257 eap->nextcmd = NULL;
23258 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023259 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 {
23261 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023262 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023264 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 }
23266 /* It's safer to return also on error. */
23267 else if (!eap->skip)
23268 {
23269 /*
23270 * Return unless the expression evaluation has been cancelled due to an
23271 * aborting error, an interrupt, or an exception.
23272 */
23273 if (!aborting())
23274 returning = do_return(eap, FALSE, TRUE, NULL);
23275 }
23276
23277 /* When skipping or the return gets pending, advance to the next command
23278 * in this line (!returning). Otherwise, ignore the rest of the line.
23279 * Following lines will be ignored by get_func_line(). */
23280 if (returning)
23281 eap->nextcmd = NULL;
23282 else if (eap->nextcmd == NULL) /* no argument */
23283 eap->nextcmd = check_nextcmd(arg);
23284
23285 if (eap->skip)
23286 --emsg_skip;
23287}
23288
23289/*
23290 * Return from a function. Possibly makes the return pending. Also called
23291 * for a pending return at the ":endtry" or after returning from an extra
23292 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023294 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 * FALSE when the return gets pending.
23296 */
23297 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023298do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 exarg_T *eap;
23300 int reanimate;
23301 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023302 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303{
23304 int idx;
23305 struct condstack *cstack = eap->cstack;
23306
23307 if (reanimate)
23308 /* Undo the return. */
23309 current_funccal->returned = FALSE;
23310
23311 /*
23312 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23313 * not in its finally clause (which then is to be executed next) is found.
23314 * In this case, make the ":return" pending for execution at the ":endtry".
23315 * Otherwise, return normally.
23316 */
23317 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23318 if (idx >= 0)
23319 {
23320 cstack->cs_pending[idx] = CSTP_RETURN;
23321
23322 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023323 /* A pending return again gets pending. "rettv" points to an
23324 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023326 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 else
23328 {
23329 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023330 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023332 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023334 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 {
23336 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023337 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 else
23340 EMSG(_(e_outofmem));
23341 }
23342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023343 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344
23345 if (reanimate)
23346 {
23347 /* The pending return value could be overwritten by a ":return"
23348 * without argument in a finally clause; reset the default
23349 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023350 current_funccal->rettv->v_type = VAR_NUMBER;
23351 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 }
23353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023354 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 }
23356 else
23357 {
23358 current_funccal->returned = TRUE;
23359
23360 /* If the return is carried out now, store the return value. For
23361 * a return immediately after reanimation, the value is already
23362 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023363 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023365 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023368 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 }
23370 }
23371
23372 return idx < 0;
23373}
23374
23375/*
23376 * Free the variable with a pending return value.
23377 */
23378 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023379discard_pending_return(rettv)
23380 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381{
Bram Moolenaar33570922005-01-25 22:26:29 +000023382 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383}
23384
23385/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023386 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 * is an allocated string. Used by report_pending() for verbose messages.
23388 */
23389 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023390get_return_cmd(rettv)
23391 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023393 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023394 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023395 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023397 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023398 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023399 if (s == NULL)
23400 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023401
23402 STRCPY(IObuff, ":return ");
23403 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23404 if (STRLEN(s) + 8 >= IOSIZE)
23405 STRCPY(IObuff + IOSIZE - 4, "...");
23406 vim_free(tofree);
23407 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408}
23409
23410/*
23411 * Get next function line.
23412 * Called by do_cmdline() to get the next line.
23413 * Returns allocated string, or NULL for end of function.
23414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 char_u *
23416get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023417 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023419 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420{
Bram Moolenaar33570922005-01-25 22:26:29 +000023421 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023422 ufunc_T *fp = fcp->func;
23423 char_u *retval;
23424 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425
23426 /* If breakpoints have been added/deleted need to check for it. */
23427 if (fcp->dbg_tick != debug_tick)
23428 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023429 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 sourcing_lnum);
23431 fcp->dbg_tick = debug_tick;
23432 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023433#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023434 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023435 func_line_end(cookie);
23436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437
Bram Moolenaar05159a02005-02-26 23:04:13 +000023438 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023439 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23440 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 retval = NULL;
23442 else
23443 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023444 /* Skip NULL lines (continuation lines). */
23445 while (fcp->linenr < gap->ga_len
23446 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23447 ++fcp->linenr;
23448 if (fcp->linenr >= gap->ga_len)
23449 retval = NULL;
23450 else
23451 {
23452 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23453 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023454#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023455 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023456 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023457#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 }
23460
23461 /* Did we encounter a breakpoint? */
23462 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23463 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023464 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023466 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 sourcing_lnum);
23468 fcp->dbg_tick = debug_tick;
23469 }
23470
23471 return retval;
23472}
23473
Bram Moolenaar05159a02005-02-26 23:04:13 +000023474#if defined(FEAT_PROFILE) || defined(PROTO)
23475/*
23476 * Called when starting to read a function line.
23477 * "sourcing_lnum" must be correct!
23478 * When skipping lines it may not actually be executed, but we won't find out
23479 * until later and we need to store the time now.
23480 */
23481 void
23482func_line_start(cookie)
23483 void *cookie;
23484{
23485 funccall_T *fcp = (funccall_T *)cookie;
23486 ufunc_T *fp = fcp->func;
23487
23488 if (fp->uf_profiling && sourcing_lnum >= 1
23489 && sourcing_lnum <= fp->uf_lines.ga_len)
23490 {
23491 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023492 /* Skip continuation lines. */
23493 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23494 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023495 fp->uf_tml_execed = FALSE;
23496 profile_start(&fp->uf_tml_start);
23497 profile_zero(&fp->uf_tml_children);
23498 profile_get_wait(&fp->uf_tml_wait);
23499 }
23500}
23501
23502/*
23503 * Called when actually executing a function line.
23504 */
23505 void
23506func_line_exec(cookie)
23507 void *cookie;
23508{
23509 funccall_T *fcp = (funccall_T *)cookie;
23510 ufunc_T *fp = fcp->func;
23511
23512 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23513 fp->uf_tml_execed = TRUE;
23514}
23515
23516/*
23517 * Called when done with a function line.
23518 */
23519 void
23520func_line_end(cookie)
23521 void *cookie;
23522{
23523 funccall_T *fcp = (funccall_T *)cookie;
23524 ufunc_T *fp = fcp->func;
23525
23526 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23527 {
23528 if (fp->uf_tml_execed)
23529 {
23530 ++fp->uf_tml_count[fp->uf_tml_idx];
23531 profile_end(&fp->uf_tml_start);
23532 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023533 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023534 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23535 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023536 }
23537 fp->uf_tml_idx = -1;
23538 }
23539}
23540#endif
23541
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542/*
23543 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023544 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 */
23546 int
23547func_has_ended(cookie)
23548 void *cookie;
23549{
Bram Moolenaar33570922005-01-25 22:26:29 +000023550 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551
23552 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23553 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023554 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 || fcp->returned);
23556}
23557
23558/*
23559 * return TRUE if cookie indicates a function which "abort"s on errors.
23560 */
23561 int
23562func_has_abort(cookie)
23563 void *cookie;
23564{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023565 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566}
23567
23568#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23569typedef enum
23570{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023571 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23572 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23573 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574} var_flavour_T;
23575
23576static var_flavour_T var_flavour __ARGS((char_u *varname));
23577
23578 static var_flavour_T
23579var_flavour(varname)
23580 char_u *varname;
23581{
23582 char_u *p = varname;
23583
23584 if (ASCII_ISUPPER(*p))
23585 {
23586 while (*(++p))
23587 if (ASCII_ISLOWER(*p))
23588 return VAR_FLAVOUR_SESSION;
23589 return VAR_FLAVOUR_VIMINFO;
23590 }
23591 else
23592 return VAR_FLAVOUR_DEFAULT;
23593}
23594#endif
23595
23596#if defined(FEAT_VIMINFO) || defined(PROTO)
23597/*
23598 * Restore global vars that start with a capital from the viminfo file
23599 */
23600 int
23601read_viminfo_varlist(virp, writing)
23602 vir_T *virp;
23603 int writing;
23604{
23605 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023606 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023607 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608
23609 if (!writing && (find_viminfo_parameter('!') != NULL))
23610 {
23611 tab = vim_strchr(virp->vir_line + 1, '\t');
23612 if (tab != NULL)
23613 {
23614 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023615 switch (*tab)
23616 {
23617 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023618#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023619 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023620#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023621 case 'D': type = VAR_DICT; break;
23622 case 'L': type = VAR_LIST; break;
23623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624
23625 tab = vim_strchr(tab, '\t');
23626 if (tab != NULL)
23627 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023628 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023629 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023630 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023632#ifdef FEAT_FLOAT
23633 else if (type == VAR_FLOAT)
23634 (void)string2float(tab + 1, &tv.vval.v_float);
23635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023637 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023638 if (type == VAR_DICT || type == VAR_LIST)
23639 {
23640 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23641
23642 if (etv == NULL)
23643 /* Failed to parse back the dict or list, use it as a
23644 * string. */
23645 tv.v_type = VAR_STRING;
23646 else
23647 {
23648 vim_free(tv.vval.v_string);
23649 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023650 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023651 }
23652 }
23653
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023654 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023655
23656 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023657 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023658 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23659 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 }
23661 }
23662 }
23663
23664 return viminfo_readline(virp);
23665}
23666
23667/*
23668 * Write global vars that start with a capital to the viminfo file
23669 */
23670 void
23671write_viminfo_varlist(fp)
23672 FILE *fp;
23673{
Bram Moolenaar33570922005-01-25 22:26:29 +000023674 hashitem_T *hi;
23675 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023676 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023677 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023678 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023679 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023680 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681
23682 if (find_viminfo_parameter('!') == NULL)
23683 return;
23684
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023685 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023686
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023687 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023688 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023690 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023692 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023693 this_var = HI2DI(hi);
23694 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023696 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023697 {
23698 case VAR_STRING: s = "STR"; break;
23699 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023700#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023701 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023702#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023703 case VAR_DICT: s = "DIC"; break;
23704 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023705 default: continue;
23706 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023707 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023708 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023709 if (p != NULL)
23710 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023711 vim_free(tofree);
23712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023713 }
23714 }
23715}
23716#endif
23717
23718#if defined(FEAT_SESSION) || defined(PROTO)
23719 int
23720store_session_globals(fd)
23721 FILE *fd;
23722{
Bram Moolenaar33570922005-01-25 22:26:29 +000023723 hashitem_T *hi;
23724 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023725 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 char_u *p, *t;
23727
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023728 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023729 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023731 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023733 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 this_var = HI2DI(hi);
23735 if ((this_var->di_tv.v_type == VAR_NUMBER
23736 || this_var->di_tv.v_type == VAR_STRING)
23737 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023739 /* Escape special characters with a backslash. Turn a LF and
23740 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023742 (char_u *)"\\\"\n\r");
23743 if (p == NULL) /* out of memory */
23744 break;
23745 for (t = p; *t != NUL; ++t)
23746 if (*t == '\n')
23747 *t = 'n';
23748 else if (*t == '\r')
23749 *t = 'r';
23750 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023751 this_var->di_key,
23752 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23753 : ' ',
23754 p,
23755 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23756 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023757 || put_eol(fd) == FAIL)
23758 {
23759 vim_free(p);
23760 return FAIL;
23761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 vim_free(p);
23763 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023764#ifdef FEAT_FLOAT
23765 else if (this_var->di_tv.v_type == VAR_FLOAT
23766 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23767 {
23768 float_T f = this_var->di_tv.vval.v_float;
23769 int sign = ' ';
23770
23771 if (f < 0)
23772 {
23773 f = -f;
23774 sign = '-';
23775 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023776 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023777 this_var->di_key, sign, f) < 0)
23778 || put_eol(fd) == FAIL)
23779 return FAIL;
23780 }
23781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 }
23783 }
23784 return OK;
23785}
23786#endif
23787
Bram Moolenaar661b1822005-07-28 22:36:45 +000023788/*
23789 * Display script name where an item was last set.
23790 * Should only be invoked when 'verbose' is non-zero.
23791 */
23792 void
23793last_set_msg(scriptID)
23794 scid_T scriptID;
23795{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023796 char_u *p;
23797
Bram Moolenaar661b1822005-07-28 22:36:45 +000023798 if (scriptID != 0)
23799 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023800 p = home_replace_save(NULL, get_scriptname(scriptID));
23801 if (p != NULL)
23802 {
23803 verbose_enter();
23804 MSG_PUTS(_("\n\tLast set from "));
23805 MSG_PUTS(p);
23806 vim_free(p);
23807 verbose_leave();
23808 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023809 }
23810}
23811
Bram Moolenaard812df62008-11-09 12:46:09 +000023812/*
23813 * List v:oldfiles in a nice way.
23814 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023815 void
23816ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023817 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023818{
23819 list_T *l = vimvars[VV_OLDFILES].vv_list;
23820 listitem_T *li;
23821 int nr = 0;
23822
23823 if (l == NULL)
23824 msg((char_u *)_("No old files"));
23825 else
23826 {
23827 msg_start();
23828 msg_scroll = TRUE;
23829 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23830 {
23831 msg_outnum((long)++nr);
23832 MSG_PUTS(": ");
23833 msg_outtrans(get_tv_string(&li->li_tv));
23834 msg_putchar('\n');
23835 out_flush(); /* output one line at a time */
23836 ui_breakcheck();
23837 }
23838 /* Assume "got_int" was set to truncate the listing. */
23839 got_int = FALSE;
23840
23841#ifdef FEAT_BROWSE_CMD
23842 if (cmdmod.browse)
23843 {
23844 quit_more = FALSE;
23845 nr = prompt_for_number(FALSE);
23846 msg_starthere();
23847 if (nr > 0)
23848 {
23849 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23850 (long)nr);
23851
23852 if (p != NULL)
23853 {
23854 p = expand_env_save(p);
23855 eap->arg = p;
23856 eap->cmdidx = CMD_edit;
23857 cmdmod.browse = FALSE;
23858 do_exedit(eap, NULL);
23859 vim_free(p);
23860 }
23861 }
23862 }
23863#endif
23864 }
23865}
23866
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867#endif /* FEAT_EVAL */
23868
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023870#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871
23872#ifdef WIN3264
23873/*
23874 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23875 */
23876static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23877static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23878static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23879
23880/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023881 * Get the short path (8.3) for the filename in "fnamep".
23882 * Only works for a valid file name.
23883 * When the path gets longer "fnamep" is changed and the allocated buffer
23884 * is put in "bufp".
23885 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23886 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 */
23888 static int
23889get_short_pathname(fnamep, bufp, fnamelen)
23890 char_u **fnamep;
23891 char_u **bufp;
23892 int *fnamelen;
23893{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023894 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 char_u *newbuf;
23896
23897 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 l = GetShortPathName(*fnamep, *fnamep, len);
23899 if (l > len - 1)
23900 {
23901 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023902 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 newbuf = vim_strnsave(*fnamep, l);
23904 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906
23907 vim_free(*bufp);
23908 *fnamep = *bufp = newbuf;
23909
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023910 /* Really should always succeed, as the buffer is big enough. */
23911 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 }
23913
23914 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023915 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916}
23917
23918/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023919 * Get the short path (8.3) for the filename in "fname". The converted
23920 * path is returned in "bufp".
23921 *
23922 * Some of the directories specified in "fname" may not exist. This function
23923 * will shorten the existing directories at the beginning of the path and then
23924 * append the remaining non-existing path.
23925 *
23926 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023927 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023928 * bufp - Pointer to an allocated buffer for the filename.
23929 * fnamelen - Length of the filename pointed to by fname
23930 *
23931 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932 */
23933 static int
23934shortpath_for_invalid_fname(fname, bufp, fnamelen)
23935 char_u **fname;
23936 char_u **bufp;
23937 int *fnamelen;
23938{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023939 char_u *short_fname, *save_fname, *pbuf_unused;
23940 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023942 int old_len, len;
23943 int new_len, sfx_len;
23944 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945
23946 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023947 old_len = *fnamelen;
23948 save_fname = vim_strnsave(*fname, old_len);
23949 pbuf_unused = NULL;
23950 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023952 endp = save_fname + old_len - 1; /* Find the end of the copy */
23953 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023955 /*
23956 * Try shortening the supplied path till it succeeds by removing one
23957 * directory at a time from the tail of the path.
23958 */
23959 len = 0;
23960 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023962 /* go back one path-separator */
23963 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23964 --endp;
23965 if (endp <= save_fname)
23966 break; /* processed the complete path */
23967
23968 /*
23969 * Replace the path separator with a NUL and try to shorten the
23970 * resulting path.
23971 */
23972 ch = *endp;
23973 *endp = 0;
23974 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023975 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023976 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23977 {
23978 retval = FAIL;
23979 goto theend;
23980 }
23981 *endp = ch; /* preserve the string */
23982
23983 if (len > 0)
23984 break; /* successfully shortened the path */
23985
23986 /* failed to shorten the path. Skip the path separator */
23987 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 }
23989
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023990 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023992 /*
23993 * Succeeded in shortening the path. Now concatenate the shortened
23994 * path with the remaining path at the tail.
23995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023997 /* Compute the length of the new path. */
23998 sfx_len = (int)(save_endp - endp) + 1;
23999 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024001 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024003 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024005 /* There is not enough space in the currently allocated string,
24006 * copy it to a buffer big enough. */
24007 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024009 {
24010 retval = FAIL;
24011 goto theend;
24012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024013 }
24014 else
24015 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024016 /* Transfer short_fname to the main buffer (it's big enough),
24017 * unless get_short_pathname() did its work in-place. */
24018 *fname = *bufp = save_fname;
24019 if (short_fname != save_fname)
24020 vim_strncpy(save_fname, short_fname, len);
24021 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024023
24024 /* concat the not-shortened part of the path */
24025 vim_strncpy(*fname + len, endp, sfx_len);
24026 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024028
24029theend:
24030 vim_free(pbuf_unused);
24031 vim_free(save_fname);
24032
24033 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034}
24035
24036/*
24037 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024038 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 */
24040 static int
24041shortpath_for_partial(fnamep, bufp, fnamelen)
24042 char_u **fnamep;
24043 char_u **bufp;
24044 int *fnamelen;
24045{
24046 int sepcount, len, tflen;
24047 char_u *p;
24048 char_u *pbuf, *tfname;
24049 int hasTilde;
24050
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024051 /* Count up the path separators from the RHS.. so we know which part
24052 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024054 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 if (vim_ispathsep(*p))
24056 ++sepcount;
24057
24058 /* Need full path first (use expand_env() to remove a "~/") */
24059 hasTilde = (**fnamep == '~');
24060 if (hasTilde)
24061 pbuf = tfname = expand_env_save(*fnamep);
24062 else
24063 pbuf = tfname = FullName_save(*fnamep, FALSE);
24064
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024065 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024067 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24068 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069
24070 if (len == 0)
24071 {
24072 /* Don't have a valid filename, so shorten the rest of the
24073 * path if we can. This CAN give us invalid 8.3 filenames, but
24074 * there's not a lot of point in guessing what it might be.
24075 */
24076 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024077 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 }
24080
24081 /* Count the paths backward to find the beginning of the desired string. */
24082 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024083 {
24084#ifdef FEAT_MBYTE
24085 if (has_mbyte)
24086 p -= mb_head_off(tfname, p);
24087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 if (vim_ispathsep(*p))
24089 {
24090 if (sepcount == 0 || (hasTilde && sepcount == 1))
24091 break;
24092 else
24093 sepcount --;
24094 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024096 if (hasTilde)
24097 {
24098 --p;
24099 if (p >= tfname)
24100 *p = '~';
24101 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 }
24104 else
24105 ++p;
24106
24107 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24108 vim_free(*bufp);
24109 *fnamelen = (int)STRLEN(p);
24110 *bufp = pbuf;
24111 *fnamep = p;
24112
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024113 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114}
24115#endif /* WIN3264 */
24116
24117/*
24118 * Adjust a filename, according to a string of modifiers.
24119 * *fnamep must be NUL terminated when called. When returning, the length is
24120 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024121 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 * When there is an error, *fnamep is set to NULL.
24123 */
24124 int
24125modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24126 char_u *src; /* string with modifiers */
24127 int *usedlen; /* characters after src that are used */
24128 char_u **fnamep; /* file name so far */
24129 char_u **bufp; /* buffer for allocated file name or NULL */
24130 int *fnamelen; /* length of fnamep */
24131{
24132 int valid = 0;
24133 char_u *tail;
24134 char_u *s, *p, *pbuf;
24135 char_u dirname[MAXPATHL];
24136 int c;
24137 int has_fullname = 0;
24138#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024139 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 int has_shortname = 0;
24141#endif
24142
24143repeat:
24144 /* ":p" - full path/file_name */
24145 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24146 {
24147 has_fullname = 1;
24148
24149 valid |= VALID_PATH;
24150 *usedlen += 2;
24151
24152 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24153 if ((*fnamep)[0] == '~'
24154#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24155 && ((*fnamep)[1] == '/'
24156# ifdef BACKSLASH_IN_FILENAME
24157 || (*fnamep)[1] == '\\'
24158# endif
24159 || (*fnamep)[1] == NUL)
24160
24161#endif
24162 )
24163 {
24164 *fnamep = expand_env_save(*fnamep);
24165 vim_free(*bufp); /* free any allocated file name */
24166 *bufp = *fnamep;
24167 if (*fnamep == NULL)
24168 return -1;
24169 }
24170
24171 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024172 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 {
24174 if (vim_ispathsep(*p)
24175 && p[1] == '.'
24176 && (p[2] == NUL
24177 || vim_ispathsep(p[2])
24178 || (p[2] == '.'
24179 && (p[3] == NUL || vim_ispathsep(p[3])))))
24180 break;
24181 }
24182
24183 /* FullName_save() is slow, don't use it when not needed. */
24184 if (*p != NUL || !vim_isAbsName(*fnamep))
24185 {
24186 *fnamep = FullName_save(*fnamep, *p != NUL);
24187 vim_free(*bufp); /* free any allocated file name */
24188 *bufp = *fnamep;
24189 if (*fnamep == NULL)
24190 return -1;
24191 }
24192
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024193#ifdef WIN3264
24194# if _WIN32_WINNT >= 0x0500
24195 if (vim_strchr(*fnamep, '~') != NULL)
24196 {
24197 /* Expand 8.3 filename to full path. Needed to make sure the same
24198 * file does not have two different names.
24199 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24200 p = alloc(_MAX_PATH + 1);
24201 if (p != NULL)
24202 {
24203 if (GetLongPathName(*fnamep, p, MAXPATHL))
24204 {
24205 vim_free(*bufp);
24206 *bufp = *fnamep = p;
24207 }
24208 else
24209 vim_free(p);
24210 }
24211 }
24212# endif
24213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 /* Append a path separator to a directory. */
24215 if (mch_isdir(*fnamep))
24216 {
24217 /* Make room for one or two extra characters. */
24218 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24219 vim_free(*bufp); /* free any allocated file name */
24220 *bufp = *fnamep;
24221 if (*fnamep == NULL)
24222 return -1;
24223 add_pathsep(*fnamep);
24224 }
24225 }
24226
24227 /* ":." - path relative to the current directory */
24228 /* ":~" - path relative to the home directory */
24229 /* ":8" - shortname path - postponed till after */
24230 while (src[*usedlen] == ':'
24231 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24232 {
24233 *usedlen += 2;
24234 if (c == '8')
24235 {
24236#ifdef WIN3264
24237 has_shortname = 1; /* Postpone this. */
24238#endif
24239 continue;
24240 }
24241 pbuf = NULL;
24242 /* Need full path first (use expand_env() to remove a "~/") */
24243 if (!has_fullname)
24244 {
24245 if (c == '.' && **fnamep == '~')
24246 p = pbuf = expand_env_save(*fnamep);
24247 else
24248 p = pbuf = FullName_save(*fnamep, FALSE);
24249 }
24250 else
24251 p = *fnamep;
24252
24253 has_fullname = 0;
24254
24255 if (p != NULL)
24256 {
24257 if (c == '.')
24258 {
24259 mch_dirname(dirname, MAXPATHL);
24260 s = shorten_fname(p, dirname);
24261 if (s != NULL)
24262 {
24263 *fnamep = s;
24264 if (pbuf != NULL)
24265 {
24266 vim_free(*bufp); /* free any allocated file name */
24267 *bufp = pbuf;
24268 pbuf = NULL;
24269 }
24270 }
24271 }
24272 else
24273 {
24274 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24275 /* Only replace it when it starts with '~' */
24276 if (*dirname == '~')
24277 {
24278 s = vim_strsave(dirname);
24279 if (s != NULL)
24280 {
24281 *fnamep = s;
24282 vim_free(*bufp);
24283 *bufp = s;
24284 }
24285 }
24286 }
24287 vim_free(pbuf);
24288 }
24289 }
24290
24291 tail = gettail(*fnamep);
24292 *fnamelen = (int)STRLEN(*fnamep);
24293
24294 /* ":h" - head, remove "/file_name", can be repeated */
24295 /* Don't remove the first "/" or "c:\" */
24296 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24297 {
24298 valid |= VALID_HEAD;
24299 *usedlen += 2;
24300 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024301 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024302 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 *fnamelen = (int)(tail - *fnamep);
24304#ifdef VMS
24305 if (*fnamelen > 0)
24306 *fnamelen += 1; /* the path separator is part of the path */
24307#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024308 if (*fnamelen == 0)
24309 {
24310 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24311 p = vim_strsave((char_u *)".");
24312 if (p == NULL)
24313 return -1;
24314 vim_free(*bufp);
24315 *bufp = *fnamep = tail = p;
24316 *fnamelen = 1;
24317 }
24318 else
24319 {
24320 while (tail > s && !after_pathsep(s, tail))
24321 mb_ptr_back(*fnamep, tail);
24322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 }
24324
24325 /* ":8" - shortname */
24326 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24327 {
24328 *usedlen += 2;
24329#ifdef WIN3264
24330 has_shortname = 1;
24331#endif
24332 }
24333
24334#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024335 /*
24336 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337 */
24338 if (has_shortname)
24339 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024340 /* Copy the string if it is shortened by :h and when it wasn't copied
24341 * yet, because we are going to change it in place. Avoids changing
24342 * the buffer name for "%:8". */
24343 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 {
24345 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024346 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 return -1;
24348 vim_free(*bufp);
24349 *bufp = *fnamep = p;
24350 }
24351
24352 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024353 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 if (!has_fullname && !vim_isAbsName(*fnamep))
24355 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024356 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 return -1;
24358 }
24359 else
24360 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024361 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362
Bram Moolenaardc935552011-08-17 15:23:23 +020024363 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024365 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 return -1;
24367
24368 if (l == 0)
24369 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024370 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024372 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 return -1;
24374 }
24375 *fnamelen = l;
24376 }
24377 }
24378#endif /* WIN3264 */
24379
24380 /* ":t" - tail, just the basename */
24381 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24382 {
24383 *usedlen += 2;
24384 *fnamelen -= (int)(tail - *fnamep);
24385 *fnamep = tail;
24386 }
24387
24388 /* ":e" - extension, can be repeated */
24389 /* ":r" - root, without extension, can be repeated */
24390 while (src[*usedlen] == ':'
24391 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24392 {
24393 /* find a '.' in the tail:
24394 * - for second :e: before the current fname
24395 * - otherwise: The last '.'
24396 */
24397 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24398 s = *fnamep - 2;
24399 else
24400 s = *fnamep + *fnamelen - 1;
24401 for ( ; s > tail; --s)
24402 if (s[0] == '.')
24403 break;
24404 if (src[*usedlen + 1] == 'e') /* :e */
24405 {
24406 if (s > tail)
24407 {
24408 *fnamelen += (int)(*fnamep - (s + 1));
24409 *fnamep = s + 1;
24410#ifdef VMS
24411 /* cut version from the extension */
24412 s = *fnamep + *fnamelen - 1;
24413 for ( ; s > *fnamep; --s)
24414 if (s[0] == ';')
24415 break;
24416 if (s > *fnamep)
24417 *fnamelen = s - *fnamep;
24418#endif
24419 }
24420 else if (*fnamep <= tail)
24421 *fnamelen = 0;
24422 }
24423 else /* :r */
24424 {
24425 if (s > tail) /* remove one extension */
24426 *fnamelen = (int)(s - *fnamep);
24427 }
24428 *usedlen += 2;
24429 }
24430
24431 /* ":s?pat?foo?" - substitute */
24432 /* ":gs?pat?foo?" - global substitute */
24433 if (src[*usedlen] == ':'
24434 && (src[*usedlen + 1] == 's'
24435 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24436 {
24437 char_u *str;
24438 char_u *pat;
24439 char_u *sub;
24440 int sep;
24441 char_u *flags;
24442 int didit = FALSE;
24443
24444 flags = (char_u *)"";
24445 s = src + *usedlen + 2;
24446 if (src[*usedlen + 1] == 'g')
24447 {
24448 flags = (char_u *)"g";
24449 ++s;
24450 }
24451
24452 sep = *s++;
24453 if (sep)
24454 {
24455 /* find end of pattern */
24456 p = vim_strchr(s, sep);
24457 if (p != NULL)
24458 {
24459 pat = vim_strnsave(s, (int)(p - s));
24460 if (pat != NULL)
24461 {
24462 s = p + 1;
24463 /* find end of substitution */
24464 p = vim_strchr(s, sep);
24465 if (p != NULL)
24466 {
24467 sub = vim_strnsave(s, (int)(p - s));
24468 str = vim_strnsave(*fnamep, *fnamelen);
24469 if (sub != NULL && str != NULL)
24470 {
24471 *usedlen = (int)(p + 1 - src);
24472 s = do_string_sub(str, pat, sub, flags);
24473 if (s != NULL)
24474 {
24475 *fnamep = s;
24476 *fnamelen = (int)STRLEN(s);
24477 vim_free(*bufp);
24478 *bufp = s;
24479 didit = TRUE;
24480 }
24481 }
24482 vim_free(sub);
24483 vim_free(str);
24484 }
24485 vim_free(pat);
24486 }
24487 }
24488 /* after using ":s", repeat all the modifiers */
24489 if (didit)
24490 goto repeat;
24491 }
24492 }
24493
Bram Moolenaar26df0922014-02-23 23:39:13 +010024494 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24495 {
24496 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24497 if (p == NULL)
24498 return -1;
24499 vim_free(*bufp);
24500 *bufp = *fnamep = p;
24501 *fnamelen = (int)STRLEN(p);
24502 *usedlen += 2;
24503 }
24504
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 return valid;
24506}
24507
24508/*
24509 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24510 * "flags" can be "g" to do a global substitute.
24511 * Returns an allocated string, NULL for error.
24512 */
24513 char_u *
24514do_string_sub(str, pat, sub, flags)
24515 char_u *str;
24516 char_u *pat;
24517 char_u *sub;
24518 char_u *flags;
24519{
24520 int sublen;
24521 regmatch_T regmatch;
24522 int i;
24523 int do_all;
24524 char_u *tail;
24525 garray_T ga;
24526 char_u *ret;
24527 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024528 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024529
24530 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24531 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024532 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533
24534 ga_init2(&ga, 1, 200);
24535
24536 do_all = (flags[0] == 'g');
24537
24538 regmatch.rm_ic = p_ic;
24539 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24540 if (regmatch.regprog != NULL)
24541 {
24542 tail = str;
24543 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24544 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024545 /* Skip empty match except for first match. */
24546 if (regmatch.startp[0] == regmatch.endp[0])
24547 {
24548 if (zero_width == regmatch.startp[0])
24549 {
24550 /* avoid getting stuck on a match with an empty string */
24551 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24552 ++ga.ga_len;
24553 continue;
24554 }
24555 zero_width = regmatch.startp[0];
24556 }
24557
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 /*
24559 * Get some space for a temporary buffer to do the substitution
24560 * into. It will contain:
24561 * - The text up to where the match is.
24562 * - The substituted text.
24563 * - The text after the match.
24564 */
24565 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24566 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24567 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24568 {
24569 ga_clear(&ga);
24570 break;
24571 }
24572
24573 /* copy the text up to where the match is */
24574 i = (int)(regmatch.startp[0] - tail);
24575 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24576 /* add the substituted text */
24577 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24578 + ga.ga_len + i, TRUE, TRUE, FALSE);
24579 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024580 tail = regmatch.endp[0];
24581 if (*tail == NUL)
24582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 if (!do_all)
24584 break;
24585 }
24586
24587 if (ga.ga_data != NULL)
24588 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24589
Bram Moolenaar473de612013-06-08 18:19:48 +020024590 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024591 }
24592
24593 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24594 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024595 if (p_cpo == empty_option)
24596 p_cpo = save_cpo;
24597 else
24598 /* Darn, evaluating {sub} expression changed the value. */
24599 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600
24601 return ret;
24602}
24603
24604#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */