blob: 693896cffff90c233805704ab7931e6fda340023 [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 Moolenaar33570922005-01-25 22:26:29 +0000364};
365
366/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_type vv_di.di_tv.v_type
368#define vv_nr vv_di.di_tv.vval.v_number
369#define vv_float vv_di.di_tv.vval.v_float
370#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000371#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
378static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
380static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
381static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
383static void list_glob_vars __ARGS((int *first));
384static void list_buf_vars __ARGS((int *first));
385static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_vim_vars __ARGS((int *first));
390static void list_script_vars __ARGS((int *first));
391static void list_func_vars __ARGS((int *first));
392static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
394static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100395static 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 +0000396static void clear_lval __ARGS((lval_T *lp));
397static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
398static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
400static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
401static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
402static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
403static void item_lock __ARGS((typval_T *tv, int deep, int lock));
404static int tv_islocked __ARGS((typval_T *tv));
405
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
407static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000412static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
413static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000415static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000420static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100422static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
423static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
424static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000431static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100432static 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 +0000433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000434static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200435static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#ifdef FEAT_FLOAT
446static int string2float __ARGS((char_u *text, float_T *value));
447#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
450static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
451static 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 +0200452static 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 +0000453static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000454static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200458static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100461static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
485static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
486#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000487static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000490static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000493static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200501static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
506static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517#ifdef FEAT_FLOAT
518static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200564static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100592static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000594static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200607static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#ifdef FEAT_LUA
611static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000618static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000621static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625#ifdef vim_mkdir
626static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100629#ifdef FEAT_MZSCHEME
630static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100634static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000636#ifdef FEAT_FLOAT
637static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000640static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200642#ifdef FEAT_PYTHON3
643static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645#ifdef FEAT_PYTHON
646static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200665static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100667static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100687#ifdef FEAT_CRYPT
688static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
689#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000690static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200691static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100750static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100775static 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 +0000776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
787static 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 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200852 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
853 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200854 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857
858 for (i = 0; i < VV_LEN; ++i)
859 {
860 p = &vimvars[i];
861 STRCPY(p->vv_di.di_key, p->vv_name);
862 if (p->vv_flags & VV_RO)
863 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 else if (p->vv_flags & VV_RO_SBX)
865 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
866 else
867 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000868
869 /* add to v: scope dict, unless the value is not always available */
870 if (p->vv_type != VAR_UNKNOWN)
871 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 /* add to compat scope dict */
874 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000876 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100877 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917
918 /* global variables */
919 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000921 /* autoloaded script names */
922 ga_clear_strings(&ga_loaded);
923
Bram Moolenaarcca74132013-09-25 21:00:28 +0200924 /* Script-local variables. First clear all the variables and in a second
925 * loop free the scriptvar_T, because a variable in one script might hold
926 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001638/*
1639 * Call vimL function "func" and return the result as a number.
1640 * Returns -1 when calling the function fails.
1641 * Uses argv[argc] for the function arguments.
1642 */
1643 long
1644call_func_retnr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
1651 long retval;
1652
1653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
1661
1662#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1663 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1664
Bram Moolenaar4f688582007-07-24 12:34:30 +00001665# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 * Call vimL function "func" and return the result as a string.
1668 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Uses argv[argc] for the function arguments.
1670 */
1671 void *
1672call_func_retstr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 return NULL;
1684
1685 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return retval;
1688}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001689# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 */
1696 void *
1697call_func_retlist(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 return NULL;
1708
1709 if (rettv.v_type != VAR_LIST)
1710 {
1711 clear_tv(&rettv);
1712 return NULL;
1713 }
1714
1715 return rettv.vval.v_list;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718
1719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar429fa852013-04-15 12:27:36 +02002128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002244 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002524 * flags:
2525 * GLV_QUIET: do not give error messages
2526 * GLV_NO_AUTOLOAD: do not use script autoloading
2527 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002529 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 */
2532 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 typval_T *rettv;
2536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int unlet;
2538 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *expr_start, *expr_end;
2544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 dictitem_T *v;
2546 typval_T var1;
2547 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002754 else
2755 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2757 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 || !valid_varname(key);
2760 if (len != -1)
2761 key[len] = prevval;
2762 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 return NULL;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* Can't add "v:" variable. */
2769 if (lp->ll_dict == &vimvardict)
2770 {
2771 EMSG2(_(e_illvar), name);
2772 return NULL;
2773 }
2774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (len == -1)
2789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 p = NULL;
2792 break;
2793 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 /* existing variable, need to check if it can be changed */
2795 else if (var_check_ro(lp->ll_di->di_flags, name))
2796 return NULL;
2797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 else
2803 {
2804 /*
2805 * Get the number and item for the only or first index of the List.
2806 */
2807 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
2810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_list = lp->ll_tv->vval.v_list;
2816 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2817 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (lp->ll_n1 < 0)
2820 {
2821 lp->ll_n1 = 0;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 }
2824 }
2825 if (lp->ll_li == NULL)
2826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
2834 /*
2835 * May need to find the item or absolute index for the second
2836 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 * When no index given: "lp->ll_empty2" is TRUE.
2838 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2857 if (lp->ll_n1 < 0)
2858 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2859 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 {
2861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return p;
2872}
2873
2874/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
2878clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 vim_free(lp->ll_exp_name);
2882 vim_free(lp->ll_newkey);
2883}
2884
2885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 */
2890 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *ri;
2900 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901
2902 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *endp;
2907 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002914 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 {
2916 if (tv_op(&tv, rettv, op) == OK)
2917 set_var(lp->ll_name, &tv, FALSE);
2918 clear_tv(&tv);
2919 }
2920 }
2921 else
2922 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else if (tv_check_lock(lp->ll_newkey == NULL
2927 ? lp->ll_tv->v_lock
2928 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2929 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 else if (lp->ll_range)
2931 {
2932 /*
2933 * Assign the List values to the list items.
2934 */
2935 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2939 else
2940 {
2941 clear_tv(&lp->ll_li->li_tv);
2942 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = ri->li_next;
2945 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2946 break;
2947 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002950 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 ri = NULL;
2953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_li = lp->ll_li->li_next;
2957 ++lp->ll_n1;
2958 }
2959 if (ri != NULL)
2960 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else if (lp->ll_empty2
2962 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 : lp->ll_n1 != lp->ll_n2)
2964 EMSG(_("E711: List value has not enough items"));
2965 }
2966 else
2967 {
2968 /*
2969 * Assign to a List or Dictionary item.
2970 */
2971 if (lp->ll_newkey != NULL)
2972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
2975 EMSG2(_(e_letwrong), op);
2976 return;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002980 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (di == NULL)
2982 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002983 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2984 {
2985 vim_free(di);
2986 return;
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 else if (op != NULL && *op != '=')
2991 {
2992 tv_op(lp->ll_tv, rettv, op);
2993 return;
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the value to the variable or list item.
3000 */
3001 if (copy)
3002 copy_tv(rettv, lp->ll_tv);
3003 else
3004 {
3005 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010}
3011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3014 * Returns OK or FAIL.
3015 */
3016 static int
3017tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T *tv1;
3019 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 char_u *op;
3021{
3022 long n;
3023 char_u numbuf[NUMBUFLEN];
3024 char_u *s;
3025
3026 /* Can't do anything with a Funcref or a Dict on the right. */
3027 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3028 {
3029 switch (tv1->v_type)
3030 {
3031 case VAR_DICT:
3032 case VAR_FUNC:
3033 break;
3034
3035 case VAR_LIST:
3036 if (*op != '+' || tv2->v_type != VAR_LIST)
3037 break;
3038 /* List += List */
3039 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3040 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3041 return OK;
3042
3043 case VAR_NUMBER:
3044 case VAR_STRING:
3045 if (tv2->v_type == VAR_LIST)
3046 break;
3047 if (*op == '+' || *op == '-')
3048 {
3049 /* nr += nr or nr -= nr*/
3050 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#ifdef FEAT_FLOAT
3052 if (tv2->v_type == VAR_FLOAT)
3053 {
3054 float_T f = n;
3055
3056 if (*op == '+')
3057 f += tv2->vval.v_float;
3058 else
3059 f -= tv2->vval.v_float;
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_FLOAT;
3062 tv1->vval.v_float = f;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
3066 {
3067 if (*op == '+')
3068 n += get_tv_number(tv2);
3069 else
3070 n -= get_tv_number(tv2);
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_NUMBER;
3073 tv1->vval.v_number = n;
3074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 }
3076 else
3077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078 if (tv2->v_type == VAR_FLOAT)
3079 break;
3080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 /* str .= str */
3082 s = get_tv_string(tv1);
3083 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3084 clear_tv(tv1);
3085 tv1->v_type = VAR_STRING;
3086 tv1->vval.v_string = s;
3087 }
3088 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089
3090#ifdef FEAT_FLOAT
3091 case VAR_FLOAT:
3092 {
3093 float_T f;
3094
3095 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3096 && tv2->v_type != VAR_NUMBER
3097 && tv2->v_type != VAR_STRING))
3098 break;
3099 if (tv2->v_type == VAR_FLOAT)
3100 f = tv2->vval.v_float;
3101 else
3102 f = get_tv_number(tv2);
3103 if (*op == '+')
3104 tv1->vval.v_float += f;
3105 else
3106 tv1->vval.v_float -= f;
3107 }
3108 return OK;
3109#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 }
3112
3113 EMSG2(_(e_letwrong), op);
3114 return FAIL;
3115}
3116
3117/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * Add a watcher to a list.
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
3125 lw->lw_next = l->lv_watch;
3126 l->lv_watch = lw;
3127}
3128
3129/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003130 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 * No warning when it isn't found...
3132 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003133 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 lwp = &l->lv_watch;
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 {
3143 if (lw == lwrem)
3144 {
3145 *lwp = lw->lw_next;
3146 break;
3147 }
3148 lwp = &lw->lw_next;
3149 }
3150}
3151
3152/*
3153 * Just before removing an item from a list: advance watchers to the next
3154 * item.
3155 */
3156 static void
3157list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 list_T *l;
3159 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3164 if (lw->lw_item == item)
3165 lw->lw_item = item->li_next;
3166}
3167
3168/*
3169 * Evaluate the expression used in a ":for var in expr" command.
3170 * "arg" points to "var".
3171 * Set "*errp" to TRUE for an error, FALSE otherwise;
3172 * Return a pointer that holds the info. Null when there is an error.
3173 */
3174 void *
3175eval_for_line(arg, errp, nextcmdp, skip)
3176 char_u *arg;
3177 int *errp;
3178 char_u **nextcmdp;
3179 int skip;
3180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 typval_T tv;
3184 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 *errp = TRUE; /* default: there is an error */
3187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 if (fi == NULL)
3190 return NULL;
3191
3192 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3193 if (expr == NULL)
3194 return fi;
3195
3196 expr = skipwhite(expr);
3197 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3198 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003199 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 return fi;
3201 }
3202
3203 if (skip)
3204 ++emsg_skip;
3205 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3206 {
3207 *errp = FALSE;
3208 if (!skip)
3209 {
3210 l = tv.vval.v_list;
3211 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 clear_tv(&tv);
3215 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 else
3217 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003218 /* No need to increment the refcount, it's already set for the
3219 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 fi->fi_list = l;
3221 list_add_watch(l, &fi->fi_lw);
3222 fi->fi_lw.lw_item = l->lv_first;
3223 }
3224 }
3225 }
3226 if (skip)
3227 --emsg_skip;
3228
3229 return fi;
3230}
3231
3232/*
3233 * Use the first item in a ":for" list. Advance to the next.
3234 * Assign the values to the variable (list). "arg" points to the first one.
3235 * Return TRUE when a valid item was found, FALSE when at end of list or
3236 * something wrong.
3237 */
3238 int
3239next_for_item(fi_void, arg)
3240 void *fi_void;
3241 char_u *arg;
3242{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 item = fi->fi_lw.lw_item;
3248 if (item == NULL)
3249 result = FALSE;
3250 else
3251 {
3252 fi->fi_lw.lw_item = item->li_next;
3253 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3254 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3255 }
3256 return result;
3257}
3258
3259/*
3260 * Free the structure used to store info used by ":for".
3261 */
3262 void
3263free_for_info(fi_void)
3264 void *fi_void;
3265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003268 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 list_unref(fi->fi_list);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 vim_free(fi);
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3277
3278 void
3279set_context_for_expression(xp, arg, cmdidx)
3280 expand_T *xp;
3281 char_u *arg;
3282 cmdidx_T cmdidx;
3283{
3284 int got_eq = FALSE;
3285 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 if (cmdidx == CMD_let)
3289 {
3290 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003291 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 {
3293 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 {
3296 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (vim_iswhite(*p))
3299 break;
3300 }
3301 return;
3302 }
3303 }
3304 else
3305 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3306 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while ((xp->xp_pattern = vim_strpbrk(arg,
3308 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3309 {
3310 c = *xp->xp_pattern;
3311 if (c == '&')
3312 {
3313 c = xp->xp_pattern[1];
3314 if (c == '&')
3315 {
3316 ++xp->xp_pattern;
3317 xp->xp_context = cmdidx != CMD_let || got_eq
3318 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3319 }
3320 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003323 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3324 xp->xp_pattern += 2;
3325
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 else if (c == '$')
3329 {
3330 /* environment variable */
3331 xp->xp_context = EXPAND_ENV_VARS;
3332 }
3333 else if (c == '=')
3334 {
3335 got_eq = TRUE;
3336 xp->xp_context = EXPAND_EXPRESSION;
3337 }
3338 else if (c == '<'
3339 && xp->xp_context == EXPAND_FUNCTIONS
3340 && vim_strchr(xp->xp_pattern, '(') == NULL)
3341 {
3342 /* Function name can start with "<SNR>" */
3343 break;
3344 }
3345 else if (cmdidx != CMD_let || got_eq)
3346 {
3347 if (c == '"') /* string */
3348 {
3349 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3350 if (c == '\\' && xp->xp_pattern[1] != NUL)
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '\'') /* literal string */
3355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3358 /* skip */ ;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '|')
3362 {
3363 if (xp->xp_pattern[1] == '|')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
3368 else
3369 xp->xp_context = EXPAND_COMMANDS;
3370 }
3371 else
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 /* Doesn't look like something valid, expand as an expression
3376 * anyway. */
3377 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 arg = xp->xp_pattern;
3379 if (*arg != NUL)
3380 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3381 /* skip */ ;
3382 }
3383 xp->xp_pattern = arg;
3384}
3385
3386#endif /* FEAT_CMDL_COMPL */
3387
3388/*
3389 * ":1,25call func(arg1, arg2)" function call.
3390 */
3391 void
3392ex_call(eap)
3393 exarg_T *eap;
3394{
3395 char_u *arg = eap->arg;
3396 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 linenr_T lnum;
3402 int doesrange;
3403 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 if (eap->skip)
3407 {
3408 /* trans_function_name() doesn't work well when skipping, use eval0()
3409 * instead to skip to any following command, e.g. for:
3410 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003411 ++emsg_skip;
3412 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3413 clear_tv(&rettv);
3414 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 return;
3416 }
3417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003418 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003419 if (fudi.fd_newkey != NULL)
3420 {
3421 /* Still need to give an error message for missing key. */
3422 EMSG2(_(e_dictkey), fudi.fd_newkey);
3423 vim_free(fudi.fd_newkey);
3424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 if (tofree == NULL)
3426 return;
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 /* Increase refcount on dictionary, it could get deleted when evaluating
3429 * the arguments. */
3430 if (fudi.fd_dict != NULL)
3431 ++fudi.fd_dict->dv_refcount;
3432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar532c7802005-01-27 14:44:31 +00003437 /* Skip white space to allow ":call func ()". Not good, but required for
3438 * backward compatibility. */
3439 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 if (*startarg != '(')
3443 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003444 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 goto end;
3446 }
3447
3448 /*
3449 * When skipping, evaluate the function once, to find the end of the
3450 * arguments.
3451 * When the function takes a range, this is discovered after the first
3452 * call, and the loop is broken.
3453 */
3454 if (eap->skip)
3455 {
3456 ++emsg_skip;
3457 lnum = eap->line2; /* do it once, also with an invalid range */
3458 }
3459 else
3460 lnum = eap->line1;
3461 for ( ; lnum <= eap->line2; ++lnum)
3462 {
3463 if (!eap->skip && eap->addr_count > 0)
3464 {
3465 curwin->w_cursor.lnum = lnum;
3466 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003467#ifdef FEAT_VIRTUALEDIT
3468 curwin->w_cursor.coladd = 0;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 eap->line1, eap->line2, &doesrange,
3474 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 failed = TRUE;
3477 break;
3478 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
3480 /* Handle a function returning a Funcref, Dictionary or List. */
3481 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3482 {
3483 failed = TRUE;
3484 break;
3485 }
3486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (doesrange || eap->skip)
3489 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (aborting())
3496 break;
3497 }
3498 if (eap->skip)
3499 --emsg_skip;
3500
3501 if (!failed)
3502 {
3503 /* Check for trailing illegal characters and a following command. */
3504 if (!ends_excmd(*arg))
3505 {
3506 emsg_severe = TRUE;
3507 EMSG(_(e_trailing));
3508 }
3509 else
3510 eap->nextcmd = check_nextcmd(arg);
3511 }
3512
3513end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * ":unlet[!] var1 ... " command.
3520 */
3521 void
3522ex_unlet(eap)
3523 exarg_T *eap;
3524{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 ex_unletlock(eap, eap->arg, 0);
3526}
3527
3528/*
3529 * ":lockvar" and ":unlockvar" commands
3530 */
3531 void
3532ex_lockvar(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int deep = 2;
3537
3538 if (eap->forceit)
3539 deep = -1;
3540 else if (vim_isdigit(*arg))
3541 {
3542 deep = getdigits(&arg);
3543 arg = skipwhite(arg);
3544 }
3545
3546 ex_unletlock(eap, arg, deep);
3547}
3548
3549/*
3550 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3551 */
3552 static void
3553ex_unletlock(eap, argstart, deep)
3554 exarg_T *eap;
3555 char_u *argstart;
3556 int deep;
3557{
3558 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 do
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003566 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003567 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (lv.ll_name == NULL)
3569 error = TRUE; /* error but continue parsing */
3570 if (name_end == NULL || (!vim_iswhite(*name_end)
3571 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (name_end != NULL)
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 if (!(eap->skip || error))
3579 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 {
3585 if (eap->cmdidx == CMD_unlet)
3586 {
3587 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3588 error = TRUE;
3589 }
3590 else
3591 {
3592 if (do_lock_var(&lv, name_end, deep,
3593 eap->cmdidx == CMD_lockvar) == FAIL)
3594 error = TRUE;
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 if (!eap->skip)
3599 clear_lval(&lv);
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 arg = skipwhite(name_end);
3602 } while (!ends_excmd(*arg));
3603
3604 eap->nextcmd = check_nextcmd(arg);
3605}
3606
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 int forceit;
3612{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 int ret = OK;
3614 int cc;
3615
3616 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3629 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 else if (lp->ll_range)
3631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633
3634 /* Delete a range of List items. */
3635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3636 {
3637 li = lp->ll_li->li_next;
3638 listitem_remove(lp->ll_list, lp->ll_li);
3639 lp->ll_li = li;
3640 ++lp->ll_n1;
3641 }
3642 }
3643 else
3644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 /* unlet a List item. */
3647 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003650 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 }
3652
3653 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hashtab_T *ht;
3666 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 ht = find_var_ht(name, &varname);
3671 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = hash_find(ht, varname);
3674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003676 di = HI2DI(hi);
3677 if (var_check_fixed(di->di_flags, name)
3678 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
3680 delete_var(ht, hi);
3681 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 if (forceit)
3685 return OK;
3686 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688}
3689
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690/*
3691 * Lock or unlock variable indicated by "lp".
3692 * "deep" is the levels to go (-1 for unlimited);
3693 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3694 */
3695 static int
3696do_lock_var(lp, name_end, deep, lock)
3697 lval_T *lp;
3698 char_u *name_end;
3699 int deep;
3700 int lock;
3701{
3702 int ret = OK;
3703 int cc;
3704 dictitem_T *di;
3705
3706 if (deep == 0) /* nothing to do */
3707 return OK;
3708
3709 if (lp->ll_tv == NULL)
3710 {
3711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
3717 else
3718 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003719 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 if (di == NULL)
3721 ret = FAIL;
3722 else
3723 {
3724 if (lock)
3725 di->di_flags |= DI_FLAGS_LOCK;
3726 else
3727 di->di_flags &= ~DI_FLAGS_LOCK;
3728 item_lock(&di->di_tv, deep, lock);
3729 }
3730 }
3731 *name_end = cc;
3732 }
3733 else if (lp->ll_range)
3734 {
3735 listitem_T *li = lp->ll_li;
3736
3737 /* (un)lock a range of List items. */
3738 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3739 {
3740 item_lock(&li->li_tv, deep, lock);
3741 li = li->li_next;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else if (lp->ll_list != NULL)
3746 /* (un)lock a List item. */
3747 item_lock(&lp->ll_li->li_tv, deep, lock);
3748 else
3749 /* un(lock) a Dictionary item. */
3750 item_lock(&lp->ll_di->di_tv, deep, lock);
3751
3752 return ret;
3753}
3754
3755/*
3756 * Lock or unlock an item. "deep" is nr of levels to go.
3757 */
3758 static void
3759item_lock(tv, deep, lock)
3760 typval_T *tv;
3761 int deep;
3762 int lock;
3763{
3764 static int recurse = 0;
3765 list_T *l;
3766 listitem_T *li;
3767 dict_T *d;
3768 hashitem_T *hi;
3769 int todo;
3770
3771 if (recurse >= DICT_MAXNEST)
3772 {
3773 EMSG(_("E743: variable nested too deep for (un)lock"));
3774 return;
3775 }
3776 if (deep == 0)
3777 return;
3778 ++recurse;
3779
3780 /* lock/unlock the item itself */
3781 if (lock)
3782 tv->v_lock |= VAR_LOCKED;
3783 else
3784 tv->v_lock &= ~VAR_LOCKED;
3785
3786 switch (tv->v_type)
3787 {
3788 case VAR_LIST:
3789 if ((l = tv->vval.v_list) != NULL)
3790 {
3791 if (lock)
3792 l->lv_lock |= VAR_LOCKED;
3793 else
3794 l->lv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 /* recursive: lock/unlock the items the List contains */
3797 for (li = l->lv_first; li != NULL; li = li->li_next)
3798 item_lock(&li->li_tv, deep - 1, lock);
3799 }
3800 break;
3801 case VAR_DICT:
3802 if ((d = tv->vval.v_dict) != NULL)
3803 {
3804 if (lock)
3805 d->dv_lock |= VAR_LOCKED;
3806 else
3807 d->dv_lock &= ~VAR_LOCKED;
3808 if (deep < 0 || deep > 1)
3809 {
3810 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003811 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3813 {
3814 if (!HASHITEM_EMPTY(hi))
3815 {
3816 --todo;
3817 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3818 }
3819 }
3820 }
3821 }
3822 }
3823 --recurse;
3824}
3825
Bram Moolenaara40058a2005-07-11 22:42:07 +00003826/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3828 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003829 */
3830 static int
3831tv_islocked(tv)
3832 typval_T *tv;
3833{
3834 return (tv->v_lock & VAR_LOCKED)
3835 || (tv->v_type == VAR_LIST
3836 && tv->vval.v_list != NULL
3837 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3838 || (tv->v_type == VAR_DICT
3839 && tv->vval.v_dict != NULL
3840 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3844/*
3845 * Delete all "menutrans_" variables.
3846 */
3847 void
3848del_menutrans_vars()
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3861 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 }
3863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866#endif
3867
3868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3869
3870/*
3871 * Local string buffer for the next two functions to store a variable name
3872 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3873 * get_user_var_name().
3874 */
3875
3876static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3877
3878static char_u *varnamebuf = NULL;
3879static int varnamebuflen = 0;
3880
3881/*
3882 * Function to concatenate a prefix and a variable name.
3883 */
3884 static char_u *
3885cat_prefix_varname(prefix, name)
3886 int prefix;
3887 char_u *name;
3888{
3889 int len;
3890
3891 len = (int)STRLEN(name) + 3;
3892 if (len > varnamebuflen)
3893 {
3894 vim_free(varnamebuf);
3895 len += 10; /* some additional space */
3896 varnamebuf = alloc(len);
3897 if (varnamebuf == NULL)
3898 {
3899 varnamebuflen = 0;
3900 return NULL;
3901 }
3902 varnamebuflen = len;
3903 }
3904 *varnamebuf = prefix;
3905 varnamebuf[1] = ':';
3906 STRCPY(varnamebuf + 2, name);
3907 return varnamebuf;
3908}
3909
3910/*
3911 * Function given to ExpandGeneric() to obtain the list of user defined
3912 * (global/buffer/window/built-in) variable names.
3913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *
3915get_user_var_name(xp, idx)
3916 expand_T *xp;
3917 int idx;
3918{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 static long_u gdone;
3920 static long_u bdone;
3921 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922#ifdef FEAT_WINDOWS
3923 static long_u tdone;
3924#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static int vidx;
3926 static hashitem_T *hi;
3927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 tdone = 0;
3934#endif
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* Global variables */
3938 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3947 return cat_prefix_varname('g', hi->hi_key);
3948 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003952 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return (char_u *)"b:changedtick";
3967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
3969 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003973 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 else
3976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 if (tdone < ht->ht_used)
3986 {
3987 if (tdone++ == 0)
3988 hi = ht->ht_array;
3989 else
3990 ++hi;
3991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('t', hi->hi_key);
3994 }
3995#endif
3996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 /* v: variables */
3998 if (vidx < VV_LEN)
3999 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 vim_free(varnamebuf);
4002 varnamebuf = NULL;
4003 varnamebuflen = 0;
4004 return NULL;
4005}
4006
4007#endif /* FEAT_CMDL_COMPL */
4008
4009/*
4010 * types for expressions.
4011 */
4012typedef enum
4013{
4014 TYPE_UNKNOWN = 0
4015 , TYPE_EQUAL /* == */
4016 , TYPE_NEQUAL /* != */
4017 , TYPE_GREATER /* > */
4018 , TYPE_GEQUAL /* >= */
4019 , TYPE_SMALLER /* < */
4020 , TYPE_SEQUAL /* <= */
4021 , TYPE_MATCH /* =~ */
4022 , TYPE_NOMATCH /* !~ */
4023} exptype_T;
4024
4025/*
4026 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4029 */
4030
4031/*
4032 * Handle zero level expression.
4033 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004035 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return OK or FAIL.
4037 */
4038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 char_u **nextcmd;
4043 int evaluate;
4044{
4045 int ret;
4046 char_u *p;
4047
4048 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (ret == FAIL || !ends_excmd(*p))
4051 {
4052 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * Report the invalid expression unless the expression evaluation has
4056 * been cancelled due to an aborting error, an interrupt, or an
4057 * exception.
4058 */
4059 if (!aborting())
4060 EMSG2(_(e_invexpr2), arg);
4061 ret = FAIL;
4062 }
4063 if (nextcmd != NULL)
4064 *nextcmd = check_nextcmd(p);
4065
4066 return ret;
4067}
4068
4069/*
4070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * "arg" must point to the first non-white of the expression.
4074 * "arg" is advanced to the next non-white after the recognized expression.
4075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004076 * Note: "rettv.v_lock" is not set.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
4086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 /*
4090 * Get the first variable.
4091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094
4095 if ((*arg)[0] == '?')
4096 {
4097 result = FALSE;
4098 if (evaluate)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 int error = FALSE;
4101
4102 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (error)
4106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115
4116 /*
4117 * Check for the ":".
4118 */
4119 if ((*arg)[0] != ':')
4120 {
4121 EMSG(_("E109: Missing ':' after '?'"));
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126
4127 /*
4128 * Get the third variable.
4129 */
4130 *arg = skipwhite(*arg + 1);
4131 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4132 {
4133 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * Handle first level expression:
4146 * expr2 || expr2 || expr2 logical OR
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 long result;
4161 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 /*
4171 * Repeat until there is no following "||".
4172 */
4173 first = TRUE;
4174 result = FALSE;
4175 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4176 {
4177 if (evaluate && first)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 first = FALSE;
4185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 2);
4191 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4192 return FAIL;
4193
4194 /*
4195 * Compute the result.
4196 */
4197 if (evaluate && !result)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (error)
4203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 if (evaluate)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_NUMBER;
4208 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 return OK;
4213}
4214
4215/*
4216 * Handle second level expression:
4217 * expr3 && expr3 && expr3 logical AND
4218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 long result;
4232 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * Get the first variable.
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240
4241 /*
4242 * Repeat until there is no following "&&".
4243 */
4244 first = TRUE;
4245 result = TRUE;
4246 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4247 {
4248 if (evaluate && first)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (error)
4254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 first = FALSE;
4256 }
4257
4258 /*
4259 * Get the second variable.
4260 */
4261 *arg = skipwhite(*arg + 2);
4262 if (eval4(arg, &var2, evaluate && result) == FAIL)
4263 return FAIL;
4264
4265 /*
4266 * Compute the result.
4267 */
4268 if (evaluate && result)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (evaluate)
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle third level expression:
4288 * var1 == var2
4289 * var1 =~ var2
4290 * var1 != var2
4291 * var1 !~ var2
4292 * var1 > var2
4293 * var1 >= var2
4294 * var1 < var2
4295 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 * var1 is var2
4297 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int evaluate;
4309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u *p;
4312 int i;
4313 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int len = 2;
4316 long n1, n2;
4317 char_u *s1, *s2;
4318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4319 regmatch_T regmatch;
4320 int ic;
4321 char_u *save_cpo;
4322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 p = *arg;
4330 switch (p[0])
4331 {
4332 case '=': if (p[1] == '=')
4333 type = TYPE_EQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_MATCH;
4336 break;
4337 case '!': if (p[1] == '=')
4338 type = TYPE_NEQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_NOMATCH;
4341 break;
4342 case '>': if (p[1] != '=')
4343 {
4344 type = TYPE_GREATER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_GEQUAL;
4349 break;
4350 case '<': if (p[1] != '=')
4351 {
4352 type = TYPE_SMALLER;
4353 len = 1;
4354 }
4355 else
4356 type = TYPE_SEQUAL;
4357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 case 'i': if (p[1] == 's')
4359 {
4360 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4361 len = 5;
4362 if (!vim_isIDc(p[len]))
4363 {
4364 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4365 type_is = TRUE;
4366 }
4367 }
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 if (type != TYPE_UNKNOWN)
4375 {
4376 /* extra question mark appended: ignore case */
4377 if (p[len] == '?')
4378 {
4379 ic = TRUE;
4380 ++len;
4381 }
4382 /* extra '#' appended: match case */
4383 else if (p[len] == '#')
4384 {
4385 ic = FALSE;
4386 ++len;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 else
4390 ic = p_ic;
4391
4392 /*
4393 * Get the second variable.
4394 */
4395 *arg = skipwhite(p + len);
4396 if (eval5(arg, &var2, evaluate) == FAIL)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400 }
4401
4402 if (evaluate)
4403 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type_is && rettv->v_type != var2.v_type)
4405 {
4406 /* For "is" a different type always means FALSE, for "notis"
4407 * it means TRUE. */
4408 n1 = (type == TYPE_NEQUAL);
4409 }
4410 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_list == var2.vval.v_list);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004425 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4434 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004440 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4441 {
4442 if (type_is)
4443 {
4444 n1 = (rettv->v_type == var2.v_type
4445 && rettv->vval.v_dict == var2.vval.v_dict);
4446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 else if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
4453 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4454 else
4455 EMSG(_("E736: Invalid operation for Dictionary"));
4456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004463 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4464 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4471 {
4472 if (rettv->v_type != var2.v_type
4473 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4474 {
4475 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 return FAIL;
4482 }
4483 else
4484 {
4485 /* Compare two Funcrefs for being equal or unequal. */
4486 if (rettv->vval.v_string == NULL
4487 || var2.vval.v_string == NULL)
4488 n1 = FALSE;
4489 else
4490 n1 = STRCMP(rettv->vval.v_string,
4491 var2.vval.v_string) == 0;
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004497#ifdef FEAT_FLOAT
4498 /*
4499 * If one of the two variables is a float, compare as a float.
4500 * When using "=~" or "!~", always compare as string.
4501 */
4502 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4503 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4504 {
4505 float_T f1, f2;
4506
4507 if (rettv->v_type == VAR_FLOAT)
4508 f1 = rettv->vval.v_float;
4509 else
4510 f1 = get_tv_number(rettv);
4511 if (var2.v_type == VAR_FLOAT)
4512 f2 = var2.vval.v_float;
4513 else
4514 f2 = get_tv_number(&var2);
4515 n1 = FALSE;
4516 switch (type)
4517 {
4518 case TYPE_EQUAL: n1 = (f1 == f2); break;
4519 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4520 case TYPE_GREATER: n1 = (f1 > f2); break;
4521 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4522 case TYPE_SMALLER: n1 = (f1 < f2); break;
4523 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4524 case TYPE_UNKNOWN:
4525 case TYPE_MATCH:
4526 case TYPE_NOMATCH: break; /* avoid gcc warning */
4527 }
4528 }
4529#endif
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * If one of the two variables is a number, compare as a number.
4533 * When using "=~" or "!~", always compare as string.
4534 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 n1 = get_tv_number(rettv);
4539 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (n1 == n2); break;
4543 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4544 case TYPE_GREATER: n1 = (n1 > n2); break;
4545 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4546 case TYPE_SMALLER: n1 = (n1 < n2); break;
4547 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4548 case TYPE_UNKNOWN:
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH: break; /* avoid gcc warning */
4551 }
4552 }
4553 else
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 s1 = get_tv_string_buf(rettv, buf1);
4556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4559 else
4560 i = 0;
4561 n1 = FALSE;
4562 switch (type)
4563 {
4564 case TYPE_EQUAL: n1 = (i == 0); break;
4565 case TYPE_NEQUAL: n1 = (i != 0); break;
4566 case TYPE_GREATER: n1 = (i > 0); break;
4567 case TYPE_GEQUAL: n1 = (i >= 0); break;
4568 case TYPE_SMALLER: n1 = (i < 0); break;
4569 case TYPE_SEQUAL: n1 = (i <= 0); break;
4570
4571 case TYPE_MATCH:
4572 case TYPE_NOMATCH:
4573 /* avoid 'l' flag in 'cpoptions' */
4574 save_cpo = p_cpo;
4575 p_cpo = (char_u *)"";
4576 regmatch.regprog = vim_regcomp(s2,
4577 RE_MAGIC + RE_STRING);
4578 regmatch.rm_ic = ic;
4579 if (regmatch.regprog != NULL)
4580 {
4581 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (type == TYPE_NOMATCH)
4584 n1 = !n1;
4585 }
4586 p_cpo = save_cpo;
4587 break;
4588
4589 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4590 }
4591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Handle fourth level expression:
4604 * + number addition
4605 * - number subtraction
4606 * . string concatenation
4607 *
4608 * "arg" must point to the first non-white of the expression.
4609 * "arg" is advanced to the next non-white after the recognized expression.
4610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int evaluate;
4618{
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var2;
4620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int op;
4622 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623#ifdef FEAT_FLOAT
4624 float_T f1 = 0, f2 = 0;
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *s1, *s2;
4627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4628 char_u *p;
4629
4630 /*
4631 * Get the first variable.
4632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635
4636 /*
4637 * Repeat computing, until no '+', '-' or '.' is following.
4638 */
4639 for (;;)
4640 {
4641 op = **arg;
4642 if (op != '+' && op != '-' && op != '.')
4643 break;
4644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 if ((op != '+' || rettv->v_type != VAR_LIST)
4646#ifdef FEAT_FLOAT
4647 && (op == '.' || rettv->v_type != VAR_FLOAT)
4648#endif
4649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 {
4651 /* For "list + ...", an illegal use of the first operand as
4652 * a number cannot be determined before evaluating the 2nd
4653 * operand: if this is also a list, all is ok.
4654 * For "something . ...", "something - ..." or "non-list + ...",
4655 * we know that the first operand needs to be a string or number
4656 * without evaluating the 2nd operand. So check before to avoid
4657 * side effects after an error. */
4658 if (evaluate && get_tv_string_chk(rettv) == NULL)
4659 {
4660 clear_tv(rettv);
4661 return FAIL;
4662 }
4663 }
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * Get the second variable.
4667 */
4668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674
4675 if (evaluate)
4676 {
4677 /*
4678 * Compute the result.
4679 */
4680 if (op == '.')
4681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4683 s2 = get_tv_string_buf_chk(&var2, buf2);
4684 if (s2 == NULL) /* type error ? */
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 else if (op == '+' && rettv->v_type == VAR_LIST
4696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004697 {
4698 /* concatenate Lists */
4699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4700 &var3) == FAIL)
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
4706 clear_tv(rettv);
4707 *rettv = var3;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 int error = FALSE;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 f1 = rettv->vval.v_float;
4717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 else
4720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 n1 = get_tv_number_chk(rettv, &error);
4723 if (error)
4724 {
4725 /* This can only happen for "list + non-list". For
4726 * "non-list + ..." or "something - ...", we returned
4727 * before evaluating the 2nd operand. */
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 f1 = n1;
4734#endif
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 {
4739 f2 = var2.vval.v_float;
4740 n2 = 0;
4741 }
4742 else
4743#endif
4744 {
4745 n2 = get_tv_number_chk(&var2, &error);
4746 if (error)
4747 {
4748 clear_tv(rettv);
4749 clear_tv(&var2);
4750 return FAIL;
4751 }
4752#ifdef FEAT_FLOAT
4753 if (rettv->v_type == VAR_FLOAT)
4754 f2 = n2;
4755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758
4759#ifdef FEAT_FLOAT
4760 /* If there is a float on either side the result is a float. */
4761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4762 {
4763 if (op == '+')
4764 f1 = f1 + f2;
4765 else
4766 f1 = f1 - f2;
4767 rettv->v_type = VAR_FLOAT;
4768 rettv->vval.v_float = f1;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#endif
4772 {
4773 if (op == '+')
4774 n1 = n1 + n2;
4775 else
4776 n1 = n1 - n2;
4777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = n1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Handle fifth level expression:
4789 * * number multiplication
4790 * / number division
4791 * % number modulo
4792 *
4793 * "arg" must point to the first non-white of the expression.
4794 * "arg" is advanced to the next non-white after the recognized expression.
4795 *
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar33570922005-01-25 22:26:29 +00004805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 int op;
4807 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 int use_float = FALSE;
4810 float_T f1 = 0, f2;
4811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Get the first variable.
4816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819
4820 /*
4821 * Repeat computing, until no '*', '/' or '%' is following.
4822 */
4823 for (;;)
4824 {
4825 op = **arg;
4826 if (op != '*' && op != '/' && op != '%')
4827 break;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 {
4834 f1 = rettv->vval.v_float;
4835 use_float = TRUE;
4836 n1 = 0;
4837 }
4838 else
4839#endif
4840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (error)
4843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
4846 n1 = 0;
4847
4848 /*
4849 * Get the second variable.
4850 */
4851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return FAIL;
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (var2.v_type == VAR_FLOAT)
4859 {
4860 if (!use_float)
4861 {
4862 f1 = n1;
4863 use_float = TRUE;
4864 }
4865 f2 = var2.vval.v_float;
4866 n2 = 0;
4867 }
4868 else
4869#endif
4870 {
4871 n2 = get_tv_number_chk(&var2, &error);
4872 clear_tv(&var2);
4873 if (error)
4874 return FAIL;
4875#ifdef FEAT_FLOAT
4876 if (use_float)
4877 f2 = n2;
4878#endif
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 f1 = f1 * f2;
4890 else if (op == '/')
4891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# ifdef VMS
4893 /* VMS crashes on divide by zero, work around it */
4894 if (f2 == 0.0)
4895 {
4896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902 }
4903 else
4904 f1 = f1 / f2;
4905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 /* We rely on the floating point library to handle divide
4907 * by zero to result in "inf" and not a crash. */
4908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 return FAIL;
4915 }
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 if (op == '*')
4923 n1 = n1 * n2;
4924 else if (op == '/')
4925 {
4926 if (n2 == 0) /* give an error message? */
4927 {
4928 if (n1 == 0)
4929 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4930 else if (n1 < 0)
4931 n1 = -0x7fffffffL;
4932 else
4933 n1 = 0x7fffffffL;
4934 }
4935 else
4936 n1 = n1 / n2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 {
4940 if (n2 == 0) /* give an error message? */
4941 n1 = 0;
4942 else
4943 n1 = n1 % n2;
4944 }
4945 rettv->v_type = VAR_NUMBER;
4946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 return OK;
4952}
4953
4954/*
4955 * Handle sixth level expression:
4956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004957 * "string" string constant
4958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * &option-name option value
4960 * @r register contents
4961 * identifier variable value
4962 * function() function call
4963 * $VAR environment variable
4964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004965 * [expr, expr] List
4966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * Also handle:
4969 * ! in front logical NOT
4970 * - in front unary minus
4971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * trailing [] subscript in String or List
4973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * "arg" must point to the first non-white of the expression.
4976 * "arg" is advanced to the next non-white after the recognized expression.
4977 *
4978 * Return OK or FAIL.
4979 */
4980 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004985 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 long n;
4988 int len;
4989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *start_leader, *end_leader;
4991 int ret = OK;
4992 char_u *alias;
4993
4994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 /*
5001 * Skip '!' and '-' characters. They are handled later.
5002 */
5003 start_leader = *arg;
5004 while (**arg == '!' || **arg == '-' || **arg == '+')
5005 *arg = skipwhite(*arg + 1);
5006 end_leader = *arg;
5007
5008 switch (**arg)
5009 {
5010 /*
5011 * Number constant.
5012 */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
5020 case '7':
5021 case '8':
5022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 {
5024#ifdef FEAT_FLOAT
5025 char_u *p = skipdigits(*arg + 1);
5026 int get_float = FALSE;
5027
5028 /* We accept a float when the format matches
5029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005030 * strict to avoid backwards compatibility problems.
5031 * Don't look for a float after the "." operator, so that
5032 * ":let vers = 1.2.3" doesn't fail. */
5033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 get_float = TRUE;
5036 p = skipdigits(p + 2);
5037 if (*p == 'e' || *p == 'E')
5038 {
5039 ++p;
5040 if (*p == '-' || *p == '+')
5041 ++p;
5042 if (!vim_isdigit(*p))
5043 get_float = FALSE;
5044 else
5045 p = skipdigits(p + 1);
5046 }
5047 if (ASCII_ISALPHA(*p) || *p == '.')
5048 get_float = FALSE;
5049 }
5050 if (get_float)
5051 {
5052 float_T f;
5053
5054 *arg += string2float(*arg, &f);
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_FLOAT;
5058 rettv->vval.v_float = f;
5059 }
5060 }
5061 else
5062#endif
5063 {
5064 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5065 *arg += len;
5066 if (evaluate)
5067 {
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /*
5076 * String constant: "string".
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 break;
5086
5087 /*
5088 * List: [expr, expr]
5089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Dictionary: {key: val, key: val}
5095 */
5096 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5097 break;
5098
5099 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104
5105 /*
5106 * Environment variable: $VAR.
5107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Register contents: @r.
5113 */
5114 case '@': ++*arg;
5115 if (evaluate)
5116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005118 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (**arg != NUL)
5121 ++*arg;
5122 break;
5123
5124 /*
5125 * nested expression: (expression).
5126 */
5127 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (**arg == ')')
5130 ++*arg;
5131 else if (ret == OK)
5132 {
5133 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ret = FAIL;
5136 }
5137 break;
5138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142
5143 if (ret == NOTDONE)
5144 {
5145 /*
5146 * Must be a variable or function name.
5147 * Can also be a curly-braces kind of name: {expr}.
5148 */
5149 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 if (alias != NULL)
5152 s = alias;
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ret = FAIL;
5156 else
5157 {
5158 if (**arg == '(') /* recursive! */
5159 {
5160 /* If "s" is the name of a variable of type VAR_FUNC
5161 * use its contents. */
5162 s = deref_func_name(s, &len);
5163
5164 /* Invoke the function. */
5165 ret = get_func_tv(s, len, rettv, arg,
5166 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168
5169 /* If evaluate is FALSE rettv->v_type was not set in
5170 * get_func_tv, but it's needed in handle_subscript() to parse
5171 * what follows. So set it here. */
5172 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5173 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005174 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175 rettv->v_type = VAR_FUNC;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /* Stop the expression evaluation when immediately
5179 * aborting on error, or when an interrupt occurred or
5180 * an exception was thrown but not caught. */
5181 if (aborting())
5182 {
5183 if (ret == OK)
5184 clear_tv(rettv);
5185 ret = FAIL;
5186 }
5187 }
5188 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005189 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005190 else
5191 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005193 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 }
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *arg = skipwhite(*arg);
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5199 * expr(expr). */
5200 if (ret == OK)
5201 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5205 */
5206 if (ret == OK && evaluate && end_leader > start_leader)
5207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 int val = 0;
5210#ifdef FEAT_FLOAT
5211 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 if (rettv->v_type == VAR_FLOAT)
5214 f = rettv->vval.v_float;
5215 else
5216#endif
5217 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 clear_tv(rettv);
5221 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else
5224 {
5225 while (end_leader > start_leader)
5226 {
5227 --end_leader;
5228 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = !f;
5233 else
5234#endif
5235 val = !val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 {
5239#ifdef FEAT_FLOAT
5240 if (rettv->v_type == VAR_FLOAT)
5241 f = -f;
5242 else
5243#endif
5244 val = -val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 {
5250 clear_tv(rettv);
5251 rettv->vval.v_float = f;
5252 }
5253 else
5254#endif
5255 {
5256 clear_tv(rettv);
5257 rettv->v_type = VAR_NUMBER;
5258 rettv->vval.v_number = val;
5259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5268 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5270 */
5271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
5278 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 long len = -1;
5282 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005286 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 if (verbose)
5289 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
5291 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292#ifdef FEAT_FLOAT
5293 else if (rettv->v_type == VAR_FLOAT)
5294 {
5295 if (verbose)
5296 EMSG(_(e_float_as_string));
5297 return FAIL;
5298 }
5299#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * dict.name
5305 */
5306 key = *arg + 1;
5307 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5308 ;
5309 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 /*
5316 * something[idx]
5317 *
5318 * Get the (first) variable from inside the [].
5319 */
5320 *arg = skipwhite(*arg + 1);
5321 if (**arg == ':')
5322 empty1 = TRUE;
5323 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5324 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5326 {
5327 /* not a number or string */
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331
5332 /*
5333 * Get the second variable from inside the [:].
5334 */
5335 if (**arg == ':')
5336 {
5337 range = TRUE;
5338 *arg = skipwhite(*arg + 1);
5339 if (**arg == ']')
5340 empty2 = TRUE;
5341 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5348 {
5349 /* not a number or string */
5350 if (!empty1)
5351 clear_tv(&var1);
5352 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 return FAIL;
5354 }
5355 }
5356
5357 /* Check for the ']'. */
5358 if (**arg != ']')
5359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (verbose)
5361 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 clear_tv(&var1);
5363 if (range)
5364 clear_tv(&var2);
5365 return FAIL;
5366 }
5367 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369
5370 if (evaluate)
5371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 n1 = 0;
5373 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n1 = get_tv_number(&var1);
5376 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 if (range)
5379 {
5380 if (empty2)
5381 n2 = -1;
5382 else
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 n2 = get_tv_number(&var2);
5385 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 }
5388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
5391 case VAR_NUMBER:
5392 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 len = (long)STRLEN(s);
5395 if (range)
5396 {
5397 /* The resulting variable is a substring. If the indexes
5398 * are out of range the result is empty. */
5399 if (n1 < 0)
5400 {
5401 n1 = len + n1;
5402 if (n1 < 0)
5403 n1 = 0;
5404 }
5405 if (n2 < 0)
5406 n2 = len + n2;
5407 else if (n2 >= len)
5408 n2 = len;
5409 if (n1 >= len || n2 < 0 || n1 > n2)
5410 s = NULL;
5411 else
5412 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5413 }
5414 else
5415 {
5416 /* The resulting variable is a string of a single
5417 * character. If the index is too big or negative the
5418 * result is empty. */
5419 if (n1 >= len || n1 < 0)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, 1);
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 break;
5428
5429 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 if (n1 < 0)
5432 n1 = len + n1;
5433 if (!empty1 && (n1 < 0 || n1 >= len))
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 /* For a range we allow invalid values and return an empty
5436 * list. A list index out of range is an error. */
5437 if (!range)
5438 {
5439 if (verbose)
5440 EMSGN(_(e_listidx), n1);
5441 return FAIL;
5442 }
5443 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 if (range)
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 list_T *l;
5448 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449
5450 if (n2 < 0)
5451 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 else if (n2 >= len)
5453 n2 = len - 1;
5454 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 l = list_alloc();
5457 if (l == NULL)
5458 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 n1 <= n2; ++n1)
5461 {
5462 if (list_append_tv(l, &item->li_tv) == FAIL)
5463 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005464 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 return FAIL;
5466 }
5467 item = item->li_next;
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_LIST;
5471 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005472 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
5474 else
5475 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005476 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 case VAR_DICT:
5483 if (range)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
5494 if (len == -1)
5495 {
5496 key = get_tv_string(&var1);
5497 if (*key == NUL)
5498 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (verbose)
5500 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&var1);
5502 return FAIL;
5503 }
5504 }
5505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 if (len == -1)
5511 clear_tv(&var1);
5512 if (item == NULL)
5513 return FAIL;
5514
5515 copy_tv(&item->di_tv, &var1);
5516 clear_tv(rettv);
5517 *rettv = var1;
5518 }
5519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 }
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return OK;
5524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Get an option value.
5528 * "arg" points to the '&' or '+' before the option name.
5529 * "arg" is advanced to character after the option name.
5530 * Return OK or FAIL.
5531 */
5532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 int evaluate;
5537{
5538 char_u *option_end;
5539 long numval;
5540 char_u *stringval;
5541 int opt_type;
5542 int c;
5543 int working = (**arg == '+'); /* has("+option") */
5544 int ret = OK;
5545 int opt_flags;
5546
5547 /*
5548 * Isolate the option name and find its value.
5549 */
5550 option_end = find_option_end(arg, &opt_flags);
5551 if (option_end == NULL)
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 EMSG2(_("E112: Option name missing: %s"), *arg);
5555 return FAIL;
5556 }
5557
5558 if (!evaluate)
5559 {
5560 *arg = option_end;
5561 return OK;
5562 }
5563
5564 c = *option_end;
5565 *option_end = NUL;
5566 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (opt_type == -3) /* invalid name */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 EMSG2(_("E113: Unknown option: %s"), *arg);
5573 ret = FAIL;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (opt_type == -2) /* hidden string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == -1) /* hidden number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == 1) /* number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else /* string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
5598 else if (working && (opt_type == -2 || opt_type == -1))
5599 ret = FAIL;
5600
5601 *option_end = c; /* put back for error messages */
5602 *arg = option_end;
5603
5604 return ret;
5605}
5606
5607/*
5608 * Allocate a variable for a string constant.
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
5618 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int extra = 0;
5620
5621 /*
5622 * Find the end of the string, skipping backslashed characters.
5623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 if (*p == '\\' && p[1] != NUL)
5627 {
5628 ++p;
5629 /* A "\<x>" form occupies at least 4 characters, and produces up
5630 * to 6 characters: reserve space for 2 extra */
5631 if (*p == '<')
5632 extra += 2;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 if (*p != '"')
5637 {
5638 EMSG2(_("E114: Missing quote: %s"), *arg);
5639 return FAIL;
5640 }
5641
5642 /* If only parsing, set *arg and return here */
5643 if (!evaluate)
5644 {
5645 *arg = p + 1;
5646 return OK;
5647 }
5648
5649 /*
5650 * Copy the string into allocated memory, handling backslashed
5651 * characters.
5652 */
5653 name = alloc((unsigned)(p - *arg + extra));
5654 if (name == NULL)
5655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (*p == '\\')
5662 {
5663 switch (*++p)
5664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 case 'b': *name++ = BS; ++p; break;
5666 case 'e': *name++ = ESC; ++p; break;
5667 case 'f': *name++ = FF; ++p; break;
5668 case 'n': *name++ = NL; ++p; break;
5669 case 'r': *name++ = CAR; ++p; break;
5670 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 case 'X': /* hex: "\x1", "\x12" */
5673 case 'x':
5674 case 'u': /* Unicode: "\u0023" */
5675 case 'U':
5676 if (vim_isxdigit(p[1]))
5677 {
5678 int n, nr;
5679 int c = toupper(*p);
5680
5681 if (c == 'X')
5682 n = 2;
5683 else
5684 n = 4;
5685 nr = 0;
5686 while (--n >= 0 && vim_isxdigit(p[1]))
5687 {
5688 ++p;
5689 nr = (nr << 4) + hex2nr(*p);
5690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_MBYTE
5693 /* For "\u" store the number according to
5694 * 'encoding'. */
5695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702
5703 /* octal: "\1", "\12", "\123" */
5704 case '0':
5705 case '1':
5706 case '2':
5707 case '3':
5708 case '4':
5709 case '5':
5710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '7': *name = *p++ - '0';
5712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name = (*name << 3) + *p++ - '0';
5715 if (*p >= '0' && *p <= '7')
5716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720
5721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (extra != 0)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 /* FALLTHROUGH */
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 }
5734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 *arg = p + 1;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 int evaluate;
5753{
5754 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 int reduce = 0;
5757
5758 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 */
5761 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 break;
5767 ++reduce;
5768 ++p;
5769 }
5770 }
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 return FAIL;
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 if (!evaluate)
5780 {
5781 *arg = p + 1;
5782 return OK;
5783 }
5784
5785 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 */
5788 str = alloc((unsigned)((p - *arg) - reduce));
5789 if (str == NULL)
5790 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 break;
5800 ++p;
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 *arg = p + 1;
5806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Allocate a variable for a List and fill it from "*arg".
5812 * Return OK or FAIL.
5813 */
5814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int evaluate;
5819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l = NULL;
5821 typval_T tv;
5822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
5824 if (evaluate)
5825 {
5826 l = list_alloc();
5827 if (l == NULL)
5828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 while (**arg != ']' && **arg != NUL)
5833 {
5834 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5835 goto failret;
5836 if (evaluate)
5837 {
5838 item = listitem_alloc();
5839 if (item != NULL)
5840 {
5841 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 list_append(l, item);
5844 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005845 else
5846 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 }
5848
5849 if (**arg == ']')
5850 break;
5851 if (**arg != ',')
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 goto failret;
5855 }
5856 *arg = skipwhite(*arg + 1);
5857 }
5858
5859 if (**arg != ']')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862failret:
5863 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005864 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 if (evaluate)
5870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 rettv->v_type = VAR_LIST;
5872 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 ++l->lv_refcount;
5874 }
5875
5876 return OK;
5877}
5878
5879/*
5880 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005881 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005883 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_alloc()
5885{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 list_T *l;
5887
5888 l = (list_T *)alloc_clear(sizeof(list_T));
5889 if (l != NULL)
5890 {
5891 /* Prepend the list to the list of lists for garbage collection. */
5892 if (first_list != NULL)
5893 first_list->lv_used_prev = l;
5894 l->lv_used_prev = NULL;
5895 l->lv_used_next = first_list;
5896 first_list = l;
5897 }
5898 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005902 * Allocate an empty list for a return value.
5903 * Returns OK or FAIL.
5904 */
5905 static int
5906rettv_list_alloc(rettv)
5907 typval_T *rettv;
5908{
5909 list_T *l = list_alloc();
5910
5911 if (l == NULL)
5912 return FAIL;
5913
5914 rettv->vval.v_list = l;
5915 rettv->v_type = VAR_LIST;
5916 ++l->lv_refcount;
5917 return OK;
5918}
5919
5920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 * Unreference a list: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005924 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928 if (l != NULL && --l->lv_refcount <= 0)
5929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
5933 * Free a list, including all items it points to.
5934 * Ignores the reference count.
5935 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005936 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937list_free(l, recurse)
5938 list_T *l;
5939 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 /* Remove the list from the list of lists for garbage collection. */
5944 if (l->lv_used_prev == NULL)
5945 first_list = l->lv_used_next;
5946 else
5947 l->lv_used_prev->lv_used_next = l->lv_used_next;
5948 if (l->lv_used_next != NULL)
5949 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5950
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953 /* Remove the item before deleting it. */
5954 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 if (recurse || (item->li_tv.v_type != VAR_LIST
5956 && item->li_tv.v_type != VAR_DICT))
5957 clear_tv(&item->li_tv);
5958 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960 vim_free(l);
5961}
5962
5963/*
5964 * Allocate a list item.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_alloc()
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 vim_free(item);
5981}
5982
5983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005984 * Remove a list item from a List and free it. Also clears the value.
5985 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
5989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990{
5991 list_remove(l, item, item);
5992 listitem_free(item);
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Get the number of items in a list.
5997 */
5998 static long
5999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (l == NULL)
6003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 * Return TRUE when two lists have exactly the same values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l1;
6013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006019 if (l1 == NULL || l2 == NULL)
6020 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006021 if (l1 == l2)
6022 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 if (list_len(l1) != list_len(l2))
6024 return FALSE;
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026 for (item1 = l1->lv_first, item2 = l2->lv_first;
6027 item1 != NULL && item2 != NULL;
6028 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 return FALSE;
6031 return item1 == NULL && item2 == NULL;
6032}
6033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006034#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6035 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036/*
6037 * Return the dictitem that an entry in a hashtable points to.
6038 */
6039 dictitem_T *
6040dict_lookup(hi)
6041 hashitem_T *hi;
6042{
6043 return HI2DI(hi);
6044}
6045#endif
6046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 * Return TRUE when two dictionaries have exactly the same key/values.
6049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *d1;
6053 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 hashitem_T *hi;
6058 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006061 if (d1 == NULL || d2 == NULL)
6062 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (d1 == d2)
6064 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 if (dict_len(d1) != dict_len(d2))
6066 return FALSE;
6067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 item2 = dict_find(d2, hi->hi_key, -1);
6074 if (item2 == NULL)
6075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return FALSE;
6078 --todo;
6079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 }
6081 return TRUE;
6082}
6083
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084static int tv_equal_recurse_limit;
6085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 */
6091 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T *tv1;
6094 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 int ic; /* ignore case */
6096 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
6098 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006101 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 * recursiveness to a limit. We guess they are equal then.
6108 * A fixed limit has the problem of still taking an awful long time.
6109 * Reduce the limit every time running into it. That should work fine for
6110 * deeply linked structures that are not recursively linked and catch
6111 * recursiveness quickly. */
6112 if (!recursive)
6113 tv_equal_recurse_limit = 1000;
6114 if (recursive_cnt >= tv_equal_recurse_limit)
6115 {
6116 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 ++recursive_cnt;
6124 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6125 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006126 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_FUNC:
6135 return (tv1->vval.v_string != NULL
6136 && tv2->vval.v_string != NULL
6137 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6138
6139 case VAR_NUMBER:
6140 return tv1->vval.v_number == tv2->vval.v_number;
6141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142#ifdef FEAT_FLOAT
6143 case VAR_FLOAT:
6144 return tv1->vval.v_float == tv2->vval.v_float;
6145#endif
6146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 case VAR_STRING:
6148 s1 = get_tv_string_buf(tv1, buf1);
6149 s2 = get_tv_string_buf(tv2, buf2);
6150 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152
6153 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154 return TRUE;
6155}
6156
6157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 * Locate item with index "n" in list "l" and return it.
6159 * A negative index is counted from the end; -1 is the last item.
6160 * Returns NULL when "n" is out of range.
6161 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 long n;
6166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 long idx;
6169
6170 if (l == NULL)
6171 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172
6173 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 n = l->lv_len + n;
6176
6177 /* Check for index out of range. */
6178 if (n < 0 || n >= l->lv_len)
6179 return NULL;
6180
6181 /* When there is a cached index may start search from there. */
6182 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_idx / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else if (n > (l->lv_idx + l->lv_len) / 2)
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
6196 else
6197 {
6198 /* closest to the cached index */
6199 item = l->lv_idx_item;
6200 idx = l->lv_idx;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
6203 else
6204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 if (n < l->lv_len / 2)
6206 {
6207 /* closest to the start of the list */
6208 item = l->lv_first;
6209 idx = 0;
6210 }
6211 else
6212 {
6213 /* closest to the end of the list */
6214 item = l->lv_last;
6215 idx = l->lv_len - 1;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 while (n > idx)
6220 {
6221 /* search forward */
6222 item = item->li_next;
6223 ++idx;
6224 }
6225 while (n < idx)
6226 {
6227 /* search backward */
6228 item = item->li_prev;
6229 --idx;
6230 }
6231
6232 /* cache the used index */
6233 l->lv_idx = idx;
6234 l->lv_idx_item = item;
6235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 return item;
6237}
6238
6239/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006240 * Get list item "l[idx]" as a number.
6241 */
6242 static long
6243list_find_nr(l, idx, errorp)
6244 list_T *l;
6245 long idx;
6246 int *errorp; /* set to TRUE when something wrong */
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx);
6251 if (li == NULL)
6252 {
6253 if (errorp != NULL)
6254 *errorp = TRUE;
6255 return -1L;
6256 }
6257 return get_tv_number_chk(&li->li_tv, errorp);
6258}
6259
6260/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6262 */
6263 char_u *
6264list_find_str(l, idx)
6265 list_T *l;
6266 long idx;
6267{
6268 listitem_T *li;
6269
6270 li = list_find(l, idx - 1);
6271 if (li == NULL)
6272 {
6273 EMSGN(_(e_listidx), idx);
6274 return NULL;
6275 }
6276 return get_tv_string(&li->li_tv);
6277}
6278
6279/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280 * Locate "item" list "l" and return its index.
6281 * Returns -1 when "item" is not in the list.
6282 */
6283 static long
6284list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287{
6288 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290
6291 if (l == NULL)
6292 return -1;
6293 idx = 0;
6294 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6295 ++idx;
6296 if (li == NULL)
6297 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006298 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006299}
6300
6301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 * Append item "item" to the end of list "l".
6303 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l;
6307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308{
6309 if (l->lv_last == NULL)
6310 {
6311 /* empty list */
6312 l->lv_first = item;
6313 l->lv_last = item;
6314 item->li_prev = NULL;
6315 }
6316 else
6317 {
6318 l->lv_last->li_next = item;
6319 item->li_prev = l->lv_last;
6320 l->lv_last = item;
6321 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 item->li_next = NULL;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006330 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
6333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 copy_tv(tv, &li->li_tv);
6340 list_append(l, li);
6341 return OK;
6342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 * Return FAIL when out of memory.
6347 */
6348 int
6349list_append_dict(list, dict)
6350 list_T *list;
6351 dict_T *dict;
6352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_DICT;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_dict = dict;
6360 list_append(list, li);
6361 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return OK;
6363}
6364
6365/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 * Returns FAIL when out of memory.
6369 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 list_T *l;
6373 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375{
6376 listitem_T *li = listitem_alloc();
6377
6378 if (li == NULL)
6379 return FAIL;
6380 list_append(l, li);
6381 li->li_tv.v_type = VAR_STRING;
6382 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006383 if (str == NULL)
6384 li->li_tv.vval.v_string = NULL;
6385 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 return FAIL;
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006392 * Append "n" to list "l".
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_append_number(l, n)
6397 list_T *l;
6398 varnumber_T n;
6399{
6400 listitem_T *li;
6401
6402 li = listitem_alloc();
6403 if (li == NULL)
6404 return FAIL;
6405 li->li_tv.v_type = VAR_NUMBER;
6406 li->li_tv.v_lock = 0;
6407 li->li_tv.vval.v_number = n;
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * If "item" is NULL append at the end.
6415 * Return FAIL when out of memory.
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l;
6420 typval_T *tv;
6421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
6425 if (ni == NULL)
6426 return FAIL;
6427 copy_tv(tv, &ni->li_tv);
6428 if (item == NULL)
6429 /* Append new item at end of list. */
6430 list_append(l, ni);
6431 else
6432 {
6433 /* Insert new item before existing item. */
6434 ni->li_prev = item->li_prev;
6435 ni->li_next = item;
6436 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006437 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439 ++l->lv_idx;
6440 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006444 l->lv_idx_item = NULL;
6445 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006447 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 }
6449 return OK;
6450}
6451
6452/*
6453 * Extend "l1" with "l2".
6454 * If "bef" is NULL append at the end, otherwise insert before this item.
6455 * Returns FAIL when out of memory.
6456 */
6457 static int
6458list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 list_T *l1;
6460 list_T *l2;
6461 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462{
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006464 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006466 /* We also quit the loop when we have inserted the original item count of
6467 * the list, avoid a hang when we extend a list with itself. */
6468 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6470 return FAIL;
6471 return OK;
6472}
6473
6474/*
6475 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6476 * Return FAIL when out of memory.
6477 */
6478 static int
6479list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006480 list_T *l1;
6481 list_T *l2;
6482 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483{
Bram Moolenaar33570922005-01-25 22:26:29 +00006484 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006486 if (l1 == NULL || l2 == NULL)
6487 return FAIL;
6488
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491 if (l == NULL)
6492 return FAIL;
6493 tv->v_type = VAR_LIST;
6494 tv->vval.v_list = l;
6495
6496 /* append all items from the second list */
6497 return list_extend(l, l2, NULL);
6498}
6499
6500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 * Returns NULL when out of memory.
6505 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511{
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 list_T *copy;
6513 listitem_T *item;
6514 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515
6516 if (orig == NULL)
6517 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518
6519 copy = list_alloc();
6520 if (copy != NULL)
6521 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 if (copyID != 0)
6523 {
6524 /* Do this before adding the items, because one of the items may
6525 * refer back to this list. */
6526 orig->lv_copyID = copyID;
6527 orig->lv_copylist = copy;
6528 }
6529 for (item = orig->lv_first; item != NULL && !got_int;
6530 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 {
6532 ni = listitem_alloc();
6533 if (ni == NULL)
6534 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 {
6537 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6538 {
6539 vim_free(ni);
6540 break;
6541 }
6542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006544 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006545 list_append(copy, ni);
6546 }
6547 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006548 if (item != NULL)
6549 {
6550 list_unref(copy);
6551 copy = NULL;
6552 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006553 }
6554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555 return copy;
6556}
6557
6558/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006560 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006562 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006563list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006564 list_T *l;
6565 listitem_T *item;
6566 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567{
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570 /* notify watchers */
6571 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006573 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 list_fix_watch(l, ip);
6575 if (ip == item2)
6576 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578
6579 if (item2->li_next == NULL)
6580 l->lv_last = item->li_prev;
6581 else
6582 item2->li_next->li_prev = item->li_prev;
6583 if (item->li_prev == NULL)
6584 l->lv_first = item2->li_next;
6585 else
6586 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006587 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588}
6589
6590/*
6591 * Return an allocated string with the string representation of a list.
6592 * May return NULL.
6593 */
6594 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006595list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006596 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006597 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598{
6599 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
6601 if (tv->vval.v_list == NULL)
6602 return NULL;
6603 ga_init2(&ga, (int)sizeof(char), 80);
6604 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006605 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 {
6607 vim_free(ga.ga_data);
6608 return NULL;
6609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 ga_append(&ga, ']');
6611 ga_append(&ga, NUL);
6612 return (char_u *)ga.ga_data;
6613}
6614
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006615typedef struct join_S {
6616 char_u *s;
6617 char_u *tofree;
6618} join_T;
6619
6620 static int
6621list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6622 garray_T *gap; /* to store the result in */
6623 list_T *l;
6624 char_u *sep;
6625 int echo_style;
6626 int copyID;
6627 garray_T *join_gap; /* to keep each list item string */
6628{
6629 int i;
6630 join_T *p;
6631 int len;
6632 int sumlen = 0;
6633 int first = TRUE;
6634 char_u *tofree;
6635 char_u numbuf[NUMBUFLEN];
6636 listitem_T *item;
6637 char_u *s;
6638
6639 /* Stringify each item in the list. */
6640 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6641 {
6642 if (echo_style)
6643 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6644 else
6645 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6646 if (s == NULL)
6647 return FAIL;
6648
6649 len = (int)STRLEN(s);
6650 sumlen += len;
6651
6652 ga_grow(join_gap, 1);
6653 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6654 if (tofree != NULL || s != numbuf)
6655 {
6656 p->s = s;
6657 p->tofree = tofree;
6658 }
6659 else
6660 {
6661 p->s = vim_strnsave(s, len);
6662 p->tofree = p->s;
6663 }
6664
6665 line_breakcheck();
6666 }
6667
6668 /* Allocate result buffer with its total size, avoid re-allocation and
6669 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6670 if (join_gap->ga_len >= 2)
6671 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6672 if (ga_grow(gap, sumlen + 2) == FAIL)
6673 return FAIL;
6674
6675 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6676 {
6677 if (first)
6678 first = FALSE;
6679 else
6680 ga_concat(gap, sep);
6681 p = ((join_T *)join_gap->ga_data) + i;
6682
6683 if (p->s != NULL)
6684 ga_concat(gap, p->s);
6685 line_breakcheck();
6686 }
6687
6688 return OK;
6689}
6690
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006693 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006694 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006695 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006696 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006697list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006698 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006699 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006700 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006701 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006702 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 garray_T join_ga;
6705 int retval;
6706 join_T *p;
6707 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006708
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006709 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6710 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6711
6712 /* Dispose each item in join_ga. */
6713 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006715 p = (join_T *)join_ga.ga_data;
6716 for (i = 0; i < join_ga.ga_len; ++i)
6717 {
6718 vim_free(p->tofree);
6719 ++p;
6720 }
6721 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723
6724 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725}
6726
6727/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 * Garbage collection for lists and dictionaries.
6729 *
6730 * We use reference counts to be able to free most items right away when they
6731 * are no longer used. But for composite items it's possible that it becomes
6732 * unused while the reference count is > 0: When there is a recursive
6733 * reference. Example:
6734 * :let l = [1, 2, 3]
6735 * :let d = {9: l}
6736 * :let l[1] = d
6737 *
6738 * Since this is quite unusual we handle this with garbage collection: every
6739 * once in a while find out which lists and dicts are not referenced from any
6740 * variable.
6741 *
6742 * Here is a good reference text about garbage collection (refers to Python
6743 * but it applies to all reference-counting mechanisms):
6744 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006745 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746
6747/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 * Do garbage collection for lists and dicts.
6749 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006750 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006751 int
6752garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006753{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006754 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006755 buf_T *buf;
6756 win_T *wp;
6757 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006758 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006759 int did_free;
6760 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761#ifdef FEAT_WINDOWS
6762 tabpage_T *tp;
6763#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006765 /* Only do this once. */
6766 want_garbage_collect = FALSE;
6767 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006768 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006769
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770 /* We advance by two because we add one for items referenced through
6771 * previous_funccal. */
6772 current_copyID += COPYID_INC;
6773 copyID = current_copyID;
6774
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 /*
6776 * 1. Go through all accessible variables and mark all lists and dicts
6777 * with copyID.
6778 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006779
6780 /* Don't free variables in the previous_funccal list unless they are only
6781 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006782 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006783 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6784 {
6785 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6786 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6787 }
6788
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789 /* script-local variables */
6790 for (i = 1; i <= ga_scripts.ga_len; ++i)
6791 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6792
6793 /* buffer-local variables */
6794 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006795 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796
6797 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006798 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006799 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006800#ifdef FEAT_AUTOCMD
6801 if (aucmd_win != NULL)
6802 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6803#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006805#ifdef FEAT_WINDOWS
6806 /* tabpage-local variables */
6807 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006808 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006809#endif
6810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 /* global variables */
6812 set_ref_in_ht(&globvarht, copyID);
6813
6814 /* function-local variables */
6815 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6816 {
6817 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6818 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6819 }
6820
Bram Moolenaard812df62008-11-09 12:46:09 +00006821 /* v: vars */
6822 set_ref_in_ht(&vimvarht, copyID);
6823
Bram Moolenaar1dced572012-04-05 16:54:08 +02006824#ifdef FEAT_LUA
6825 set_ref_in_lua(copyID);
6826#endif
6827
Bram Moolenaardb913952012-06-29 12:54:53 +02006828#ifdef FEAT_PYTHON
6829 set_ref_in_python(copyID);
6830#endif
6831
6832#ifdef FEAT_PYTHON3
6833 set_ref_in_python3(copyID);
6834#endif
6835
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006836 /*
6837 * 2. Free lists and dictionaries that are not referenced.
6838 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006839 did_free = free_unref_items(copyID);
6840
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006841 /*
6842 * 3. Check if any funccal can be freed now.
6843 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 for (pfc = &previous_funccal; *pfc != NULL; )
6845 {
6846 if (can_free_funccal(*pfc, copyID))
6847 {
6848 fc = *pfc;
6849 *pfc = fc->caller;
6850 free_funccal(fc, TRUE);
6851 did_free = TRUE;
6852 did_free_funccal = TRUE;
6853 }
6854 else
6855 pfc = &(*pfc)->caller;
6856 }
6857 if (did_free_funccal)
6858 /* When a funccal was freed some more items might be garbage
6859 * collected, so run again. */
6860 (void)garbage_collect();
6861
6862 return did_free;
6863}
6864
6865/*
6866 * Free lists and dictionaries that are no longer referenced.
6867 */
6868 static int
6869free_unref_items(copyID)
6870 int copyID;
6871{
6872 dict_T *dd;
6873 list_T *ll;
6874 int did_free = FALSE;
6875
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 */
6879 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006882 /* Free the Dictionary and ordinary items it contains, but don't
6883 * recurse into Lists and Dictionaries, they will be in the list
6884 * of dicts or list of lists. */
6885 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 did_free = TRUE;
6887
6888 /* restart, next dict may also have been freed */
6889 dd = first_dict;
6890 }
6891 else
6892 dd = dd->dv_used_next;
6893
6894 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 * Go through the list of lists and free items without the copyID.
6896 * But don't free a list that has a watcher (used in a for loop), these
6897 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 */
6899 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6901 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006903 /* Free the List and ordinary items it contains, but don't recurse
6904 * into Lists and Dictionaries, they will be in the list of dicts
6905 * or list of lists. */
6906 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 did_free = TRUE;
6908
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006909 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 ll = first_list;
6911 }
6912 else
6913 ll = ll->lv_used_next;
6914
6915 return did_free;
6916}
6917
6918/*
6919 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6920 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006921 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922set_ref_in_ht(ht, copyID)
6923 hashtab_T *ht;
6924 int copyID;
6925{
6926 int todo;
6927 hashitem_T *hi;
6928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006929 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930 for (hi = ht->ht_array; todo > 0; ++hi)
6931 if (!HASHITEM_EMPTY(hi))
6932 {
6933 --todo;
6934 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6935 }
6936}
6937
6938/*
6939 * Mark all lists and dicts referenced through list "l" with "copyID".
6940 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006941 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942set_ref_in_list(l, copyID)
6943 list_T *l;
6944 int copyID;
6945{
6946 listitem_T *li;
6947
6948 for (li = l->lv_first; li != NULL; li = li->li_next)
6949 set_ref_in_item(&li->li_tv, copyID);
6950}
6951
6952/*
6953 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6954 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006955 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956set_ref_in_item(tv, copyID)
6957 typval_T *tv;
6958 int copyID;
6959{
6960 dict_T *dd;
6961 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962
6963 switch (tv->v_type)
6964 {
6965 case VAR_DICT:
6966 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006967 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006968 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /* Didn't see this dict yet. */
6970 dd->dv_copyID = copyID;
6971 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006974
6975 case VAR_LIST:
6976 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006977 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006978 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /* Didn't see this list yet. */
6980 ll->lv_copyID = copyID;
6981 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006982 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006984 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006986}
6987
6988/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006989 * Allocate an empty header for a dictionary.
6990 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006991 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006992dict_alloc()
6993{
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995
Bram Moolenaar33570922005-01-25 22:26:29 +00006996 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006999 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 if (first_dict != NULL)
7001 first_dict->dv_used_prev = d;
7002 d->dv_used_next = first_dict;
7003 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007007 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007008 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007009 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007010 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007011 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013}
7014
7015/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007016 * Allocate an empty dict for a return value.
7017 * Returns OK or FAIL.
7018 */
7019 static int
7020rettv_dict_alloc(rettv)
7021 typval_T *rettv;
7022{
7023 dict_T *d = dict_alloc();
7024
7025 if (d == NULL)
7026 return FAIL;
7027
7028 rettv->vval.v_dict = d;
7029 rettv->v_type = VAR_DICT;
7030 ++d->dv_refcount;
7031 return OK;
7032}
7033
7034
7035/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036 * Unreference a Dictionary: decrement the reference count and free it when it
7037 * becomes zero.
7038 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007039 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007043 if (d != NULL && --d->dv_refcount <= 0)
7044 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045}
7046
7047/*
7048 * Free a Dictionary, including all items it contains.
7049 * Ignores the reference count.
7050 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007051 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052dict_free(d, recurse)
7053 dict_T *d;
7054 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007057 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 /* Remove the dict from the list of dicts for garbage collection. */
7061 if (d->dv_used_prev == NULL)
7062 first_dict = d->dv_used_next;
7063 else
7064 d->dv_used_prev->dv_used_next = d->dv_used_next;
7065 if (d->dv_used_next != NULL)
7066 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7067
7068 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007069 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007070 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007073 if (!HASHITEM_EMPTY(hi))
7074 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007075 /* Remove the item before deleting it, just in case there is
7076 * something recursive causing trouble. */
7077 di = HI2DI(hi);
7078 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007079 if (recurse || (di->di_tv.v_type != VAR_LIST
7080 && di->di_tv.v_type != VAR_DICT))
7081 clear_tv(&di->di_tv);
7082 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 --todo;
7084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007085 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 vim_free(d);
7088}
7089
7090/*
7091 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 * The "key" is copied to the new item.
7093 * Note that the value of the item "di_tv" still needs to be initialized!
7094 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007096 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097dictitem_alloc(key)
7098 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099{
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007102 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007103 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007106 di->di_flags = 0;
7107 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007108 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109}
7110
7111/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 * Make a copy of a Dictionary item.
7113 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007114 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117{
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007120 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7121 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007122 if (di != NULL)
7123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126 copy_tv(&org->di_tv, &di->di_tv);
7127 }
7128 return di;
7129}
7130
7131/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 * Remove item "item" from Dictionary "dict" and free it.
7133 */
7134 static void
7135dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 dict_T *dict;
7137 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138{
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007140
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007142 if (HASHITEM_EMPTY(hi))
7143 EMSG2(_(e_intern2), "dictitem_remove()");
7144 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 dictitem_free(item);
7147}
7148
7149/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150 * Free a dict item. Also clears the value.
7151 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007152 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007155{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007156 clear_tv(&item->di_tv);
7157 vim_free(item);
7158}
7159
7160/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7162 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007163 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164 * Returns NULL when out of memory.
7165 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007167dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007170 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171{
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 dict_T *copy;
7173 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176
7177 if (orig == NULL)
7178 return NULL;
7179
7180 copy = dict_alloc();
7181 if (copy != NULL)
7182 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007183 if (copyID != 0)
7184 {
7185 orig->dv_copyID = copyID;
7186 orig->dv_copydict = copy;
7187 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007188 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193 --todo;
7194
7195 di = dictitem_alloc(hi->hi_key);
7196 if (di == NULL)
7197 break;
7198 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007199 {
7200 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7201 copyID) == FAIL)
7202 {
7203 vim_free(di);
7204 break;
7205 }
7206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 else
7208 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7209 if (dict_add(copy, di) == FAIL)
7210 {
7211 dictitem_free(di);
7212 break;
7213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216
Bram Moolenaare9a41262005-01-15 22:18:47 +00007217 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 if (todo > 0)
7219 {
7220 dict_unref(copy);
7221 copy = NULL;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
7224
7225 return copy;
7226}
7227
7228/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007230 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007232 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 dict_T *d;
7235 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236{
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238}
7239
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007241 * Add a number or string entry to dictionary "d".
7242 * When "str" is NULL use number "nr", otherwise use "str".
7243 * Returns FAIL when out of memory and when key already exists.
7244 */
7245 int
7246dict_add_nr_str(d, key, nr, str)
7247 dict_T *d;
7248 char *key;
7249 long nr;
7250 char_u *str;
7251{
7252 dictitem_T *item;
7253
7254 item = dictitem_alloc((char_u *)key);
7255 if (item == NULL)
7256 return FAIL;
7257 item->di_tv.v_lock = 0;
7258 if (str == NULL)
7259 {
7260 item->di_tv.v_type = VAR_NUMBER;
7261 item->di_tv.vval.v_number = nr;
7262 }
7263 else
7264 {
7265 item->di_tv.v_type = VAR_STRING;
7266 item->di_tv.vval.v_string = vim_strsave(str);
7267 }
7268 if (dict_add(d, item) == FAIL)
7269 {
7270 dictitem_free(item);
7271 return FAIL;
7272 }
7273 return OK;
7274}
7275
7276/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007277 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007278 * Returns FAIL when out of memory and when key already exists.
7279 */
7280 int
7281dict_add_list(d, key, list)
7282 dict_T *d;
7283 char *key;
7284 list_T *list;
7285{
7286 dictitem_T *item;
7287
7288 item = dictitem_alloc((char_u *)key);
7289 if (item == NULL)
7290 return FAIL;
7291 item->di_tv.v_lock = 0;
7292 item->di_tv.v_type = VAR_LIST;
7293 item->di_tv.vval.v_list = list;
7294 if (dict_add(d, item) == FAIL)
7295 {
7296 dictitem_free(item);
7297 return FAIL;
7298 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007299 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007300 return OK;
7301}
7302
7303/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 * Get the number of items in a Dictionary.
7305 */
7306 static long
7307dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 if (d == NULL)
7311 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007312 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313}
7314
7315/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 * Find item "key[len]" in Dictionary "d".
7317 * If "len" is negative use strlen(key).
7318 * Returns NULL when not found.
7319 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007320 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323 char_u *key;
7324 int len;
7325{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007326#define AKEYLEN 200
7327 char_u buf[AKEYLEN];
7328 char_u *akey;
7329 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 if (len < 0)
7333 akey = key;
7334 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007335 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 tofree = akey = vim_strnsave(key, len);
7337 if (akey == NULL)
7338 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007339 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 else
7341 {
7342 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007343 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 akey = buf;
7345 }
7346
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 vim_free(tofree);
7349 if (HASHITEM_EMPTY(hi))
7350 return NULL;
7351 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352}
7353
7354/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007355 * Get a string item from a dictionary.
7356 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007357 * Returns NULL if the entry doesn't exist or out of memory.
7358 */
7359 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007360get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007361 dict_T *d;
7362 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007363 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007364{
7365 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007366 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007367
7368 di = dict_find(d, key, -1);
7369 if (di == NULL)
7370 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007371 s = get_tv_string(&di->di_tv);
7372 if (save && s != NULL)
7373 s = vim_strsave(s);
7374 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007375}
7376
7377/*
7378 * Get a number item from a dictionary.
7379 * Returns 0 if the entry doesn't exist or out of memory.
7380 */
7381 long
7382get_dict_number(d, key)
7383 dict_T *d;
7384 char_u *key;
7385{
7386 dictitem_T *di;
7387
7388 di = dict_find(d, key, -1);
7389 if (di == NULL)
7390 return 0;
7391 return get_tv_number(&di->di_tv);
7392}
7393
7394/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395 * Return an allocated string with the string representation of a Dictionary.
7396 * May return NULL.
7397 */
7398 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007399dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007401 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402{
7403 garray_T ga;
7404 int first = TRUE;
7405 char_u *tofree;
7406 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 return NULL;
7414 ga_init2(&ga, (int)sizeof(char), 80);
7415 ga_append(&ga, '{');
7416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007417 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007420 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007422 --todo;
7423
7424 if (first)
7425 first = FALSE;
7426 else
7427 ga_concat(&ga, (char_u *)", ");
7428
7429 tofree = string_quote(hi->hi_key, FALSE);
7430 if (tofree != NULL)
7431 {
7432 ga_concat(&ga, tofree);
7433 vim_free(tofree);
7434 }
7435 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007436 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 if (s != NULL)
7438 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007440 if (s == NULL)
7441 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 if (todo > 0)
7445 {
7446 vim_free(ga.ga_data);
7447 return NULL;
7448 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449
7450 ga_append(&ga, '}');
7451 ga_append(&ga, NUL);
7452 return (char_u *)ga.ga_data;
7453}
7454
7455/*
7456 * Allocate a variable for a Dictionary and fill it from "*arg".
7457 * Return OK or FAIL. Returns NOTDONE for {expr}.
7458 */
7459 static int
7460get_dict_tv(arg, rettv, evaluate)
7461 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 int evaluate;
7464{
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 dict_T *d = NULL;
7466 typval_T tvkey;
7467 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007468 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472
7473 /*
7474 * First check if it's not a curly-braces thing: {expr}.
7475 * Must do this without evaluating, otherwise a function may be called
7476 * twice. Unfortunately this means we need to call eval1() twice for the
7477 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 if (*start != '}')
7481 {
7482 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7483 return FAIL;
7484 if (*start == '}')
7485 return NOTDONE;
7486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 if (evaluate)
7489 {
7490 d = dict_alloc();
7491 if (d == NULL)
7492 return FAIL;
7493 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 tvkey.v_type = VAR_UNKNOWN;
7495 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
7497 *arg = skipwhite(*arg + 1);
7498 while (**arg != '}' && **arg != NUL)
7499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 goto failret;
7502 if (**arg != ':')
7503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007504 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506 goto failret;
7507 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007508 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007510 key = get_tv_string_buf_chk(&tvkey, buf);
7511 if (key == NULL || *key == NUL)
7512 {
7513 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7514 if (key != NULL)
7515 EMSG(_(e_emptykey));
7516 clear_tv(&tvkey);
7517 goto failret;
7518 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520
7521 *arg = skipwhite(*arg + 1);
7522 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7523 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007524 if (evaluate)
7525 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 goto failret;
7527 }
7528 if (evaluate)
7529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 if (item != NULL)
7532 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007533 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 clear_tv(&tv);
7536 goto failret;
7537 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007538 item = dictitem_alloc(key);
7539 clear_tv(&tvkey);
7540 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007543 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 if (dict_add(d, item) == FAIL)
7545 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 }
7547 }
7548
7549 if (**arg == '}')
7550 break;
7551 if (**arg != ',')
7552 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007553 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 goto failret;
7555 }
7556 *arg = skipwhite(*arg + 1);
7557 }
7558
7559 if (**arg != '}')
7560 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007561 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562failret:
7563 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007564 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 return FAIL;
7566 }
7567
7568 *arg = skipwhite(*arg + 1);
7569 if (evaluate)
7570 {
7571 rettv->v_type = VAR_DICT;
7572 rettv->vval.v_dict = d;
7573 ++d->dv_refcount;
7574 }
7575
7576 return OK;
7577}
7578
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007580 * Return a string with the string representation of a variable.
7581 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007582 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007583 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007584 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007585 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586 */
7587 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007590 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007592 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594 static int recurse = 0;
7595 char_u *r = NULL;
7596
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007599 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 *tofree = NULL;
7601 return NULL;
7602 }
7603 ++recurse;
7604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 switch (tv->v_type)
7606 {
7607 case VAR_FUNC:
7608 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 r = tv->vval.v_string;
7610 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007611
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 if (tv->vval.v_list == NULL)
7614 {
7615 *tofree = NULL;
7616 r = NULL;
7617 }
7618 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7619 {
7620 *tofree = NULL;
7621 r = (char_u *)"[...]";
7622 }
7623 else
7624 {
7625 tv->vval.v_list->lv_copyID = copyID;
7626 *tofree = list2string(tv, copyID);
7627 r = *tofree;
7628 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007630
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632 if (tv->vval.v_dict == NULL)
7633 {
7634 *tofree = NULL;
7635 r = NULL;
7636 }
7637 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7638 {
7639 *tofree = NULL;
7640 r = (char_u *)"{...}";
7641 }
7642 else
7643 {
7644 tv->vval.v_dict->dv_copyID = copyID;
7645 *tofree = dict2string(tv, copyID);
7646 r = *tofree;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 case VAR_STRING:
7651 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 *tofree = NULL;
7653 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007654 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007656#ifdef FEAT_FLOAT
7657 case VAR_FLOAT:
7658 *tofree = NULL;
7659 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7660 r = numbuf;
7661 break;
7662#endif
7663
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007665 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668
7669 --recurse;
7670 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671}
7672
7673/*
7674 * Return a string with the string representation of a variable.
7675 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7676 * "numbuf" is used for a number.
7677 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007678 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 */
7680 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007681tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007683 char_u **tofree;
7684 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007685 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686{
7687 switch (tv->v_type)
7688 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 case VAR_FUNC:
7690 *tofree = string_quote(tv->vval.v_string, TRUE);
7691 return *tofree;
7692 case VAR_STRING:
7693 *tofree = string_quote(tv->vval.v_string, FALSE);
7694 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007695#ifdef FEAT_FLOAT
7696 case VAR_FLOAT:
7697 *tofree = NULL;
7698 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7699 return numbuf;
7700#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007702 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007704 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007707 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007708 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007709}
7710
7711/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 * Return string "str" in ' quotes, doubling ' characters.
7713 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 */
7716 static char_u *
7717string_quote(str, function)
7718 char_u *str;
7719 int function;
7720{
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007722 char_u *p, *r, *s;
7723
Bram Moolenaar33570922005-01-25 22:26:29 +00007724 len = (function ? 13 : 3);
7725 if (str != NULL)
7726 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007727 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 for (p = str; *p != NUL; mb_ptr_adv(p))
7729 if (*p == '\'')
7730 ++len;
7731 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007732 s = r = alloc(len);
7733 if (r != NULL)
7734 {
7735 if (function)
7736 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 r += 10;
7739 }
7740 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 if (str != NULL)
7743 for (p = str; *p != NUL; )
7744 {
7745 if (*p == '\'')
7746 *r++ = '\'';
7747 MB_COPY_CHAR(p, r);
7748 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007750 if (function)
7751 *r++ = ')';
7752 *r++ = NUL;
7753 }
7754 return s;
7755}
7756
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007757#ifdef FEAT_FLOAT
7758/*
7759 * Convert the string "text" to a floating point number.
7760 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7761 * this always uses a decimal point.
7762 * Returns the length of the text that was consumed.
7763 */
7764 static int
7765string2float(text, value)
7766 char_u *text;
7767 float_T *value; /* result stored here */
7768{
7769 char *s = (char *)text;
7770 float_T f;
7771
7772 f = strtod(s, &s);
7773 *value = f;
7774 return (int)((char_u *)s - text);
7775}
7776#endif
7777
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 * Get the value of an environment variable.
7780 * "arg" is pointing to the '$'. It is advanced to after the name.
7781 * If the environment variable was not set, silently assume it is empty.
7782 * Always return OK.
7783 */
7784 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007785get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 int evaluate;
7789{
7790 char_u *string = NULL;
7791 int len;
7792 int cc;
7793 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795
7796 ++*arg;
7797 name = *arg;
7798 len = get_env_len(arg);
7799 if (evaluate)
7800 {
7801 if (len != 0)
7802 {
7803 cc = name[len];
7804 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007805 /* first try vim_getenv(), fast for normal environment vars */
7806 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007808 {
7809 if (!mustfree)
7810 string = vim_strsave(string);
7811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 else
7813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007814 if (mustfree)
7815 vim_free(string);
7816
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 /* next try expanding things like $VIM and ${HOME} */
7818 string = expand_env_save(name - 1);
7819 if (string != NULL && *string == '$')
7820 {
7821 vim_free(string);
7822 string = NULL;
7823 }
7824 }
7825 name[len] = cc;
7826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007827 rettv->v_type = VAR_STRING;
7828 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830
7831 return OK;
7832}
7833
7834/*
7835 * Array with names and number of arguments of all internal functions
7836 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7837 */
7838static struct fst
7839{
7840 char *f_name; /* function name */
7841 char f_min_argc; /* minimal number of arguments */
7842 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007844 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845} functions[] =
7846{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847#ifdef FEAT_FLOAT
7848 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007851 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007852 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"append", 2, 2, f_append},
7854 {"argc", 0, 0, f_argc},
7855 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007856 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007857#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007858 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007860 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007863 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"bufexists", 1, 1, f_bufexists},
7865 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7866 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7867 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7868 {"buflisted", 1, 1, f_buflisted},
7869 {"bufloaded", 1, 1, f_bufloaded},
7870 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007871 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"bufwinnr", 1, 1, f_bufwinnr},
7873 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007874 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007875 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#ifdef FEAT_FLOAT
7878 {"ceil", 1, 1, f_ceil},
7879#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007880 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007881 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007883 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007885#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007886 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007887 {"complete_add", 1, 1, f_complete_add},
7888 {"complete_check", 0, 0, f_complete_check},
7889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#ifdef FEAT_FLOAT
7893 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007894 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007895#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007896 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007898 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007899 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"delete", 1, 1, f_delete},
7901 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007902 {"diff_filler", 1, 1, f_diff_filler},
7903 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007904 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"eventhandler", 0, 0, f_eventhandler},
7908 {"executable", 1, 1, f_executable},
7909 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007910#ifdef FEAT_FLOAT
7911 {"exp", 1, 1, f_exp},
7912#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007913 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007914 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007915 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7917 {"filereadable", 1, 1, f_filereadable},
7918 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007920 {"finddir", 1, 3, f_finddir},
7921 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007922#ifdef FEAT_FLOAT
7923 {"float2nr", 1, 1, f_float2nr},
7924 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007925 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007926#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007927 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"fnamemodify", 2, 2, f_fnamemodify},
7929 {"foldclosed", 1, 1, f_foldclosed},
7930 {"foldclosedend", 1, 1, f_foldclosedend},
7931 {"foldlevel", 1, 1, f_foldlevel},
7932 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007933 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007936 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007937 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007938 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007939 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"getchar", 0, 1, f_getchar},
7941 {"getcharmod", 0, 0, f_getcharmod},
7942 {"getcmdline", 0, 0, f_getcmdline},
7943 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007944 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007946 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007947 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getfsize", 1, 1, f_getfsize},
7949 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007950 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007951 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007952 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007953 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007954 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007955 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007956 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007957 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007959 {"gettabvar", 2, 3, f_gettabvar},
7960 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"getwinposx", 0, 0, f_getwinposx},
7962 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007963 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007964 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007965 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007968 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007969 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7971 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7972 {"histadd", 2, 2, f_histadd},
7973 {"histdel", 1, 2, f_histdel},
7974 {"histget", 1, 2, f_histget},
7975 {"histnr", 1, 1, f_histnr},
7976 {"hlID", 1, 1, f_hlID},
7977 {"hlexists", 1, 1, f_hlexists},
7978 {"hostname", 0, 0, f_hostname},
7979 {"iconv", 3, 3, f_iconv},
7980 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007982 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007984 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"inputrestore", 0, 0, f_inputrestore},
7986 {"inputsave", 0, 0, f_inputsave},
7987 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007989 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007991 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007992 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007993 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007994 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007996 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {"libcall", 3, 3, f_libcall},
7998 {"libcallnr", 3, 3, f_libcallnr},
7999 {"line", 1, 1, f_line},
8000 {"line2byte", 1, 1, f_line2byte},
8001 {"lispindent", 1, 1, f_lispindent},
8002 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008003#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008004 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005 {"log10", 1, 1, f_log10},
8006#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008007#ifdef FEAT_LUA
8008 {"luaeval", 1, 2, f_luaeval},
8009#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008011 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008012 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008013 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008014 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008015 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008016 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008017 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008018 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008019 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008020 {"max", 1, 1, f_max},
8021 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008022#ifdef vim_mkdir
8023 {"mkdir", 1, 3, f_mkdir},
8024#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008025 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008026#ifdef FEAT_MZSCHEME
8027 {"mzeval", 1, 1, f_mzeval},
8028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008030 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008031 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008032 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008033#ifdef FEAT_FLOAT
8034 {"pow", 2, 2, f_pow},
8035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008037 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008038 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008039#ifdef FEAT_PYTHON3
8040 {"py3eval", 1, 1, f_py3eval},
8041#endif
8042#ifdef FEAT_PYTHON
8043 {"pyeval", 1, 1, f_pyeval},
8044#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008046 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008047 {"reltime", 0, 2, f_reltime},
8048 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {"remote_expr", 2, 3, f_remote_expr},
8050 {"remote_foreground", 1, 1, f_remote_foreground},
8051 {"remote_peek", 1, 2, f_remote_peek},
8052 {"remote_read", 1, 1, f_remote_read},
8053 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008054 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008056 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008058 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008059#ifdef FEAT_FLOAT
8060 {"round", 1, 1, f_round},
8061#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008062 {"screenattr", 2, 2, f_screenattr},
8063 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008064 {"screencol", 0, 0, f_screencol},
8065 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008066 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008067 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008068 {"searchpair", 3, 7, f_searchpair},
8069 {"searchpairpos", 3, 7, f_searchpairpos},
8070 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"server2client", 2, 2, f_server2client},
8072 {"serverlist", 0, 0, f_serverlist},
8073 {"setbufvar", 3, 3, f_setbufvar},
8074 {"setcmdpos", 1, 1, f_setcmdpos},
8075 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008076 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008077 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008078 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008079 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008081 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008082 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008084#ifdef FEAT_CRYPT
8085 {"sha256", 1, 1, f_sha256},
8086#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008087 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008088 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090#ifdef FEAT_FLOAT
8091 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008092 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008093#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008094 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008095 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008096 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008097 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008098 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"sqrt", 1, 1, f_sqrt},
8101 {"str2float", 1, 1, f_str2float},
8102#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008103 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008104 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008105 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106#ifdef HAVE_STRFTIME
8107 {"strftime", 1, 2, f_strftime},
8108#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008109 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008110 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"strlen", 1, 1, f_strlen},
8112 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008113 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008115 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {"submatch", 1, 1, f_submatch},
8117 {"substitute", 4, 4, f_substitute},
8118 {"synID", 3, 3, f_synID},
8119 {"synIDattr", 2, 3, f_synIDattr},
8120 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008121 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008122 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008123 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008124 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008125 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008126 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008127 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008128 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008129#ifdef FEAT_FLOAT
8130 {"tan", 1, 1, f_tan},
8131 {"tanh", 1, 1, f_tanh},
8132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008134 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"tolower", 1, 1, f_tolower},
8136 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008137 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008138#ifdef FEAT_FLOAT
8139 {"trunc", 1, 1, f_trunc},
8140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008142 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008143 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008144 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"virtcol", 1, 1, f_virtcol},
8146 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008147 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"winbufnr", 1, 1, f_winbufnr},
8149 {"wincol", 0, 0, f_wincol},
8150 {"winheight", 1, 1, f_winheight},
8151 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008152 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008154 {"winrestview", 1, 1, f_winrestview},
8155 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008157 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008158 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159};
8160
8161#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8162
8163/*
8164 * Function given to ExpandGeneric() to obtain the list of internal
8165 * or user defined function names.
8166 */
8167 char_u *
8168get_function_name(xp, idx)
8169 expand_T *xp;
8170 int idx;
8171{
8172 static int intidx = -1;
8173 char_u *name;
8174
8175 if (idx == 0)
8176 intidx = -1;
8177 if (intidx < 0)
8178 {
8179 name = get_user_func_name(xp, idx);
8180 if (name != NULL)
8181 return name;
8182 }
8183 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8184 {
8185 STRCPY(IObuff, functions[intidx].f_name);
8186 STRCAT(IObuff, "(");
8187 if (functions[intidx].f_max_argc == 0)
8188 STRCAT(IObuff, ")");
8189 return IObuff;
8190 }
8191
8192 return NULL;
8193}
8194
8195/*
8196 * Function given to ExpandGeneric() to obtain the list of internal or
8197 * user defined variable or function names.
8198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 char_u *
8200get_expr_name(xp, idx)
8201 expand_T *xp;
8202 int idx;
8203{
8204 static int intidx = -1;
8205 char_u *name;
8206
8207 if (idx == 0)
8208 intidx = -1;
8209 if (intidx < 0)
8210 {
8211 name = get_function_name(xp, idx);
8212 if (name != NULL)
8213 return name;
8214 }
8215 return get_user_var_name(xp, ++intidx);
8216}
8217
8218#endif /* FEAT_CMDL_COMPL */
8219
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008220#if defined(EBCDIC) || defined(PROTO)
8221/*
8222 * Compare struct fst by function name.
8223 */
8224 static int
8225compare_func_name(s1, s2)
8226 const void *s1;
8227 const void *s2;
8228{
8229 struct fst *p1 = (struct fst *)s1;
8230 struct fst *p2 = (struct fst *)s2;
8231
8232 return STRCMP(p1->f_name, p2->f_name);
8233}
8234
8235/*
8236 * Sort the function table by function name.
8237 * The sorting of the table above is ASCII dependant.
8238 * On machines using EBCDIC we have to sort it.
8239 */
8240 static void
8241sortFunctions()
8242{
8243 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8244
8245 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8246}
8247#endif
8248
8249
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250/*
8251 * Find internal function in table above.
8252 * Return index, or -1 if not found
8253 */
8254 static int
8255find_internal_func(name)
8256 char_u *name; /* name of the function */
8257{
8258 int first = 0;
8259 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8260 int cmp;
8261 int x;
8262
8263 /*
8264 * Find the function name in the table. Binary search.
8265 */
8266 while (first <= last)
8267 {
8268 x = first + ((unsigned)(last - first) >> 1);
8269 cmp = STRCMP(name, functions[x].f_name);
8270 if (cmp < 0)
8271 last = x - 1;
8272 else if (cmp > 0)
8273 first = x + 1;
8274 else
8275 return x;
8276 }
8277 return -1;
8278}
8279
8280/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8282 * name it contains, otherwise return "name".
8283 */
8284 static char_u *
8285deref_func_name(name, lenp)
8286 char_u *name;
8287 int *lenp;
8288{
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 int cc;
8291
8292 cc = name[*lenp];
8293 name[*lenp] = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008294 v = find_var(name, NULL, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008296 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008297 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 {
8300 *lenp = 0;
8301 return (char_u *)""; /* just in case */
8302 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008303 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008304 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 }
8306
8307 return name;
8308}
8309
8310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 * Allocate a variable for the result of a function.
8312 * Return OK or FAIL.
8313 */
8314 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008315get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8316 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 char_u *name; /* name of the function */
8318 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 char_u **arg; /* argument, pointing to the '(' */
8321 linenr_T firstline; /* first line of range */
8322 linenr_T lastline; /* last line of range */
8323 int *doesrange; /* return: function handled range */
8324 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326{
8327 char_u *argp;
8328 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008329 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 int argcount = 0; /* number of arguments found */
8331
8332 /*
8333 * Get the arguments.
8334 */
8335 argp = *arg;
8336 while (argcount < MAX_FUNC_ARGS)
8337 {
8338 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8339 if (*argp == ')' || *argp == ',' || *argp == NUL)
8340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8342 {
8343 ret = FAIL;
8344 break;
8345 }
8346 ++argcount;
8347 if (*argp != ',')
8348 break;
8349 }
8350 if (*argp == ')')
8351 ++argp;
8352 else
8353 ret = FAIL;
8354
8355 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008356 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 {
8360 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008361 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008362 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008363 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365
8366 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008367 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 *arg = skipwhite(argp);
8370 return ret;
8371}
8372
8373
8374/*
8375 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008376 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008377 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 */
8379 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008380call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008382 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008384 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008386 typval_T *argvars; /* vars for arguments, must have "argcount"
8387 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 linenr_T firstline; /* first line of range */
8389 linenr_T lastline; /* last line of range */
8390 int *doesrange; /* return: function handled range */
8391 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395#define ERROR_UNKNOWN 0
8396#define ERROR_TOOMANY 1
8397#define ERROR_TOOFEW 2
8398#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008399#define ERROR_DICT 4
8400#define ERROR_NONE 5
8401#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 int error = ERROR_NONE;
8403 int i;
8404 int llen;
8405 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406#define FLEN_FIXED 40
8407 char_u fname_buf[FLEN_FIXED + 1];
8408 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008409 char_u *name;
8410
8411 /* Make a copy of the name, if it comes from a funcref variable it could
8412 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008413 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008414 if (name == NULL)
8415 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
8417 /*
8418 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8419 * Change <SNR>123_name() to K_SNR 123_name().
8420 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 llen = eval_fname_script(name);
8423 if (llen > 0)
8424 {
8425 fname_buf[0] = K_SPECIAL;
8426 fname_buf[1] = KS_EXTRA;
8427 fname_buf[2] = (int)KE_SNR;
8428 i = 3;
8429 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8430 {
8431 if (current_SID <= 0)
8432 error = ERROR_SCRIPT;
8433 else
8434 {
8435 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8436 i = (int)STRLEN(fname_buf);
8437 }
8438 }
8439 if (i + STRLEN(name + llen) < FLEN_FIXED)
8440 {
8441 STRCPY(fname_buf + i, name + llen);
8442 fname = fname_buf;
8443 }
8444 else
8445 {
8446 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8447 if (fname == NULL)
8448 error = ERROR_OTHER;
8449 else
8450 {
8451 mch_memmove(fname, fname_buf, (size_t)i);
8452 STRCPY(fname + i, name + llen);
8453 }
8454 }
8455 }
8456 else
8457 fname = name;
8458
8459 *doesrange = FALSE;
8460
8461
8462 /* execute the function if no errors detected and executing */
8463 if (evaluate && error == ERROR_NONE)
8464 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008465 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 error = ERROR_UNKNOWN;
8468
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
8471 /*
8472 * User defined function.
8473 */
8474 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 /* Trigger FuncUndefined event, may load the function. */
8478 if (fp == NULL
8479 && apply_autocmds(EVENT_FUNCUNDEFINED,
8480 fname, fname, TRUE, NULL)
8481 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008483 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 fp = find_func(fname);
8485 }
8486#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008488 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008489 {
8490 /* loaded a package, search for the function again */
8491 fp = find_func(fname);
8492 }
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 if (fp != NULL)
8495 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008496 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008498 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008500 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008502 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 else
8505 {
8506 /*
8507 * Call the user function.
8508 * Save and restore search patterns, script variables and
8509 * redo buffer.
8510 */
8511 save_search_patterns();
8512 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008513 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008514 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008515 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8517 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8518 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008519 /* Function was unreferenced while being used, free it
8520 * now. */
8521 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 restoreRedobuff();
8523 restore_search_patterns();
8524 error = ERROR_NONE;
8525 }
8526 }
8527 }
8528 else
8529 {
8530 /*
8531 * Find the function name in the table, call its implementation.
8532 */
8533 i = find_internal_func(fname);
8534 if (i >= 0)
8535 {
8536 if (argcount < functions[i].f_min_argc)
8537 error = ERROR_TOOFEW;
8538 else if (argcount > functions[i].f_max_argc)
8539 error = ERROR_TOOMANY;
8540 else
8541 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008542 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008543 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 error = ERROR_NONE;
8545 }
8546 }
8547 }
8548 /*
8549 * The function call (or "FuncUndefined" autocommand sequence) might
8550 * have been aborted by an error, an interrupt, or an explicitly thrown
8551 * exception that has not been caught so far. This situation can be
8552 * tested for by calling aborting(). For an error in an internal
8553 * function or for the "E132" error in call_user_func(), however, the
8554 * throw point at which the "force_abort" flag (temporarily reset by
8555 * emsg()) is normally updated has not been reached yet. We need to
8556 * update that flag first to make aborting() reliable.
8557 */
8558 update_force_abort();
8559 }
8560 if (error == ERROR_NONE)
8561 ret = OK;
8562
8563 /*
8564 * Report an error unless the argument evaluation or function call has been
8565 * cancelled due to an aborting error, an interrupt, or an exception.
8566 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 if (!aborting())
8568 {
8569 switch (error)
8570 {
8571 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008572 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008573 break;
8574 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008575 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 break;
8577 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008578 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008579 name);
8580 break;
8581 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008582 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008583 name);
8584 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008585 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008586 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 name);
8588 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008589 }
8590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 if (fname != name && fname != fname_buf)
8593 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008594 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 return ret;
8597}
8598
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008599/*
8600 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008601 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 */
8603 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008604emsg_funcname(ermsg, name)
8605 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008606 char_u *name;
8607{
8608 char_u *p;
8609
8610 if (*name == K_SPECIAL)
8611 p = concat_str((char_u *)"<SNR>", name + 3);
8612 else
8613 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008614 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008615 if (p != name)
8616 vim_free(p);
8617}
8618
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008619/*
8620 * Return TRUE for a non-zero Number and a non-empty String.
8621 */
8622 static int
8623non_zero_arg(argvars)
8624 typval_T *argvars;
8625{
8626 return ((argvars[0].v_type == VAR_NUMBER
8627 && argvars[0].vval.v_number != 0)
8628 || (argvars[0].v_type == VAR_STRING
8629 && argvars[0].vval.v_string != NULL
8630 && *argvars[0].vval.v_string != NUL));
8631}
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633/*********************************************
8634 * Implementation of the built-in functions
8635 */
8636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008638static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8639
8640/*
8641 * Get the float value of "argvars[0]" into "f".
8642 * Returns FAIL when the argument is not a Number or Float.
8643 */
8644 static int
8645get_float_arg(argvars, f)
8646 typval_T *argvars;
8647 float_T *f;
8648{
8649 if (argvars[0].v_type == VAR_FLOAT)
8650 {
8651 *f = argvars[0].vval.v_float;
8652 return OK;
8653 }
8654 if (argvars[0].v_type == VAR_NUMBER)
8655 {
8656 *f = (float_T)argvars[0].vval.v_number;
8657 return OK;
8658 }
8659 EMSG(_("E808: Number or Float required"));
8660 return FAIL;
8661}
8662
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008663/*
8664 * "abs(expr)" function
8665 */
8666 static void
8667f_abs(argvars, rettv)
8668 typval_T *argvars;
8669 typval_T *rettv;
8670{
8671 if (argvars[0].v_type == VAR_FLOAT)
8672 {
8673 rettv->v_type = VAR_FLOAT;
8674 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8675 }
8676 else
8677 {
8678 varnumber_T n;
8679 int error = FALSE;
8680
8681 n = get_tv_number_chk(&argvars[0], &error);
8682 if (error)
8683 rettv->vval.v_number = -1;
8684 else if (n > 0)
8685 rettv->vval.v_number = n;
8686 else
8687 rettv->vval.v_number = -n;
8688 }
8689}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008690
8691/*
8692 * "acos()" function
8693 */
8694 static void
8695f_acos(argvars, rettv)
8696 typval_T *argvars;
8697 typval_T *rettv;
8698{
8699 float_T f;
8700
8701 rettv->v_type = VAR_FLOAT;
8702 if (get_float_arg(argvars, &f) == OK)
8703 rettv->vval.v_float = acos(f);
8704 else
8705 rettv->vval.v_float = 0.0;
8706}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008707#endif
8708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008710 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 */
8712 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008713f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 typval_T *argvars;
8715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
Bram Moolenaar33570922005-01-25 22:26:29 +00008717 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008720 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008722 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008723 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008724 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008726 }
8727 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 EMSG(_(e_listreq));
8729}
8730
8731/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008732 * "and(expr, expr)" function
8733 */
8734 static void
8735f_and(argvars, rettv)
8736 typval_T *argvars;
8737 typval_T *rettv;
8738{
8739 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8740 & get_tv_number_chk(&argvars[1], NULL);
8741}
8742
8743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 * "append(lnum, string/list)" function
8745 */
8746 static void
8747f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750{
8751 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008753 list_T *l = NULL;
8754 listitem_T *li = NULL;
8755 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008756 long added = 0;
8757
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008758 /* When coming here from Insert mode, sync undo, so that this can be
8759 * undone separately from what was previously inserted. */
8760 if (u_sync_once == 2)
8761 {
8762 u_sync_once = 1; /* notify that u_sync() was called */
8763 u_sync(TRUE);
8764 }
8765
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 lnum = get_tv_lnum(argvars);
8767 if (lnum >= 0
8768 && lnum <= curbuf->b_ml.ml_line_count
8769 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008770 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008771 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008772 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008773 l = argvars[1].vval.v_list;
8774 if (l == NULL)
8775 return;
8776 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008777 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008778 for (;;)
8779 {
8780 if (l == NULL)
8781 tv = &argvars[1]; /* append a string */
8782 else if (li == NULL)
8783 break; /* end of list */
8784 else
8785 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 line = get_tv_string_chk(tv);
8787 if (line == NULL) /* type error */
8788 {
8789 rettv->vval.v_number = 1; /* Failed */
8790 break;
8791 }
8792 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008793 ++added;
8794 if (l == NULL)
8795 break;
8796 li = li->li_next;
8797 }
8798
8799 appended_lines_mark(lnum, added);
8800 if (curwin->w_cursor.lnum > lnum)
8801 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008803 else
8804 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805}
8806
8807/*
8808 * "argc()" function
8809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008811f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008812 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816}
8817
8818/*
8819 * "argidx()" function
8820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
8828
8829/*
8830 * "argv(nr)" function
8831 */
8832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *argvars;
8835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836{
8837 int idx;
8838
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008839 if (argvars[0].v_type != VAR_UNKNOWN)
8840 {
8841 idx = get_tv_number_chk(&argvars[0], NULL);
8842 if (idx >= 0 && idx < ARGCOUNT)
8843 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8844 else
8845 rettv->vval.v_string = NULL;
8846 rettv->v_type = VAR_STRING;
8847 }
8848 else if (rettv_list_alloc(rettv) == OK)
8849 for (idx = 0; idx < ARGCOUNT; ++idx)
8850 list_append_string(rettv->vval.v_list,
8851 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852}
8853
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008854#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008858 static void
8859f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008860 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008861 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008862{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008863 float_T f;
8864
8865 rettv->v_type = VAR_FLOAT;
8866 if (get_float_arg(argvars, &f) == OK)
8867 rettv->vval.v_float = asin(f);
8868 else
8869 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870}
8871
8872/*
8873 * "atan()" function
8874 */
8875 static void
8876f_atan(argvars, rettv)
8877 typval_T *argvars;
8878 typval_T *rettv;
8879{
8880 float_T f;
8881
8882 rettv->v_type = VAR_FLOAT;
8883 if (get_float_arg(argvars, &f) == OK)
8884 rettv->vval.v_float = atan(f);
8885 else
8886 rettv->vval.v_float = 0.0;
8887}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008888
8889/*
8890 * "atan2()" function
8891 */
8892 static void
8893f_atan2(argvars, rettv)
8894 typval_T *argvars;
8895 typval_T *rettv;
8896{
8897 float_T fx, fy;
8898
8899 rettv->v_type = VAR_FLOAT;
8900 if (get_float_arg(argvars, &fx) == OK
8901 && get_float_arg(&argvars[1], &fy) == OK)
8902 rettv->vval.v_float = atan2(fx, fy);
8903 else
8904 rettv->vval.v_float = 0.0;
8905}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008906#endif
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908/*
8909 * "browse(save, title, initdir, default)" function
8910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008913 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916#ifdef FEAT_BROWSE
8917 int save;
8918 char_u *title;
8919 char_u *initdir;
8920 char_u *defname;
8921 char_u buf[NUMBUFLEN];
8922 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008925 save = get_tv_number_chk(&argvars[0], &error);
8926 title = get_tv_string_chk(&argvars[1]);
8927 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8928 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 if (error || title == NULL || initdir == NULL || defname == NULL)
8931 rettv->vval.v_string = NULL;
8932 else
8933 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008934 do_browse(save ? BROWSE_SAVE : 0,
8935 title, defname, NULL, initdir, NULL, curbuf);
8936#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008938#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008940}
8941
8942/*
8943 * "browsedir(title, initdir)" function
8944 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008947 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008949{
8950#ifdef FEAT_BROWSE
8951 char_u *title;
8952 char_u *initdir;
8953 char_u buf[NUMBUFLEN];
8954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 title = get_tv_string_chk(&argvars[0]);
8956 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 if (title == NULL || initdir == NULL)
8959 rettv->vval.v_string = NULL;
8960 else
8961 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008962 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967}
8968
Bram Moolenaar33570922005-01-25 22:26:29 +00008969static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008970
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971/*
8972 * Find a buffer by number or exact name.
8973 */
8974 static buf_T *
8975find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008976 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980 if (avar->v_type == VAR_NUMBER)
8981 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008982 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008985 if (buf == NULL)
8986 {
8987 /* No full path name match, try a match with a URL or a "nofile"
8988 * buffer, these don't use the full path. */
8989 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8990 if (buf->b_fname != NULL
8991 && (path_with_url(buf->b_fname)
8992#ifdef FEAT_QUICKFIX
8993 || bt_nofile(buf)
8994#endif
8995 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008997 break;
8998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 }
9000 return buf;
9001}
9002
9003/*
9004 * "bufexists(expr)" function
9005 */
9006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 typval_T *argvars;
9009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012}
9013
9014/*
9015 * "buflisted(expr)" function
9016 */
9017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 typval_T *argvars;
9020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022 buf_T *buf;
9023
9024 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026}
9027
9028/*
9029 * "bufloaded(expr)" function
9030 */
9031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 typval_T *argvars;
9034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 buf_T *buf;
9037
9038 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040}
9041
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009042static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009043
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044/*
9045 * Get buffer by number or pattern.
9046 */
9047 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009048get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009050 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 int save_magic;
9054 char_u *save_cpo;
9055 buf_T *buf;
9056
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 if (tv->v_type == VAR_NUMBER)
9058 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009059 if (tv->v_type != VAR_STRING)
9060 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 if (name == NULL || *name == NUL)
9062 return curbuf;
9063 if (name[0] == '$' && name[1] == NUL)
9064 return lastbuf;
9065
9066 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9067 save_magic = p_magic;
9068 p_magic = TRUE;
9069 save_cpo = p_cpo;
9070 p_cpo = (char_u *)"";
9071
9072 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009073 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 p_magic = save_magic;
9076 p_cpo = save_cpo;
9077
9078 /* If not found, try expanding the name, like done for bufexists(). */
9079 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081
9082 return buf;
9083}
9084
9085/*
9086 * "bufname(expr)" function
9087 */
9088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 typval_T *argvars;
9091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093 buf_T *buf;
9094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009095 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009097 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 --emsg_off;
9104}
9105
9106/*
9107 * "bufnr(expr)" function
9108 */
9109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 typval_T *argvars;
9112 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009115 int error = FALSE;
9116 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009118 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009120 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009121 --emsg_off;
9122
9123 /* If the buffer isn't found and the second argument is not zero create a
9124 * new buffer. */
9125 if (buf == NULL
9126 && argvars[1].v_type != VAR_UNKNOWN
9127 && get_tv_number_chk(&argvars[1], &error) != 0
9128 && !error
9129 && (name = get_tv_string_chk(&argvars[0])) != NULL
9130 && !error)
9131 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9132
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138
9139/*
9140 * "bufwinnr(nr)" function
9141 */
9142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 typval_T *argvars;
9145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147#ifdef FEAT_WINDOWS
9148 win_T *wp;
9149 int winnr = 0;
9150#endif
9151 buf_T *buf;
9152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009155 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#ifdef FEAT_WINDOWS
9157 for (wp = firstwin; wp; wp = wp->w_next)
9158 {
9159 ++winnr;
9160 if (wp->w_buffer == buf)
9161 break;
9162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#endif
9167 --emsg_off;
9168}
9169
9170/*
9171 * "byte2line(byte)" function
9172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180#else
9181 long boff = 0;
9182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009185 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 (linenr_T)0, &boff);
9189#endif
9190}
9191
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009192 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009193byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 typval_T *argvars;
9195 typval_T *rettv;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009196 int comp;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009197{
9198#ifdef FEAT_MBYTE
9199 char_u *t;
9200#endif
9201 char_u *str;
9202 long idx;
9203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 str = get_tv_string_chk(&argvars[0]);
9205 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009208 return;
9209
9210#ifdef FEAT_MBYTE
9211 t = str;
9212 for ( ; idx > 0; idx--)
9213 {
9214 if (*t == NUL) /* EOL reached */
9215 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009216 if (enc_utf8 && comp)
9217 t += utf_ptr2len(t);
9218 else
9219 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009220 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009221 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009222#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009223 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009225#endif
9226}
9227
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009228/*
9229 * "byteidx()" function
9230 */
9231 static void
9232f_byteidx(argvars, rettv)
9233 typval_T *argvars;
9234 typval_T *rettv;
9235{
9236 byteidx(argvars, rettv, FALSE);
9237}
9238
9239/*
9240 * "byteidxcomp()" function
9241 */
9242 static void
9243f_byteidxcomp(argvars, rettv)
9244 typval_T *argvars;
9245 typval_T *rettv;
9246{
9247 byteidx(argvars, rettv, TRUE);
9248}
9249
Bram Moolenaardb913952012-06-29 12:54:53 +02009250 int
9251func_call(name, args, selfdict, rettv)
9252 char_u *name;
9253 typval_T *args;
9254 dict_T *selfdict;
9255 typval_T *rettv;
9256{
9257 listitem_T *item;
9258 typval_T argv[MAX_FUNC_ARGS + 1];
9259 int argc = 0;
9260 int dummy;
9261 int r = 0;
9262
9263 for (item = args->vval.v_list->lv_first; item != NULL;
9264 item = item->li_next)
9265 {
9266 if (argc == MAX_FUNC_ARGS)
9267 {
9268 EMSG(_("E699: Too many arguments"));
9269 break;
9270 }
9271 /* Make a copy of each argument. This is needed to be able to set
9272 * v_lock to VAR_FIXED in the copy without changing the original list.
9273 */
9274 copy_tv(&item->li_tv, &argv[argc++]);
9275 }
9276
9277 if (item == NULL)
9278 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9279 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9280 &dummy, TRUE, selfdict);
9281
9282 /* Free the arguments. */
9283 while (argc > 0)
9284 clear_tv(&argv[--argc]);
9285
9286 return r;
9287}
9288
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009289/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009290 * "call(func, arglist)" function
9291 */
9292 static void
9293f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T *argvars;
9295 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009296{
9297 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009298 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009299
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009300 if (argvars[1].v_type != VAR_LIST)
9301 {
9302 EMSG(_(e_listreq));
9303 return;
9304 }
9305 if (argvars[1].vval.v_list == NULL)
9306 return;
9307
9308 if (argvars[0].v_type == VAR_FUNC)
9309 func = argvars[0].vval.v_string;
9310 else
9311 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009312 if (*func == NUL)
9313 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009314
Bram Moolenaare9a41262005-01-15 22:18:47 +00009315 if (argvars[2].v_type != VAR_UNKNOWN)
9316 {
9317 if (argvars[2].v_type != VAR_DICT)
9318 {
9319 EMSG(_(e_dictreq));
9320 return;
9321 }
9322 selfdict = argvars[2].vval.v_dict;
9323 }
9324
Bram Moolenaardb913952012-06-29 12:54:53 +02009325 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009326}
9327
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009328#ifdef FEAT_FLOAT
9329/*
9330 * "ceil({float})" function
9331 */
9332 static void
9333f_ceil(argvars, rettv)
9334 typval_T *argvars;
9335 typval_T *rettv;
9336{
9337 float_T f;
9338
9339 rettv->v_type = VAR_FLOAT;
9340 if (get_float_arg(argvars, &f) == OK)
9341 rettv->vval.v_float = ceil(f);
9342 else
9343 rettv->vval.v_float = 0.0;
9344}
9345#endif
9346
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009347/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009348 * "changenr()" function
9349 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009350 static void
9351f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009352 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009353 typval_T *rettv;
9354{
9355 rettv->vval.v_number = curbuf->b_u_seq_cur;
9356}
9357
9358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 * "char2nr(string)" function
9360 */
9361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
9366#ifdef FEAT_MBYTE
9367 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009368 {
9369 int utf8 = 0;
9370
9371 if (argvars[1].v_type != VAR_UNKNOWN)
9372 utf8 = get_tv_number_chk(&argvars[1], NULL);
9373
9374 if (utf8)
9375 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9376 else
9377 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 else
9380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * "cindent(lnum)" function
9386 */
9387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009389 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391{
9392#ifdef FEAT_CINDENT
9393 pos_T pos;
9394 linenr_T lnum;
9395
9396 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9399 {
9400 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009401 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 curwin->w_cursor = pos;
9403 }
9404 else
9405#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407}
9408
9409/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009410 * "clearmatches()" function
9411 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009412 static void
9413f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009414 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009415 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009416{
9417#ifdef FEAT_SEARCH_EXTRA
9418 clear_matches(curwin);
9419#endif
9420}
9421
9422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 * "col(string)" function
9424 */
9425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
9430 colnr_T col = 0;
9431 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009432 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009434 fp = var2fpos(&argvars[0], FALSE, &fnum);
9435 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
9437 if (fp->col == MAXCOL)
9438 {
9439 /* '> can be MAXCOL, get the length of the line then */
9440 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009441 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 else
9443 col = MAXCOL;
9444 }
9445 else
9446 {
9447 col = fp->col + 1;
9448#ifdef FEAT_VIRTUALEDIT
9449 /* col(".") when the cursor is on the NUL at the end of the line
9450 * because of "coladd" can be seen as an extra column. */
9451 if (virtual_active() && fp == &curwin->w_cursor)
9452 {
9453 char_u *p = ml_get_cursor();
9454
9455 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9456 curwin->w_virtcol - curwin->w_cursor.coladd))
9457 {
9458# ifdef FEAT_MBYTE
9459 int l;
9460
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009461 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 col += l;
9463# else
9464 if (*p != NUL && p[1] == NUL)
9465 ++col;
9466# endif
9467 }
9468 }
9469#endif
9470 }
9471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009472 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473}
9474
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475#if defined(FEAT_INS_EXPAND)
9476/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009477 * "complete()" function
9478 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009479 static void
9480f_complete(argvars, rettv)
9481 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009482 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009483{
9484 int startcol;
9485
9486 if ((State & INSERT) == 0)
9487 {
9488 EMSG(_("E785: complete() can only be used in Insert mode"));
9489 return;
9490 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009491
9492 /* Check for undo allowed here, because if something was already inserted
9493 * the line was already saved for undo and this check isn't done. */
9494 if (!undo_allowed())
9495 return;
9496
Bram Moolenaarade00832006-03-10 21:46:58 +00009497 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9498 {
9499 EMSG(_(e_invarg));
9500 return;
9501 }
9502
9503 startcol = get_tv_number_chk(&argvars[0], NULL);
9504 if (startcol <= 0)
9505 return;
9506
9507 set_completion(startcol - 1, argvars[1].vval.v_list);
9508}
9509
9510/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009511 * "complete_add()" function
9512 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009513 static void
9514f_complete_add(argvars, rettv)
9515 typval_T *argvars;
9516 typval_T *rettv;
9517{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009518 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009519}
9520
9521/*
9522 * "complete_check()" function
9523 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009524 static void
9525f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009526 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009527 typval_T *rettv;
9528{
9529 int saved = RedrawingDisabled;
9530
9531 RedrawingDisabled = 0;
9532 ins_compl_check_keys(0);
9533 rettv->vval.v_number = compl_interrupted;
9534 RedrawingDisabled = saved;
9535}
9536#endif
9537
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538/*
9539 * "confirm(message, buttons[, default [, type]])" function
9540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009543 typval_T *argvars UNUSED;
9544 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9547 char_u *message;
9548 char_u *buttons = NULL;
9549 char_u buf[NUMBUFLEN];
9550 char_u buf2[NUMBUFLEN];
9551 int def = 1;
9552 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 char_u *typestr;
9554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 message = get_tv_string_chk(&argvars[0]);
9557 if (message == NULL)
9558 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9562 if (buttons == NULL)
9563 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009564 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9570 if (typestr == NULL)
9571 error = TRUE;
9572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 switch (TOUPPER_ASC(*typestr))
9575 {
9576 case 'E': type = VIM_ERROR; break;
9577 case 'Q': type = VIM_QUESTION; break;
9578 case 'I': type = VIM_INFO; break;
9579 case 'W': type = VIM_WARNING; break;
9580 case 'G': type = VIM_GENERIC; break;
9581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 }
9583 }
9584 }
9585 }
9586
9587 if (buttons == NULL || *buttons == NUL)
9588 buttons = (char_u *)_("&Ok");
9589
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009590 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009591 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009592 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
9594}
9595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596/*
9597 * "copy()" function
9598 */
9599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009601 typval_T *argvars;
9602 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009603{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009604 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009607#ifdef FEAT_FLOAT
9608/*
9609 * "cos()" function
9610 */
9611 static void
9612f_cos(argvars, rettv)
9613 typval_T *argvars;
9614 typval_T *rettv;
9615{
9616 float_T f;
9617
9618 rettv->v_type = VAR_FLOAT;
9619 if (get_float_arg(argvars, &f) == OK)
9620 rettv->vval.v_float = cos(f);
9621 else
9622 rettv->vval.v_float = 0.0;
9623}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009624
9625/*
9626 * "cosh()" function
9627 */
9628 static void
9629f_cosh(argvars, rettv)
9630 typval_T *argvars;
9631 typval_T *rettv;
9632{
9633 float_T f;
9634
9635 rettv->v_type = VAR_FLOAT;
9636 if (get_float_arg(argvars, &f) == OK)
9637 rettv->vval.v_float = cosh(f);
9638 else
9639 rettv->vval.v_float = 0.0;
9640}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009641#endif
9642
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009644 * "count()" function
9645 */
9646 static void
9647f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 typval_T *argvars;
9649 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009650{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009651 long n = 0;
9652 int ic = FALSE;
9653
Bram Moolenaare9a41262005-01-15 22:18:47 +00009654 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009655 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 listitem_T *li;
9657 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009658 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009659
Bram Moolenaare9a41262005-01-15 22:18:47 +00009660 if ((l = argvars[0].vval.v_list) != NULL)
9661 {
9662 li = l->lv_first;
9663 if (argvars[2].v_type != VAR_UNKNOWN)
9664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 int error = FALSE;
9666
9667 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 if (argvars[3].v_type != VAR_UNKNOWN)
9669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009670 idx = get_tv_number_chk(&argvars[3], &error);
9671 if (!error)
9672 {
9673 li = list_find(l, idx);
9674 if (li == NULL)
9675 EMSGN(_(e_listidx), idx);
9676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009678 if (error)
9679 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 }
9681
9682 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009683 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009684 ++n;
9685 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 else if (argvars[0].v_type == VAR_DICT)
9688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009689 int todo;
9690 dict_T *d;
9691 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009693 if ((d = argvars[0].vval.v_dict) != NULL)
9694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 int error = FALSE;
9696
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 if (argvars[2].v_type != VAR_UNKNOWN)
9698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 if (argvars[3].v_type != VAR_UNKNOWN)
9701 EMSG(_(e_invarg));
9702 }
9703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009704 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009706 {
9707 if (!HASHITEM_EMPTY(hi))
9708 {
9709 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009710 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009711 ++n;
9712 }
9713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009714 }
9715 }
9716 else
9717 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009718 rettv->vval.v_number = n;
9719}
9720
9721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9723 *
9724 * Checks the existence of a cscope connection.
9725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009728 typval_T *argvars UNUSED;
9729 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731#ifdef FEAT_CSCOPE
9732 int num = 0;
9733 char_u *dbpath = NULL;
9734 char_u *prepend = NULL;
9735 char_u buf[NUMBUFLEN];
9736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009737 if (argvars[0].v_type != VAR_UNKNOWN
9738 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 num = (int)get_tv_number(&argvars[0]);
9741 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009742 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#endif
9748}
9749
9750/*
9751 * "cursor(lnum, col)" function
9752 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009753 * Moves the cursor to the specified line and column.
9754 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009758 typval_T *argvars;
9759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009762#ifdef FEAT_VIRTUALEDIT
9763 long coladd = 0;
9764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009766 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009767 if (argvars[1].v_type == VAR_UNKNOWN)
9768 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009769 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009770
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009771 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009772 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009773 line = pos.lnum;
9774 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009775#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009776 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009777#endif
9778 }
9779 else
9780 {
9781 line = get_tv_lnum(argvars);
9782 col = get_tv_number_chk(&argvars[1], NULL);
9783#ifdef FEAT_VIRTUALEDIT
9784 if (argvars[2].v_type != VAR_UNKNOWN)
9785 coladd = get_tv_number_chk(&argvars[2], NULL);
9786#endif
9787 }
9788 if (line < 0 || col < 0
9789#ifdef FEAT_VIRTUALEDIT
9790 || coladd < 0
9791#endif
9792 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 if (line > 0)
9795 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 if (col > 0)
9797 curwin->w_cursor.col = col - 1;
9798#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009799 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800#endif
9801
9802 /* Make sure the cursor is in a valid position. */
9803 check_cursor();
9804#ifdef FEAT_MBYTE
9805 /* Correct cursor for multi-byte character. */
9806 if (has_mbyte)
9807 mb_adjust_cursor();
9808#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009809
9810 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009811 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009815 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 */
9817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009819 typval_T *argvars;
9820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009822 int noref = 0;
9823
9824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009826 if (noref < 0 || noref > 1)
9827 EMSG(_(e_invarg));
9828 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009829 {
9830 current_copyID += COPYID_INC;
9831 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833}
9834
9835/*
9836 * "delete()" function
9837 */
9838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009840 typval_T *argvars;
9841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842{
9843 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847}
9848
9849/*
9850 * "did_filetype()" function
9851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009854 typval_T *argvars UNUSED;
9855 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859#endif
9860}
9861
9862/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009863 * "diff_filler()" function
9864 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009867 typval_T *argvars UNUSED;
9868 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009869{
9870#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009872#endif
9873}
9874
9875/*
9876 * "diff_hlID()" function
9877 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009880 typval_T *argvars UNUSED;
9881 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882{
9883#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009885 static linenr_T prev_lnum = 0;
9886 static int changedtick = 0;
9887 static int fnum = 0;
9888 static int change_start = 0;
9889 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009890 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009891 int filler_lines;
9892 int col;
9893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009894 if (lnum < 0) /* ignore type error in {lnum} arg */
9895 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009896 if (lnum != prev_lnum
9897 || changedtick != curbuf->b_changedtick
9898 || fnum != curbuf->b_fnum)
9899 {
9900 /* New line, buffer, change: need to get the values. */
9901 filler_lines = diff_check(curwin, lnum);
9902 if (filler_lines < 0)
9903 {
9904 if (filler_lines == -1)
9905 {
9906 change_start = MAXCOL;
9907 change_end = -1;
9908 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9909 hlID = HLF_ADD; /* added line */
9910 else
9911 hlID = HLF_CHD; /* changed line */
9912 }
9913 else
9914 hlID = HLF_ADD; /* added line */
9915 }
9916 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009917 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009918 prev_lnum = lnum;
9919 changedtick = curbuf->b_changedtick;
9920 fnum = curbuf->b_fnum;
9921 }
9922
9923 if (hlID == HLF_CHD || hlID == HLF_TXD)
9924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009926 if (col >= change_start && col <= change_end)
9927 hlID = HLF_TXD; /* changed text */
9928 else
9929 hlID = HLF_CHD; /* changed line */
9930 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009931 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009932#endif
9933}
9934
9935/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009936 * "empty({expr})" function
9937 */
9938 static void
9939f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009942{
9943 int n;
9944
9945 switch (argvars[0].v_type)
9946 {
9947 case VAR_STRING:
9948 case VAR_FUNC:
9949 n = argvars[0].vval.v_string == NULL
9950 || *argvars[0].vval.v_string == NUL;
9951 break;
9952 case VAR_NUMBER:
9953 n = argvars[0].vval.v_number == 0;
9954 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009955#ifdef FEAT_FLOAT
9956 case VAR_FLOAT:
9957 n = argvars[0].vval.v_float == 0.0;
9958 break;
9959#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009960 case VAR_LIST:
9961 n = argvars[0].vval.v_list == NULL
9962 || argvars[0].vval.v_list->lv_first == NULL;
9963 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009964 case VAR_DICT:
9965 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009966 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009967 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009968 default:
9969 EMSG2(_(e_intern2), "f_empty()");
9970 n = 0;
9971 }
9972
9973 rettv->vval.v_number = n;
9974}
9975
9976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 * "escape({string}, {chars})" function
9978 */
9979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009981 typval_T *argvars;
9982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984 char_u buf[NUMBUFLEN];
9985
Bram Moolenaar758711c2005-02-02 23:11:38 +00009986 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9987 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989}
9990
9991/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009992 * "eval()" function
9993 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009994 static void
9995f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009996 typval_T *argvars;
9997 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009998{
9999 char_u *s;
10000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010001 s = get_tv_string_chk(&argvars[0]);
10002 if (s != NULL)
10003 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010005 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10006 {
10007 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010010 else if (*s != NUL)
10011 EMSG(_(e_trailing));
10012}
10013
10014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 * "eventhandler()" function
10016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010018f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010019 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023}
10024
10025/*
10026 * "executable()" function
10027 */
10028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010029f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 typval_T *argvars;
10031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034}
10035
10036/*
10037 * "exists()" function
10038 */
10039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010041 typval_T *argvars;
10042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043{
10044 char_u *p;
10045 char_u *name;
10046 int n = FALSE;
10047 int len = 0;
10048
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 if (*p == '$') /* environment variable */
10051 {
10052 /* first try "normal" environment variables (fast) */
10053 if (mch_getenv(p + 1) != NULL)
10054 n = TRUE;
10055 else
10056 {
10057 /* try expanding things like $VIM and ${HOME} */
10058 p = expand_env_save(p);
10059 if (p != NULL && *p != '$')
10060 n = TRUE;
10061 vim_free(p);
10062 }
10063 }
10064 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010065 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010067 if (*skipwhite(p) != NUL)
10068 n = FALSE; /* trailing garbage */
10069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 else if (*p == '*') /* internal or user defined function */
10071 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010072 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 }
10074 else if (*p == ':')
10075 {
10076 n = cmd_exists(p + 1);
10077 }
10078 else if (*p == '#')
10079 {
10080#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010081 if (p[1] == '#')
10082 n = autocmd_supported(p + 2);
10083 else
10084 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085#endif
10086 }
10087 else /* internal variable */
10088 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010089 char_u *tofree;
10090 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010092 /* get_name_len() takes care of expanding curly braces */
10093 name = p;
10094 len = get_name_len(&p, &tofree, TRUE, FALSE);
10095 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010097 if (tofree != NULL)
10098 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010099 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010100 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010102 /* handle d.key, l[idx], f(expr) */
10103 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10104 if (n)
10105 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010108 if (*p != NUL)
10109 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010111 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 }
10113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115}
10116
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010117#ifdef FEAT_FLOAT
10118/*
10119 * "exp()" function
10120 */
10121 static void
10122f_exp(argvars, rettv)
10123 typval_T *argvars;
10124 typval_T *rettv;
10125{
10126 float_T f;
10127
10128 rettv->v_type = VAR_FLOAT;
10129 if (get_float_arg(argvars, &f) == OK)
10130 rettv->vval.v_float = exp(f);
10131 else
10132 rettv->vval.v_float = 0.0;
10133}
10134#endif
10135
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136/*
10137 * "expand()" function
10138 */
10139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010141 typval_T *argvars;
10142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
10144 char_u *s;
10145 int len;
10146 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010147 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010149 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010150 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010153 if (argvars[1].v_type != VAR_UNKNOWN
10154 && argvars[2].v_type != VAR_UNKNOWN
10155 && get_tv_number_chk(&argvars[2], &error)
10156 && !error)
10157 {
10158 rettv->v_type = VAR_LIST;
10159 rettv->vval.v_list = NULL;
10160 }
10161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (*s == '%' || *s == '#' || *s == '<')
10164 {
10165 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010166 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010168 if (rettv->v_type == VAR_LIST)
10169 {
10170 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10171 list_append_string(rettv->vval.v_list, result, -1);
10172 else
10173 vim_free(result);
10174 }
10175 else
10176 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 }
10178 else
10179 {
10180 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010181 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 if (argvars[1].v_type != VAR_UNKNOWN
10183 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010184 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 if (!error)
10186 {
10187 ExpandInit(&xpc);
10188 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010189 if (p_wic)
10190 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010191 if (rettv->v_type == VAR_STRING)
10192 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10193 options, WILD_ALL);
10194 else if (rettv_list_alloc(rettv) != FAIL)
10195 {
10196 int i;
10197
10198 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10199 for (i = 0; i < xpc.xp_numfiles; i++)
10200 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10201 ExpandCleanup(&xpc);
10202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 }
10204 else
10205 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 }
10207}
10208
10209/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010210 * Go over all entries in "d2" and add them to "d1".
10211 * When "action" is "error" then a duplicate key is an error.
10212 * When "action" is "force" then a duplicate key is overwritten.
10213 * Otherwise duplicate keys are ignored ("action" is "keep").
10214 */
10215 void
10216dict_extend(d1, d2, action)
10217 dict_T *d1;
10218 dict_T *d2;
10219 char_u *action;
10220{
10221 dictitem_T *di1;
10222 hashitem_T *hi2;
10223 int todo;
10224
10225 todo = (int)d2->dv_hashtab.ht_used;
10226 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10227 {
10228 if (!HASHITEM_EMPTY(hi2))
10229 {
10230 --todo;
10231 di1 = dict_find(d1, hi2->hi_key, -1);
10232 if (d1->dv_scope != 0)
10233 {
10234 /* Disallow replacing a builtin function in l: and g:.
10235 * Check the key to be valid when adding to any
10236 * scope. */
10237 if (d1->dv_scope == VAR_DEF_SCOPE
10238 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10239 && var_check_func_name(hi2->hi_key,
10240 di1 == NULL))
10241 break;
10242 if (!valid_varname(hi2->hi_key))
10243 break;
10244 }
10245 if (di1 == NULL)
10246 {
10247 di1 = dictitem_copy(HI2DI(hi2));
10248 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10249 dictitem_free(di1);
10250 }
10251 else if (*action == 'e')
10252 {
10253 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10254 break;
10255 }
10256 else if (*action == 'f' && HI2DI(hi2) != di1)
10257 {
10258 clear_tv(&di1->di_tv);
10259 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10260 }
10261 }
10262 }
10263}
10264
10265/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010266 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010267 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268 */
10269 static void
10270f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010271 typval_T *argvars;
10272 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010273{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010274 char *arg_errmsg = N_("extend() argument");
10275
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010277 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010278 list_T *l1, *l2;
10279 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010282
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 l1 = argvars[0].vval.v_list;
10284 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010285 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010286 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287 {
10288 if (argvars[2].v_type != VAR_UNKNOWN)
10289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 before = get_tv_number_chk(&argvars[2], &error);
10291 if (error)
10292 return; /* type error; errmsg already given */
10293
Bram Moolenaar758711c2005-02-02 23:11:38 +000010294 if (before == l1->lv_len)
10295 item = NULL;
10296 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010298 item = list_find(l1, before);
10299 if (item == NULL)
10300 {
10301 EMSGN(_(e_listidx), before);
10302 return;
10303 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010304 }
10305 }
10306 else
10307 item = NULL;
10308 list_extend(l1, l2, item);
10309
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 copy_tv(&argvars[0], rettv);
10311 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010312 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10314 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010315 dict_T *d1, *d2;
10316 char_u *action;
10317 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318
10319 d1 = argvars[0].vval.v_dict;
10320 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010321 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010322 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 {
10324 /* Check the third argument. */
10325 if (argvars[2].v_type != VAR_UNKNOWN)
10326 {
10327 static char *(av[]) = {"keep", "force", "error"};
10328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 action = get_tv_string_chk(&argvars[2]);
10330 if (action == NULL)
10331 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 for (i = 0; i < 3; ++i)
10333 if (STRCMP(action, av[i]) == 0)
10334 break;
10335 if (i == 3)
10336 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010337 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010338 return;
10339 }
10340 }
10341 else
10342 action = (char_u *)"force";
10343
Bram Moolenaara9922d62013-05-30 13:01:18 +020010344 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346 copy_tv(&argvars[0], rettv);
10347 }
10348 }
10349 else
10350 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010351}
10352
10353/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010354 * "feedkeys()" function
10355 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010356 static void
10357f_feedkeys(argvars, rettv)
10358 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010359 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010360{
10361 int remap = TRUE;
10362 char_u *keys, *flags;
10363 char_u nbuf[NUMBUFLEN];
10364 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010365 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010366
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010367 /* This is not allowed in the sandbox. If the commands would still be
10368 * executed in the sandbox it would be OK, but it probably happens later,
10369 * when "sandbox" is no longer set. */
10370 if (check_secure())
10371 return;
10372
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010373 keys = get_tv_string(&argvars[0]);
10374 if (*keys != NUL)
10375 {
10376 if (argvars[1].v_type != VAR_UNKNOWN)
10377 {
10378 flags = get_tv_string_buf(&argvars[1], nbuf);
10379 for ( ; *flags != NUL; ++flags)
10380 {
10381 switch (*flags)
10382 {
10383 case 'n': remap = FALSE; break;
10384 case 'm': remap = TRUE; break;
10385 case 't': typed = TRUE; break;
10386 }
10387 }
10388 }
10389
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010390 /* Need to escape K_SPECIAL and CSI before putting the string in the
10391 * typeahead buffer. */
10392 keys_esc = vim_strsave_escape_csi(keys);
10393 if (keys_esc != NULL)
10394 {
10395 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010396 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010397 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010398 if (vgetc_busy)
10399 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010400 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010401 }
10402}
10403
10404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 * "filereadable()" function
10406 */
10407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 typval_T *argvars;
10410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010412 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 char_u *p;
10414 int n;
10415
Bram Moolenaarc236c162008-07-13 17:41:49 +000010416#ifndef O_NONBLOCK
10417# define O_NONBLOCK 0
10418#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010419 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010420 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10421 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 {
10423 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010424 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 }
10426 else
10427 n = FALSE;
10428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430}
10431
10432/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010433 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 * rights to write into.
10435 */
10436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010437f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010438 typval_T *argvars;
10439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010441 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
10443
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010444static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010445
10446 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010447findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010449 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010450 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010451{
10452#ifdef FEAT_SEARCHPATH
10453 char_u *fname;
10454 char_u *fresult = NULL;
10455 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10456 char_u *p;
10457 char_u pathbuf[NUMBUFLEN];
10458 int count = 1;
10459 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010460 int error = FALSE;
10461#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010462
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010463 rettv->vval.v_string = NULL;
10464 rettv->v_type = VAR_STRING;
10465
10466#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010468
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010469 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10472 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010473 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 else
10475 {
10476 if (*p != NUL)
10477 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010480 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010482 }
10483
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010484 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10485 error = TRUE;
10486
10487 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 do
10490 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010491 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010492 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 fresult = find_file_in_path_option(first ? fname : NULL,
10494 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010495 0, first, path,
10496 find_what,
10497 curbuf->b_ffname,
10498 find_what == FINDFILE_DIR
10499 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010501
10502 if (fresult != NULL && rettv->v_type == VAR_LIST)
10503 list_append_string(rettv->vval.v_list, fresult, -1);
10504
10505 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010507
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010508 if (rettv->v_type == VAR_STRING)
10509 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010510#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010511}
10512
Bram Moolenaar33570922005-01-25 22:26:29 +000010513static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10514static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010515
10516/*
10517 * Implementation of map() and filter().
10518 */
10519 static void
10520filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010523 int map;
10524{
10525 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 listitem_T *li, *nli;
10528 list_T *l = NULL;
10529 dictitem_T *di;
10530 hashtab_T *ht;
10531 hashitem_T *hi;
10532 dict_T *d = NULL;
10533 typval_T save_val;
10534 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010536 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010537 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10538 char *arg_errmsg = (map ? N_("map() argument")
10539 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010540 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010541 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010542
Bram Moolenaare9a41262005-01-15 22:18:47 +000010543 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010544 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010545 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010546 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 return;
10548 }
10549 else if (argvars[0].v_type == VAR_DICT)
10550 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010551 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010552 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 return;
10554 }
10555 else
10556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010557 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 return;
10559 }
10560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 expr = get_tv_string_buf_chk(&argvars[1], buf);
10562 /* On type errors, the preceding call has already displayed an error
10563 * message. Avoid a misleading error message for an empty string that
10564 * was not passed as argument. */
10565 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 prepare_vimvar(VV_VAL, &save_val);
10568 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010569
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010570 /* We reset "did_emsg" to be able to detect whether an error
10571 * occurred during evaluation of the expression. */
10572 save_did_emsg = did_emsg;
10573 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010574
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010575 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 vimvars[VV_KEY].vv_type = VAR_STRING;
10579
10580 ht = &d->dv_hashtab;
10581 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010582 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 if (!HASHITEM_EMPTY(hi))
10586 {
10587 --todo;
10588 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010589 if (tv_check_lock(di->di_tv.v_lock,
10590 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 break;
10592 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010593 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010594 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 break;
10596 if (!map && rem)
10597 dictitem_remove(d, di);
10598 clear_tv(&vimvars[VV_KEY].vv_tv);
10599 }
10600 }
10601 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 }
10603 else
10604 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010605 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 for (li = l->lv_first; li != NULL; li = nli)
10608 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010609 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010610 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010612 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010613 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010614 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010615 break;
10616 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010618 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010619 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010620 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010621
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010622 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010624
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010625 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627
10628 copy_tv(&argvars[0], rettv);
10629}
10630
10631 static int
10632filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 char_u *expr;
10635 int map;
10636 int *remp;
10637{
Bram Moolenaar33570922005-01-25 22:26:29 +000010638 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010640 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 s = expr;
10644 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010645 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010646 if (*s != NUL) /* check for trailing chars after expr */
10647 {
10648 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010649 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 }
10651 if (map)
10652 {
10653 /* map(): replace the list item value */
10654 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010655 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010656 *tv = rettv;
10657 }
10658 else
10659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 int error = FALSE;
10661
Bram Moolenaare9a41262005-01-15 22:18:47 +000010662 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010664 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 /* On type error, nothing has been removed; return FAIL to stop the
10666 * loop. The error message was given by get_tv_number_chk(). */
10667 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010668 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010669 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010670 retval = OK;
10671theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010673 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010674}
10675
10676/*
10677 * "filter()" function
10678 */
10679 static void
10680f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010681 typval_T *argvars;
10682 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010683{
10684 filter_map(argvars, rettv, FALSE);
10685}
10686
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688 * "finddir({fname}[, {path}[, {count}]])" function
10689 */
10690 static void
10691f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010692 typval_T *argvars;
10693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010695 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010696}
10697
10698/*
10699 * "findfile({fname}[, {path}[, {count}]])" function
10700 */
10701 static void
10702f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010703 typval_T *argvars;
10704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010705{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010706 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707}
10708
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010709#ifdef FEAT_FLOAT
10710/*
10711 * "float2nr({float})" function
10712 */
10713 static void
10714f_float2nr(argvars, rettv)
10715 typval_T *argvars;
10716 typval_T *rettv;
10717{
10718 float_T f;
10719
10720 if (get_float_arg(argvars, &f) == OK)
10721 {
10722 if (f < -0x7fffffff)
10723 rettv->vval.v_number = -0x7fffffff;
10724 else if (f > 0x7fffffff)
10725 rettv->vval.v_number = 0x7fffffff;
10726 else
10727 rettv->vval.v_number = (varnumber_T)f;
10728 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010729}
10730
10731/*
10732 * "floor({float})" function
10733 */
10734 static void
10735f_floor(argvars, rettv)
10736 typval_T *argvars;
10737 typval_T *rettv;
10738{
10739 float_T f;
10740
10741 rettv->v_type = VAR_FLOAT;
10742 if (get_float_arg(argvars, &f) == OK)
10743 rettv->vval.v_float = floor(f);
10744 else
10745 rettv->vval.v_float = 0.0;
10746}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010747
10748/*
10749 * "fmod()" function
10750 */
10751 static void
10752f_fmod(argvars, rettv)
10753 typval_T *argvars;
10754 typval_T *rettv;
10755{
10756 float_T fx, fy;
10757
10758 rettv->v_type = VAR_FLOAT;
10759 if (get_float_arg(argvars, &fx) == OK
10760 && get_float_arg(&argvars[1], &fy) == OK)
10761 rettv->vval.v_float = fmod(fx, fy);
10762 else
10763 rettv->vval.v_float = 0.0;
10764}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010765#endif
10766
Bram Moolenaar0d660222005-01-07 21:51:51 +000010767/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010768 * "fnameescape({string})" function
10769 */
10770 static void
10771f_fnameescape(argvars, rettv)
10772 typval_T *argvars;
10773 typval_T *rettv;
10774{
10775 rettv->vval.v_string = vim_strsave_fnameescape(
10776 get_tv_string(&argvars[0]), FALSE);
10777 rettv->v_type = VAR_STRING;
10778}
10779
10780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 * "fnamemodify({fname}, {mods})" function
10782 */
10783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010784f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010785 typval_T *argvars;
10786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
10788 char_u *fname;
10789 char_u *mods;
10790 int usedlen = 0;
10791 int len;
10792 char_u *fbuf = NULL;
10793 char_u buf[NUMBUFLEN];
10794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 fname = get_tv_string_chk(&argvars[0]);
10796 mods = get_tv_string_buf_chk(&argvars[1], buf);
10797 if (fname == NULL || mods == NULL)
10798 fname = NULL;
10799 else
10800 {
10801 len = (int)STRLEN(fname);
10802 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 vim_free(fbuf);
10811}
10812
Bram Moolenaar33570922005-01-25 22:26:29 +000010813static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815/*
10816 * "foldclosed()" function
10817 */
10818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010821 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010822 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
10824#ifdef FEAT_FOLDING
10825 linenr_T lnum;
10826 linenr_T first, last;
10827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10830 {
10831 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10832 {
10833 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 return;
10838 }
10839 }
10840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010841 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842}
10843
10844/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010845 * "foldclosed()" function
10846 */
10847 static void
10848f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851{
10852 foldclosed_both(argvars, rettv, FALSE);
10853}
10854
10855/*
10856 * "foldclosedend()" function
10857 */
10858 static void
10859f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010860 typval_T *argvars;
10861 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010862{
10863 foldclosed_both(argvars, rettv, TRUE);
10864}
10865
10866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 * "foldlevel()" function
10868 */
10869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010870f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010871 typval_T *argvars UNUSED;
10872 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
10874#ifdef FEAT_FOLDING
10875 linenr_T lnum;
10876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883/*
10884 * "foldtext()" function
10885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010888 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891#ifdef FEAT_FOLDING
10892 linenr_T lnum;
10893 char_u *s;
10894 char_u *r;
10895 int len;
10896 char *txt;
10897#endif
10898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 rettv->v_type = VAR_STRING;
10900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010902 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10903 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10904 <= curbuf->b_ml.ml_line_count
10905 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 {
10907 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010908 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10909 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 {
10911 if (!linewhite(lnum))
10912 break;
10913 ++lnum;
10914 }
10915
10916 /* Find interesting text in this line. */
10917 s = skipwhite(ml_get(lnum));
10918 /* skip C comment-start */
10919 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010922 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010923 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010924 {
10925 s = skipwhite(ml_get(lnum + 1));
10926 if (*s == '*')
10927 s = skipwhite(s + 1);
10928 }
10929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 txt = _("+-%s%3ld lines: ");
10931 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 + 20 /* for %3ld */
10934 + STRLEN(s))); /* concatenated */
10935 if (r != NULL)
10936 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010937 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10938 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10939 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 len = (int)STRLEN(r);
10941 STRCAT(r, s);
10942 /* remove 'foldmarker' and 'commentstring' */
10943 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 }
10946 }
10947#endif
10948}
10949
10950/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010951 * "foldtextresult(lnum)" function
10952 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010955 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010956 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010957{
10958#ifdef FEAT_FOLDING
10959 linenr_T lnum;
10960 char_u *text;
10961 char_u buf[51];
10962 foldinfo_T foldinfo;
10963 int fold_count;
10964#endif
10965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010966 rettv->v_type = VAR_STRING;
10967 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010968#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010969 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 /* treat illegal types and illegal string values for {lnum} the same */
10971 if (lnum < 0)
10972 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010973 fold_count = foldedCount(curwin, lnum, &foldinfo);
10974 if (fold_count > 0)
10975 {
10976 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10977 &foldinfo, buf);
10978 if (text == buf)
10979 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010981 }
10982#endif
10983}
10984
10985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 * "foreground()" function
10987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010990 typval_T *argvars UNUSED;
10991 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993#ifdef FEAT_GUI
10994 if (gui.in_use)
10995 gui_mch_set_foreground();
10996#else
10997# ifdef WIN32
10998 win32_set_foreground();
10999# endif
11000#endif
11001}
11002
11003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 * "function()" function
11005 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *argvars;
11009 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011010{
11011 char_u *s;
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011014 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011015 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011016 /* Don't check an autoload name for existence here. */
11017 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011018 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011019 else
11020 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011021 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011022 {
11023 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011024 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011025
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011026 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11027 * also be called from another script. Using trans_function_name()
11028 * would also work, but some plugins depend on the name being
11029 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011030 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011031 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011032 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011033 if (rettv->vval.v_string != NULL)
11034 {
11035 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011036 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011037 }
11038 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011039 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011040 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011042 }
11043}
11044
11045/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011046 * "garbagecollect()" function
11047 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011048 static void
11049f_garbagecollect(argvars, rettv)
11050 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011051 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011052{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011053 /* This is postponed until we are back at the toplevel, because we may be
11054 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11055 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011056
11057 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11058 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011059}
11060
11061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011062 * "get()" function
11063 */
11064 static void
11065f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011066 typval_T *argvars;
11067 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011068{
Bram Moolenaar33570922005-01-25 22:26:29 +000011069 listitem_T *li;
11070 list_T *l;
11071 dictitem_T *di;
11072 dict_T *d;
11073 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074
Bram Moolenaare9a41262005-01-15 22:18:47 +000011075 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011076 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011077 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 int error = FALSE;
11080
11081 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11082 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011085 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086 else if (argvars[0].v_type == VAR_DICT)
11087 {
11088 if ((d = argvars[0].vval.v_dict) != NULL)
11089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011090 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 if (di != NULL)
11092 tv = &di->di_tv;
11093 }
11094 }
11095 else
11096 EMSG2(_(e_listdictarg), "get()");
11097
11098 if (tv == NULL)
11099 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 copy_tv(&argvars[2], rettv);
11102 }
11103 else
11104 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011105}
11106
Bram Moolenaar342337a2005-07-21 21:11:17 +000011107static 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 +000011108
11109/*
11110 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011111 * Return a range (from start to end) of lines in rettv from the specified
11112 * buffer.
11113 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011114 */
11115 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011116get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011117 buf_T *buf;
11118 linenr_T start;
11119 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011120 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011121 typval_T *rettv;
11122{
11123 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011124
Bram Moolenaar959a1432013-12-14 12:17:38 +010011125 rettv->v_type = VAR_STRING;
11126 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011127 if (retlist && rettv_list_alloc(rettv) == FAIL)
11128 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011129
11130 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11131 return;
11132
11133 if (!retlist)
11134 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011135 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11136 p = ml_get_buf(buf, start, FALSE);
11137 else
11138 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011139 rettv->vval.v_string = vim_strsave(p);
11140 }
11141 else
11142 {
11143 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 return;
11145
11146 if (start < 1)
11147 start = 1;
11148 if (end > buf->b_ml.ml_line_count)
11149 end = buf->b_ml.ml_line_count;
11150 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011151 if (list_append_string(rettv->vval.v_list,
11152 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011153 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011154 }
11155}
11156
11157/*
11158 * "getbufline()" function
11159 */
11160 static void
11161f_getbufline(argvars, rettv)
11162 typval_T *argvars;
11163 typval_T *rettv;
11164{
11165 linenr_T lnum;
11166 linenr_T end;
11167 buf_T *buf;
11168
11169 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11170 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011171 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011172 --emsg_off;
11173
Bram Moolenaar661b1822005-07-28 22:36:45 +000011174 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011175 if (argvars[2].v_type == VAR_UNKNOWN)
11176 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011177 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011178 end = get_tv_lnum_buf(&argvars[2], buf);
11179
Bram Moolenaar342337a2005-07-21 21:11:17 +000011180 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011181}
11182
Bram Moolenaar0d660222005-01-07 21:51:51 +000011183/*
11184 * "getbufvar()" function
11185 */
11186 static void
11187f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190{
11191 buf_T *buf;
11192 buf_T *save_curbuf;
11193 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011194 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011195 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11198 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011200 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011202 rettv->v_type = VAR_STRING;
11203 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011204
11205 if (buf != NULL && varname != NULL)
11206 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011207 /* set curbuf to be our buf, temporarily */
11208 save_curbuf = curbuf;
11209 curbuf = buf;
11210
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011212 {
11213 if (get_option_tv(&varname, rettv, TRUE) == OK)
11214 done = TRUE;
11215 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011216 else if (STRCMP(varname, "changedtick") == 0)
11217 {
11218 rettv->v_type = VAR_NUMBER;
11219 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011220 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011221 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011222 else
11223 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011224 /* Look up the variable. */
11225 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11226 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11227 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011229 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011230 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011231 done = TRUE;
11232 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011233 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011234
11235 /* restore previous notion of curbuf */
11236 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 }
11238
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011239 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11240 /* use the default value */
11241 copy_tv(&argvars[2], rettv);
11242
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243 --emsg_off;
11244}
11245
11246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 * "getchar()" function
11248 */
11249 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011251 typval_T *argvars;
11252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253{
11254 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011257 /* Position the cursor. Needed after a message that ends in a space. */
11258 windgoto(msg_row, msg_col);
11259
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 ++no_mapping;
11261 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011262 for (;;)
11263 {
11264 if (argvars[0].v_type == VAR_UNKNOWN)
11265 /* getchar(): blocking wait. */
11266 n = safe_vgetc();
11267 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11268 /* getchar(1): only check if char avail */
11269 n = vpeekc();
11270 else if (error || vpeekc() == NUL)
11271 /* illegal argument or getchar(0) and no char avail: return zero */
11272 n = 0;
11273 else
11274 /* getchar(0) and char avail: return char */
11275 n = safe_vgetc();
11276 if (n == K_IGNORE)
11277 continue;
11278 break;
11279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 --no_mapping;
11281 --allow_keys;
11282
Bram Moolenaar219b8702006-11-01 14:32:36 +000011283 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11284 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11285 vimvars[VV_MOUSE_COL].vv_nr = 0;
11286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011287 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 if (IS_SPECIAL(n) || mod_mask != 0)
11289 {
11290 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11291 int i = 0;
11292
11293 /* Turn a special key into three bytes, plus modifier. */
11294 if (mod_mask != 0)
11295 {
11296 temp[i++] = K_SPECIAL;
11297 temp[i++] = KS_MODIFIER;
11298 temp[i++] = mod_mask;
11299 }
11300 if (IS_SPECIAL(n))
11301 {
11302 temp[i++] = K_SPECIAL;
11303 temp[i++] = K_SECOND(n);
11304 temp[i++] = K_THIRD(n);
11305 }
11306#ifdef FEAT_MBYTE
11307 else if (has_mbyte)
11308 i += (*mb_char2bytes)(n, temp + i);
11309#endif
11310 else
11311 temp[i++] = n;
11312 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->v_type = VAR_STRING;
11314 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011315
11316#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011317 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011318 {
11319 int row = mouse_row;
11320 int col = mouse_col;
11321 win_T *win;
11322 linenr_T lnum;
11323# ifdef FEAT_WINDOWS
11324 win_T *wp;
11325# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011326 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011327
11328 if (row >= 0 && col >= 0)
11329 {
11330 /* Find the window at the mouse coordinates and compute the
11331 * text position. */
11332 win = mouse_find_win(&row, &col);
11333 (void)mouse_comp_pos(win, &row, &col, &lnum);
11334# ifdef FEAT_WINDOWS
11335 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011336 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011337# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011338 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011339 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11340 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11341 }
11342 }
11343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 }
11345}
11346
11347/*
11348 * "getcharmod()" function
11349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011352 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356}
11357
11358/*
11359 * "getcmdline()" function
11360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 rettv->v_type = VAR_STRING;
11367 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368}
11369
11370/*
11371 * "getcmdpos()" function
11372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011374f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011375 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379}
11380
11381/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011382 * "getcmdtype()" function
11383 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011384 static void
11385f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011386 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011387 typval_T *rettv;
11388{
11389 rettv->v_type = VAR_STRING;
11390 rettv->vval.v_string = alloc(2);
11391 if (rettv->vval.v_string != NULL)
11392 {
11393 rettv->vval.v_string[0] = get_cmdline_type();
11394 rettv->vval.v_string[1] = NUL;
11395 }
11396}
11397
11398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399 * "getcwd()" function
11400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011403 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011406 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011409 rettv->vval.v_string = NULL;
11410 cwd = alloc(MAXPATHL);
11411 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011413 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11414 {
11415 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011417 if (rettv->vval.v_string != NULL)
11418 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011420 }
11421 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 }
11423}
11424
11425/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011426 * "getfontname()" function
11427 */
11428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011430 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011431 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->v_type = VAR_STRING;
11434 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011435#ifdef FEAT_GUI
11436 if (gui.in_use)
11437 {
11438 GuiFont font;
11439 char_u *name = NULL;
11440
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011442 {
11443 /* Get the "Normal" font. Either the name saved by
11444 * hl_set_font_name() or from the font ID. */
11445 font = gui.norm_font;
11446 name = hl_get_font_name();
11447 }
11448 else
11449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011451 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11452 return;
11453 font = gui_mch_get_font(name, FALSE);
11454 if (font == NOFONT)
11455 return; /* Invalid font name, return empty string. */
11456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011458 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011459 gui_mch_free_font(font);
11460 }
11461#endif
11462}
11463
11464/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011465 * "getfperm({fname})" function
11466 */
11467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *argvars;
11470 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011471{
11472 char_u *fname;
11473 struct stat st;
11474 char_u *perm = NULL;
11475 char_u flags[] = "rwx";
11476 int i;
11477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481 if (mch_stat((char *)fname, &st) >= 0)
11482 {
11483 perm = vim_strsave((char_u *)"---------");
11484 if (perm != NULL)
11485 {
11486 for (i = 0; i < 9; i++)
11487 {
11488 if (st.st_mode & (1 << (8 - i)))
11489 perm[i] = flags[i % 3];
11490 }
11491 }
11492 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011494}
11495
11496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 * "getfsize({fname})" function
11498 */
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 char_u *fname;
11505 struct stat st;
11506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
11511 if (mch_stat((char *)fname, &st) >= 0)
11512 {
11513 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011518
11519 /* non-perfect check for overflow */
11520 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11521 rettv->vval.v_number = -2;
11522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 }
11524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526}
11527
11528/*
11529 * "getftime({fname})" function
11530 */
11531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *argvars;
11534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535{
11536 char_u *fname;
11537 struct stat st;
11538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540
11541 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545}
11546
11547/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011548 * "getftype({fname})" function
11549 */
11550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011554{
11555 char_u *fname;
11556 struct stat st;
11557 char_u *type = NULL;
11558 char *t;
11559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011563 if (mch_lstat((char *)fname, &st) >= 0)
11564 {
11565#ifdef S_ISREG
11566 if (S_ISREG(st.st_mode))
11567 t = "file";
11568 else if (S_ISDIR(st.st_mode))
11569 t = "dir";
11570# ifdef S_ISLNK
11571 else if (S_ISLNK(st.st_mode))
11572 t = "link";
11573# endif
11574# ifdef S_ISBLK
11575 else if (S_ISBLK(st.st_mode))
11576 t = "bdev";
11577# endif
11578# ifdef S_ISCHR
11579 else if (S_ISCHR(st.st_mode))
11580 t = "cdev";
11581# endif
11582# ifdef S_ISFIFO
11583 else if (S_ISFIFO(st.st_mode))
11584 t = "fifo";
11585# endif
11586# ifdef S_ISSOCK
11587 else if (S_ISSOCK(st.st_mode))
11588 t = "fifo";
11589# endif
11590 else
11591 t = "other";
11592#else
11593# ifdef S_IFMT
11594 switch (st.st_mode & S_IFMT)
11595 {
11596 case S_IFREG: t = "file"; break;
11597 case S_IFDIR: t = "dir"; break;
11598# ifdef S_IFLNK
11599 case S_IFLNK: t = "link"; break;
11600# endif
11601# ifdef S_IFBLK
11602 case S_IFBLK: t = "bdev"; break;
11603# endif
11604# ifdef S_IFCHR
11605 case S_IFCHR: t = "cdev"; break;
11606# endif
11607# ifdef S_IFIFO
11608 case S_IFIFO: t = "fifo"; break;
11609# endif
11610# ifdef S_IFSOCK
11611 case S_IFSOCK: t = "socket"; break;
11612# endif
11613 default: t = "other";
11614 }
11615# else
11616 if (mch_isdir(fname))
11617 t = "dir";
11618 else
11619 t = "file";
11620# endif
11621#endif
11622 type = vim_strsave((char_u *)t);
11623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011625}
11626
11627/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011628 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629 */
11630 static void
11631f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011632 typval_T *argvars;
11633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634{
11635 linenr_T lnum;
11636 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011637 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011638
11639 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011640 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011641 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011642 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011643 retlist = FALSE;
11644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011646 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011648 retlist = TRUE;
11649 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011650
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011652}
11653
11654/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011655 * "getmatches()" function
11656 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011657 static void
11658f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011659 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011660 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011661{
11662#ifdef FEAT_SEARCH_EXTRA
11663 dict_T *dict;
11664 matchitem_T *cur = curwin->w_match_head;
11665
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011666 if (rettv_list_alloc(rettv) == OK)
11667 {
11668 while (cur != NULL)
11669 {
11670 dict = dict_alloc();
11671 if (dict == NULL)
11672 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011673 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11674 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11675 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11676 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11677 list_append_dict(rettv->vval.v_list, dict);
11678 cur = cur->next;
11679 }
11680 }
11681#endif
11682}
11683
11684/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011685 * "getpid()" function
11686 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011687 static void
11688f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011689 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011690 typval_T *rettv;
11691{
11692 rettv->vval.v_number = mch_get_pid();
11693}
11694
11695/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011696 * "getpos(string)" function
11697 */
11698 static void
11699f_getpos(argvars, rettv)
11700 typval_T *argvars;
11701 typval_T *rettv;
11702{
11703 pos_T *fp;
11704 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011705 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011706
11707 if (rettv_list_alloc(rettv) == OK)
11708 {
11709 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011710 fp = var2fpos(&argvars[0], TRUE, &fnum);
11711 if (fnum != -1)
11712 list_append_number(l, (varnumber_T)fnum);
11713 else
11714 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011715 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11716 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011717 list_append_number(l, (fp != NULL)
11718 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011719 : (varnumber_T)0);
11720 list_append_number(l,
11721#ifdef FEAT_VIRTUALEDIT
11722 (fp != NULL) ? (varnumber_T)fp->coladd :
11723#endif
11724 (varnumber_T)0);
11725 }
11726 else
11727 rettv->vval.v_number = FALSE;
11728}
11729
11730/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011731 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011732 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011733 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011734f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011735 typval_T *argvars UNUSED;
11736 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011737{
11738#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011739 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011740#endif
11741
Bram Moolenaar2641f772005-03-25 21:58:17 +000011742#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011743 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011744 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011745 wp = NULL;
11746 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11747 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011748 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011749 if (wp == NULL)
11750 return;
11751 }
11752
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011753 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011754 }
11755#endif
11756}
11757
11758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 * "getreg()" function
11760 */
11761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011763 typval_T *argvars;
11764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765{
11766 char_u *strregname;
11767 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011768 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011771 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 strregname = get_tv_string_chk(&argvars[0]);
11774 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 regname = (strregname == NULL ? '"' : *strregname);
11781 if (regname == 0)
11782 regname = '"';
11783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 rettv->vval.v_string = error ? NULL :
11786 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787}
11788
11789/*
11790 * "getregtype()" function
11791 */
11792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 typval_T *argvars;
11795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796{
11797 char_u *strregname;
11798 int regname;
11799 char_u buf[NUMBUFLEN + 2];
11800 long reglen = 0;
11801
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011802 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 {
11804 strregname = get_tv_string_chk(&argvars[0]);
11805 if (strregname == NULL) /* type error; errmsg already given */
11806 {
11807 rettv->v_type = VAR_STRING;
11808 rettv->vval.v_string = NULL;
11809 return;
11810 }
11811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 else
11813 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011814 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815
11816 regname = (strregname == NULL ? '"' : *strregname);
11817 if (regname == 0)
11818 regname = '"';
11819
11820 buf[0] = NUL;
11821 buf[1] = NUL;
11822 switch (get_reg_type(regname, &reglen))
11823 {
11824 case MLINE: buf[0] = 'V'; break;
11825 case MCHAR: buf[0] = 'v'; break;
11826#ifdef FEAT_VISUAL
11827 case MBLOCK:
11828 buf[0] = Ctrl_V;
11829 sprintf((char *)buf + 1, "%ld", reglen + 1);
11830 break;
11831#endif
11832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->v_type = VAR_STRING;
11834 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011835}
11836
11837/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011838 * "gettabvar()" function
11839 */
11840 static void
11841f_gettabvar(argvars, rettv)
11842 typval_T *argvars;
11843 typval_T *rettv;
11844{
11845 tabpage_T *tp;
11846 dictitem_T *v;
11847 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011848 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011849
11850 rettv->v_type = VAR_STRING;
11851 rettv->vval.v_string = NULL;
11852
11853 varname = get_tv_string_chk(&argvars[1]);
11854 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11855 if (tp != NULL && varname != NULL)
11856 {
11857 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011858 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011859 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011860 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011861 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011862 done = TRUE;
11863 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011864 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011865
11866 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011867 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011868}
11869
11870/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011871 * "gettabwinvar()" function
11872 */
11873 static void
11874f_gettabwinvar(argvars, rettv)
11875 typval_T *argvars;
11876 typval_T *rettv;
11877{
11878 getwinvar(argvars, rettv, 1);
11879}
11880
11881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 * "getwinposx()" function
11883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011886 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890#ifdef FEAT_GUI
11891 if (gui.in_use)
11892 {
11893 int x, y;
11894
11895 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 }
11898#endif
11899}
11900
11901/*
11902 * "getwinposy()" function
11903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011909 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910#ifdef FEAT_GUI
11911 if (gui.in_use)
11912 {
11913 int x, y;
11914
11915 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 }
11918#endif
11919}
11920
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011921/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011922 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011923 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011924 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011925find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011926 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011927 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011928{
11929#ifdef FEAT_WINDOWS
11930 win_T *wp;
11931#endif
11932 int nr;
11933
11934 nr = get_tv_number_chk(vp, NULL);
11935
11936#ifdef FEAT_WINDOWS
11937 if (nr < 0)
11938 return NULL;
11939 if (nr == 0)
11940 return curwin;
11941
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011942 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11943 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011944 if (--nr <= 0)
11945 break;
11946 return wp;
11947#else
11948 if (nr == 0 || nr == 1)
11949 return curwin;
11950 return NULL;
11951#endif
11952}
11953
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954/*
11955 * "getwinvar()" function
11956 */
11957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 typval_T *argvars;
11960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011962 getwinvar(argvars, rettv, 0);
11963}
11964
11965/*
11966 * getwinvar() and gettabwinvar()
11967 */
11968 static void
11969getwinvar(argvars, rettv, off)
11970 typval_T *argvars;
11971 typval_T *rettv;
11972 int off; /* 1 for gettabwinvar() */
11973{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 win_T *win, *oldcurwin;
11975 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011976 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011977 tabpage_T *tp = NULL;
11978 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011979 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011981#ifdef FEAT_WINDOWS
11982 if (off == 1)
11983 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11984 else
11985 tp = curtab;
11986#endif
11987 win = find_win_by_nr(&argvars[off], tp);
11988 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011991 rettv->v_type = VAR_STRING;
11992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993
11994 if (win != NULL && varname != NULL)
11995 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011996 /* Set curwin to be our win, temporarily. Also set the tabpage,
11997 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011998 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011999
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012001 {
12002 if (get_option_tv(&varname, rettv, 1) == OK)
12003 done = TRUE;
12004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 else
12006 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012007 /* Look up the variable. */
12008 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12009 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012011 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012012 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012013 done = TRUE;
12014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012016
12017 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012018 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 }
12020
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012021 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12022 /* use the default return value */
12023 copy_tv(&argvars[off + 2], rettv);
12024
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 --emsg_off;
12026}
12027
12028/*
12029 * "glob()" function
12030 */
12031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012033 typval_T *argvars;
12034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012036 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012038 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012040 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012041 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012043 if (argvars[1].v_type != VAR_UNKNOWN)
12044 {
12045 if (get_tv_number_chk(&argvars[1], &error))
12046 options |= WILD_KEEP_ALL;
12047 if (argvars[2].v_type != VAR_UNKNOWN
12048 && get_tv_number_chk(&argvars[2], &error))
12049 {
12050 rettv->v_type = VAR_LIST;
12051 rettv->vval.v_list = NULL;
12052 }
12053 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012054 if (!error)
12055 {
12056 ExpandInit(&xpc);
12057 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012058 if (p_wic)
12059 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012060 if (rettv->v_type == VAR_STRING)
12061 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012062 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012063 else if (rettv_list_alloc(rettv) != FAIL)
12064 {
12065 int i;
12066
12067 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12068 NULL, options, WILD_ALL_KEEP);
12069 for (i = 0; i < xpc.xp_numfiles; i++)
12070 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12071
12072 ExpandCleanup(&xpc);
12073 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012074 }
12075 else
12076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077}
12078
12079/*
12080 * "globpath()" function
12081 */
12082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 typval_T *argvars;
12085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012087 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012090 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012092 /* When the optional second argument is non-zero, don't remove matches
12093 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12094 if (argvars[2].v_type != VAR_UNKNOWN
12095 && get_tv_number_chk(&argvars[2], &error))
12096 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012098 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 rettv->vval.v_string = NULL;
12100 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012101 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12102 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103}
12104
12105/*
12106 * "has()" function
12107 */
12108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012110 typval_T *argvars;
12111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112{
12113 int i;
12114 char_u *name;
12115 int n = FALSE;
12116 static char *(has_list[]) =
12117 {
12118#ifdef AMIGA
12119 "amiga",
12120# ifdef FEAT_ARP
12121 "arp",
12122# endif
12123#endif
12124#ifdef __BEOS__
12125 "beos",
12126#endif
12127#ifdef MSDOS
12128# ifdef DJGPP
12129 "dos32",
12130# else
12131 "dos16",
12132# endif
12133#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012134#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 "mac",
12136#endif
12137#if defined(MACOS_X_UNIX)
12138 "macunix",
12139#endif
12140#ifdef OS2
12141 "os2",
12142#endif
12143#ifdef __QNX__
12144 "qnx",
12145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146#ifdef UNIX
12147 "unix",
12148#endif
12149#ifdef VMS
12150 "vms",
12151#endif
12152#ifdef WIN16
12153 "win16",
12154#endif
12155#ifdef WIN32
12156 "win32",
12157#endif
12158#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12159 "win32unix",
12160#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012161#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 "win64",
12163#endif
12164#ifdef EBCDIC
12165 "ebcdic",
12166#endif
12167#ifndef CASE_INSENSITIVE_FILENAME
12168 "fname_case",
12169#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012170#ifdef HAVE_ACL
12171 "acl",
12172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173#ifdef FEAT_ARABIC
12174 "arabic",
12175#endif
12176#ifdef FEAT_AUTOCMD
12177 "autocmd",
12178#endif
12179#ifdef FEAT_BEVAL
12180 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012181# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12182 "balloon_multiline",
12183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184#endif
12185#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12186 "builtin_terms",
12187# ifdef ALL_BUILTIN_TCAPS
12188 "all_builtin_terms",
12189# endif
12190#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012191#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12192 || defined(FEAT_GUI_W32) \
12193 || defined(FEAT_GUI_MOTIF))
12194 "browsefilter",
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196#ifdef FEAT_BYTEOFF
12197 "byte_offset",
12198#endif
12199#ifdef FEAT_CINDENT
12200 "cindent",
12201#endif
12202#ifdef FEAT_CLIENTSERVER
12203 "clientserver",
12204#endif
12205#ifdef FEAT_CLIPBOARD
12206 "clipboard",
12207#endif
12208#ifdef FEAT_CMDL_COMPL
12209 "cmdline_compl",
12210#endif
12211#ifdef FEAT_CMDHIST
12212 "cmdline_hist",
12213#endif
12214#ifdef FEAT_COMMENTS
12215 "comments",
12216#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012217#ifdef FEAT_CONCEAL
12218 "conceal",
12219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220#ifdef FEAT_CRYPT
12221 "cryptv",
12222#endif
12223#ifdef FEAT_CSCOPE
12224 "cscope",
12225#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012226#ifdef FEAT_CURSORBIND
12227 "cursorbind",
12228#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012229#ifdef CURSOR_SHAPE
12230 "cursorshape",
12231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#ifdef DEBUG
12233 "debug",
12234#endif
12235#ifdef FEAT_CON_DIALOG
12236 "dialog_con",
12237#endif
12238#ifdef FEAT_GUI_DIALOG
12239 "dialog_gui",
12240#endif
12241#ifdef FEAT_DIFF
12242 "diff",
12243#endif
12244#ifdef FEAT_DIGRAPHS
12245 "digraphs",
12246#endif
12247#ifdef FEAT_DND
12248 "dnd",
12249#endif
12250#ifdef FEAT_EMACS_TAGS
12251 "emacs_tags",
12252#endif
12253 "eval", /* always present, of course! */
12254#ifdef FEAT_EX_EXTRA
12255 "ex_extra",
12256#endif
12257#ifdef FEAT_SEARCH_EXTRA
12258 "extra_search",
12259#endif
12260#ifdef FEAT_FKMAP
12261 "farsi",
12262#endif
12263#ifdef FEAT_SEARCHPATH
12264 "file_in_path",
12265#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012266#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012267 "filterpipe",
12268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#ifdef FEAT_FIND_ID
12270 "find_in_path",
12271#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012272#ifdef FEAT_FLOAT
12273 "float",
12274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275#ifdef FEAT_FOLDING
12276 "folding",
12277#endif
12278#ifdef FEAT_FOOTER
12279 "footer",
12280#endif
12281#if !defined(USE_SYSTEM) && defined(UNIX)
12282 "fork",
12283#endif
12284#ifdef FEAT_GETTEXT
12285 "gettext",
12286#endif
12287#ifdef FEAT_GUI
12288 "gui",
12289#endif
12290#ifdef FEAT_GUI_ATHENA
12291# ifdef FEAT_GUI_NEXTAW
12292 "gui_neXtaw",
12293# else
12294 "gui_athena",
12295# endif
12296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297#ifdef FEAT_GUI_GTK
12298 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012301#ifdef FEAT_GUI_GNOME
12302 "gui_gnome",
12303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304#ifdef FEAT_GUI_MAC
12305 "gui_mac",
12306#endif
12307#ifdef FEAT_GUI_MOTIF
12308 "gui_motif",
12309#endif
12310#ifdef FEAT_GUI_PHOTON
12311 "gui_photon",
12312#endif
12313#ifdef FEAT_GUI_W16
12314 "gui_win16",
12315#endif
12316#ifdef FEAT_GUI_W32
12317 "gui_win32",
12318#endif
12319#ifdef FEAT_HANGULIN
12320 "hangul_input",
12321#endif
12322#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12323 "iconv",
12324#endif
12325#ifdef FEAT_INS_EXPAND
12326 "insert_expand",
12327#endif
12328#ifdef FEAT_JUMPLIST
12329 "jumplist",
12330#endif
12331#ifdef FEAT_KEYMAP
12332 "keymap",
12333#endif
12334#ifdef FEAT_LANGMAP
12335 "langmap",
12336#endif
12337#ifdef FEAT_LIBCALL
12338 "libcall",
12339#endif
12340#ifdef FEAT_LINEBREAK
12341 "linebreak",
12342#endif
12343#ifdef FEAT_LISP
12344 "lispindent",
12345#endif
12346#ifdef FEAT_LISTCMDS
12347 "listcmds",
12348#endif
12349#ifdef FEAT_LOCALMAP
12350 "localmap",
12351#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012352#ifdef FEAT_LUA
12353# ifndef DYNAMIC_LUA
12354 "lua",
12355# endif
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef FEAT_MENU
12358 "menu",
12359#endif
12360#ifdef FEAT_SESSION
12361 "mksession",
12362#endif
12363#ifdef FEAT_MODIFY_FNAME
12364 "modify_fname",
12365#endif
12366#ifdef FEAT_MOUSE
12367 "mouse",
12368#endif
12369#ifdef FEAT_MOUSESHAPE
12370 "mouseshape",
12371#endif
12372#if defined(UNIX) || defined(VMS)
12373# ifdef FEAT_MOUSE_DEC
12374 "mouse_dec",
12375# endif
12376# ifdef FEAT_MOUSE_GPM
12377 "mouse_gpm",
12378# endif
12379# ifdef FEAT_MOUSE_JSB
12380 "mouse_jsbterm",
12381# endif
12382# ifdef FEAT_MOUSE_NET
12383 "mouse_netterm",
12384# endif
12385# ifdef FEAT_MOUSE_PTERM
12386 "mouse_pterm",
12387# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012388# ifdef FEAT_MOUSE_SGR
12389 "mouse_sgr",
12390# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012391# ifdef FEAT_SYSMOUSE
12392 "mouse_sysmouse",
12393# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012394# ifdef FEAT_MOUSE_URXVT
12395 "mouse_urxvt",
12396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397# ifdef FEAT_MOUSE_XTERM
12398 "mouse_xterm",
12399# endif
12400#endif
12401#ifdef FEAT_MBYTE
12402 "multi_byte",
12403#endif
12404#ifdef FEAT_MBYTE_IME
12405 "multi_byte_ime",
12406#endif
12407#ifdef FEAT_MULTI_LANG
12408 "multi_lang",
12409#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012410#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012411#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012412 "mzscheme",
12413#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415#ifdef FEAT_OLE
12416 "ole",
12417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418#ifdef FEAT_PATH_EXTRA
12419 "path_extra",
12420#endif
12421#ifdef FEAT_PERL
12422#ifndef DYNAMIC_PERL
12423 "perl",
12424#endif
12425#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012426#ifdef FEAT_PERSISTENT_UNDO
12427 "persistent_undo",
12428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_PYTHON
12430#ifndef DYNAMIC_PYTHON
12431 "python",
12432#endif
12433#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012434#ifdef FEAT_PYTHON3
12435#ifndef DYNAMIC_PYTHON3
12436 "python3",
12437#endif
12438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef FEAT_POSTSCRIPT
12440 "postscript",
12441#endif
12442#ifdef FEAT_PRINTER
12443 "printer",
12444#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012445#ifdef FEAT_PROFILE
12446 "profile",
12447#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012448#ifdef FEAT_RELTIME
12449 "reltime",
12450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451#ifdef FEAT_QUICKFIX
12452 "quickfix",
12453#endif
12454#ifdef FEAT_RIGHTLEFT
12455 "rightleft",
12456#endif
12457#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12458 "ruby",
12459#endif
12460#ifdef FEAT_SCROLLBIND
12461 "scrollbind",
12462#endif
12463#ifdef FEAT_CMDL_INFO
12464 "showcmd",
12465 "cmdline_info",
12466#endif
12467#ifdef FEAT_SIGNS
12468 "signs",
12469#endif
12470#ifdef FEAT_SMARTINDENT
12471 "smartindent",
12472#endif
12473#ifdef FEAT_SNIFF
12474 "sniff",
12475#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012476#ifdef STARTUPTIME
12477 "startuptime",
12478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479#ifdef FEAT_STL_OPT
12480 "statusline",
12481#endif
12482#ifdef FEAT_SUN_WORKSHOP
12483 "sun_workshop",
12484#endif
12485#ifdef FEAT_NETBEANS_INTG
12486 "netbeans_intg",
12487#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012488#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012489 "spell",
12490#endif
12491#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 "syntax",
12493#endif
12494#if defined(USE_SYSTEM) || !defined(UNIX)
12495 "system",
12496#endif
12497#ifdef FEAT_TAG_BINS
12498 "tag_binary",
12499#endif
12500#ifdef FEAT_TAG_OLDSTATIC
12501 "tag_old_static",
12502#endif
12503#ifdef FEAT_TAG_ANYWHITE
12504 "tag_any_white",
12505#endif
12506#ifdef FEAT_TCL
12507# ifndef DYNAMIC_TCL
12508 "tcl",
12509# endif
12510#endif
12511#ifdef TERMINFO
12512 "terminfo",
12513#endif
12514#ifdef FEAT_TERMRESPONSE
12515 "termresponse",
12516#endif
12517#ifdef FEAT_TEXTOBJ
12518 "textobjects",
12519#endif
12520#ifdef HAVE_TGETENT
12521 "tgetent",
12522#endif
12523#ifdef FEAT_TITLE
12524 "title",
12525#endif
12526#ifdef FEAT_TOOLBAR
12527 "toolbar",
12528#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012529#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12530 "unnamedplus",
12531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532#ifdef FEAT_USR_CMDS
12533 "user-commands", /* was accidentally included in 5.4 */
12534 "user_commands",
12535#endif
12536#ifdef FEAT_VIMINFO
12537 "viminfo",
12538#endif
12539#ifdef FEAT_VERTSPLIT
12540 "vertsplit",
12541#endif
12542#ifdef FEAT_VIRTUALEDIT
12543 "virtualedit",
12544#endif
12545#ifdef FEAT_VISUAL
12546 "visual",
12547#endif
12548#ifdef FEAT_VISUALEXTRA
12549 "visualextra",
12550#endif
12551#ifdef FEAT_VREPLACE
12552 "vreplace",
12553#endif
12554#ifdef FEAT_WILDIGN
12555 "wildignore",
12556#endif
12557#ifdef FEAT_WILDMENU
12558 "wildmenu",
12559#endif
12560#ifdef FEAT_WINDOWS
12561 "windows",
12562#endif
12563#ifdef FEAT_WAK
12564 "winaltkeys",
12565#endif
12566#ifdef FEAT_WRITEBACKUP
12567 "writebackup",
12568#endif
12569#ifdef FEAT_XIM
12570 "xim",
12571#endif
12572#ifdef FEAT_XFONTSET
12573 "xfontset",
12574#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012575#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012576 "xpm",
12577 "xpm_w32", /* for backward compatibility */
12578#else
12579# if defined(HAVE_XPM)
12580 "xpm",
12581# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#ifdef USE_XSMP
12584 "xsmp",
12585#endif
12586#ifdef USE_XSMP_INTERACT
12587 "xsmp_interact",
12588#endif
12589#ifdef FEAT_XCLIPBOARD
12590 "xterm_clipboard",
12591#endif
12592#ifdef FEAT_XTERM_SAVE
12593 "xterm_save",
12594#endif
12595#if defined(UNIX) && defined(FEAT_X11)
12596 "X11",
12597#endif
12598 NULL
12599 };
12600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012601 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 for (i = 0; has_list[i] != NULL; ++i)
12603 if (STRICMP(name, has_list[i]) == 0)
12604 {
12605 n = TRUE;
12606 break;
12607 }
12608
12609 if (n == FALSE)
12610 {
12611 if (STRNICMP(name, "patch", 5) == 0)
12612 n = has_patch(atoi((char *)name + 5));
12613 else if (STRICMP(name, "vim_starting") == 0)
12614 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012615#ifdef FEAT_MBYTE
12616 else if (STRICMP(name, "multi_byte_encoding") == 0)
12617 n = has_mbyte;
12618#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012619#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12620 else if (STRICMP(name, "balloon_multiline") == 0)
12621 n = multiline_balloon_available();
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623#ifdef DYNAMIC_TCL
12624 else if (STRICMP(name, "tcl") == 0)
12625 n = tcl_enabled(FALSE);
12626#endif
12627#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12628 else if (STRICMP(name, "iconv") == 0)
12629 n = iconv_enabled(FALSE);
12630#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012631#ifdef DYNAMIC_LUA
12632 else if (STRICMP(name, "lua") == 0)
12633 n = lua_enabled(FALSE);
12634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012635#ifdef DYNAMIC_MZSCHEME
12636 else if (STRICMP(name, "mzscheme") == 0)
12637 n = mzscheme_enabled(FALSE);
12638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639#ifdef DYNAMIC_RUBY
12640 else if (STRICMP(name, "ruby") == 0)
12641 n = ruby_enabled(FALSE);
12642#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012643#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644#ifdef DYNAMIC_PYTHON
12645 else if (STRICMP(name, "python") == 0)
12646 n = python_enabled(FALSE);
12647#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012648#endif
12649#ifdef FEAT_PYTHON3
12650#ifdef DYNAMIC_PYTHON3
12651 else if (STRICMP(name, "python3") == 0)
12652 n = python3_enabled(FALSE);
12653#endif
12654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655#ifdef DYNAMIC_PERL
12656 else if (STRICMP(name, "perl") == 0)
12657 n = perl_enabled(FALSE);
12658#endif
12659#ifdef FEAT_GUI
12660 else if (STRICMP(name, "gui_running") == 0)
12661 n = (gui.in_use || gui.starting);
12662# ifdef FEAT_GUI_W32
12663 else if (STRICMP(name, "gui_win32s") == 0)
12664 n = gui_is_win32s();
12665# endif
12666# ifdef FEAT_BROWSE
12667 else if (STRICMP(name, "browse") == 0)
12668 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12669# endif
12670#endif
12671#ifdef FEAT_SYN_HL
12672 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012673 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674#endif
12675#if defined(WIN3264)
12676 else if (STRICMP(name, "win95") == 0)
12677 n = mch_windows95();
12678#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012679#ifdef FEAT_NETBEANS_INTG
12680 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012681 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 }
12684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686}
12687
12688/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012689 * "has_key()" function
12690 */
12691 static void
12692f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012696 if (argvars[0].v_type != VAR_DICT)
12697 {
12698 EMSG(_(e_dictreq));
12699 return;
12700 }
12701 if (argvars[0].vval.v_dict == NULL)
12702 return;
12703
12704 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012705 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012706}
12707
12708/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012709 * "haslocaldir()" function
12710 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012711 static void
12712f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012713 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012714 typval_T *rettv;
12715{
12716 rettv->vval.v_number = (curwin->w_localdir != NULL);
12717}
12718
12719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 * "hasmapto()" function
12721 */
12722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012724 typval_T *argvars;
12725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726{
12727 char_u *name;
12728 char_u *mode;
12729 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012730 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 mode = (char_u *)"nvo";
12735 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012736 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012737 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012738 if (argvars[2].v_type != VAR_UNKNOWN)
12739 abbr = get_tv_number(&argvars[2]);
12740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
Bram Moolenaar2c932302006-03-18 21:42:09 +000012742 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746}
12747
12748/*
12749 * "histadd()" function
12750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755{
12756#ifdef FEAT_CMDHIST
12757 int histype;
12758 char_u *str;
12759 char_u buf[NUMBUFLEN];
12760#endif
12761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 if (check_restricted() || check_secure())
12764 return;
12765#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012766 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12767 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 if (histype >= 0)
12769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 if (*str != NUL)
12772 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012773 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 return;
12777 }
12778 }
12779#endif
12780}
12781
12782/*
12783 * "histdel()" function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
12788 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790#ifdef FEAT_CMDHIST
12791 int n;
12792 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12796 if (str == NULL)
12797 n = 0;
12798 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012803 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 else
12806 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012807 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 get_tv_string_buf(&argvars[1], buf));
12809 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810#endif
12811}
12812
12813/*
12814 * "histget()" function
12815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
12821#ifdef FEAT_CMDHIST
12822 int type;
12823 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012826 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12827 if (str == NULL)
12828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012830 {
12831 type = get_histtype(str);
12832 if (argvars[1].v_type == VAR_UNKNOWN)
12833 idx = get_history_idx(type);
12834 else
12835 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12836 /* -1 on type error */
12837 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843}
12844
12845/*
12846 * "histnr()" function
12847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012850 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852{
12853 int i;
12854
12855#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 char_u *history = get_tv_string_chk(&argvars[0]);
12857
12858 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 if (i >= HIST_CMD && i < HIST_COUNT)
12860 i = get_history_idx(i);
12861 else
12862#endif
12863 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865}
12866
12867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 * "highlightID(name)" function
12869 */
12870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012872 typval_T *argvars;
12873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876}
12877
12878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012879 * "highlight_exists()" function
12880 */
12881 static void
12882f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012883 typval_T *argvars;
12884 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012885{
12886 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12887}
12888
12889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 * "hostname()" function
12891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012894 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
12897 char_u hostname[256];
12898
12899 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->v_type = VAR_STRING;
12901 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902}
12903
12904/*
12905 * iconv() function
12906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012909 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911{
12912#ifdef FEAT_MBYTE
12913 char_u buf1[NUMBUFLEN];
12914 char_u buf2[NUMBUFLEN];
12915 char_u *from, *to, *str;
12916 vimconv_T vimconv;
12917#endif
12918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->v_type = VAR_STRING;
12920 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921
12922#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 str = get_tv_string(&argvars[0]);
12924 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12925 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926 vimconv.vc_type = CONV_NONE;
12927 convert_setup(&vimconv, from, to);
12928
12929 /* If the encodings are equal, no conversion needed. */
12930 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934
12935 convert_setup(&vimconv, NULL, NULL);
12936 vim_free(from);
12937 vim_free(to);
12938#endif
12939}
12940
12941/*
12942 * "indent()" function
12943 */
12944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012946 typval_T *argvars;
12947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948{
12949 linenr_T lnum;
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956}
12957
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012958/*
12959 * "index()" function
12960 */
12961 static void
12962f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012963 typval_T *argvars;
12964 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012965{
Bram Moolenaar33570922005-01-25 22:26:29 +000012966 list_T *l;
12967 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012968 long idx = 0;
12969 int ic = FALSE;
12970
12971 rettv->vval.v_number = -1;
12972 if (argvars[0].v_type != VAR_LIST)
12973 {
12974 EMSG(_(e_listreq));
12975 return;
12976 }
12977 l = argvars[0].vval.v_list;
12978 if (l != NULL)
12979 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012980 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012983 int error = FALSE;
12984
Bram Moolenaar758711c2005-02-02 23:11:38 +000012985 /* Start at specified item. Use the cached index that list_find()
12986 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012988 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012989 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012990 ic = get_tv_number_chk(&argvars[3], &error);
12991 if (error)
12992 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012993 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012994
Bram Moolenaar758711c2005-02-02 23:11:38 +000012995 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012996 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012997 {
12998 rettv->vval.v_number = idx;
12999 break;
13000 }
13001 }
13002}
13003
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004static int inputsecret_flag = 0;
13005
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013006static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13007
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013009 * This function is used by f_input() and f_inputdialog() functions. The third
13010 * argument to f_input() specifies the type of completion to use at the
13011 * prompt. The third argument to f_inputdialog() specifies the value to return
13012 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 */
13014 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013015get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013016 typval_T *argvars;
13017 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 char_u *p = NULL;
13022 int c;
13023 char_u buf[NUMBUFLEN];
13024 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026 int xp_type = EXPAND_NOTHING;
13027 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013029 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031
13032#ifdef NO_CONSOLE_INPUT
13033 /* While starting up, there is no place to enter text. */
13034 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036#endif
13037
13038 cmd_silent = FALSE; /* Want to see the prompt. */
13039 if (prompt != NULL)
13040 {
13041 /* Only the part of the message after the last NL is considered as
13042 * prompt for the command line */
13043 p = vim_strrchr(prompt, '\n');
13044 if (p == NULL)
13045 p = prompt;
13046 else
13047 {
13048 ++p;
13049 c = *p;
13050 *p = NUL;
13051 msg_start();
13052 msg_clr_eos();
13053 msg_puts_attr(prompt, echo_attr);
13054 msg_didout = FALSE;
13055 msg_starthere();
13056 *p = c;
13057 }
13058 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 if (argvars[1].v_type != VAR_UNKNOWN)
13061 {
13062 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13063 if (defstr != NULL)
13064 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013066 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013067 {
13068 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013069 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013070 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013071
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013072 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013073 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013074
Bram Moolenaar4463f292005-09-25 22:20:24 +000013075 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13076 if (xp_name == NULL)
13077 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013079 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013080
Bram Moolenaar4463f292005-09-25 22:20:24 +000013081 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13082 &xp_arg) == FAIL)
13083 return;
13084 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013085 }
13086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013088 {
13089# ifdef FEAT_EX_EXTRA
13090 int save_ex_normal_busy = ex_normal_busy;
13091 ex_normal_busy = 0;
13092# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013093 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013094 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13095 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013096# ifdef FEAT_EX_EXTRA
13097 ex_normal_busy = save_ex_normal_busy;
13098# endif
13099 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013100 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013101 && argvars[1].v_type != VAR_UNKNOWN
13102 && argvars[2].v_type != VAR_UNKNOWN)
13103 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13104 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013105
13106 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 /* since the user typed this, no need to wait for return */
13109 need_wait_return = FALSE;
13110 msg_didout = FALSE;
13111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 cmd_silent = cmd_silent_save;
13113}
13114
13115/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013116 * "input()" function
13117 * Also handles inputsecret() when inputsecret is set.
13118 */
13119 static void
13120f_input(argvars, rettv)
13121 typval_T *argvars;
13122 typval_T *rettv;
13123{
13124 get_user_input(argvars, rettv, FALSE);
13125}
13126
13127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128 * "inputdialog()" function
13129 */
13130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013132 typval_T *argvars;
13133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134{
13135#if defined(FEAT_GUI_TEXTDIALOG)
13136 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13137 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13138 {
13139 char_u *message;
13140 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013141 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013143 message = get_tv_string_chk(&argvars[0]);
13144 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013145 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013146 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 else
13148 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013149 if (message != NULL && defstr != NULL
13150 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013151 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 else
13154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 if (message != NULL && defstr != NULL
13156 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_string = vim_strsave(
13159 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 }
13165 else
13166#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013167 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168}
13169
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013170/*
13171 * "inputlist()" function
13172 */
13173 static void
13174f_inputlist(argvars, rettv)
13175 typval_T *argvars;
13176 typval_T *rettv;
13177{
13178 listitem_T *li;
13179 int selected;
13180 int mouse_used;
13181
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013182#ifdef NO_CONSOLE_INPUT
13183 /* While starting up, there is no place to enter text. */
13184 if (no_console_input())
13185 return;
13186#endif
13187 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13188 {
13189 EMSG2(_(e_listarg), "inputlist()");
13190 return;
13191 }
13192
13193 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013194 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013195 lines_left = Rows; /* avoid more prompt */
13196 msg_scroll = TRUE;
13197 msg_clr_eos();
13198
13199 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13200 {
13201 msg_puts(get_tv_string(&li->li_tv));
13202 msg_putchar('\n');
13203 }
13204
13205 /* Ask for choice. */
13206 selected = prompt_for_number(&mouse_used);
13207 if (mouse_used)
13208 selected -= lines_left;
13209
13210 rettv->vval.v_number = selected;
13211}
13212
13213
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13215
13216/*
13217 * "inputrestore()" function
13218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013221 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223{
13224 if (ga_userinput.ga_len > 0)
13225 {
13226 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13228 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013229 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 }
13231 else if (p_verbose > 1)
13232 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013233 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
13236}
13237
13238/*
13239 * "inputsave()" function
13240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013243 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013246 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 if (ga_grow(&ga_userinput, 1) == OK)
13248 {
13249 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13250 + ga_userinput.ga_len);
13251 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013252 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 }
13254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
13259 * "inputsecret()" function
13260 */
13261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *argvars;
13264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266 ++cmdline_star;
13267 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 --cmdline_star;
13270 --inputsecret_flag;
13271}
13272
13273/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013274 * "insert()" function
13275 */
13276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013278 typval_T *argvars;
13279 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280{
13281 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 listitem_T *item;
13283 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013285
13286 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013287 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013288 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013289 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290 {
13291 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013292 before = get_tv_number_chk(&argvars[2], &error);
13293 if (error)
13294 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013295
Bram Moolenaar758711c2005-02-02 23:11:38 +000013296 if (before == l->lv_len)
13297 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013298 else
13299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013300 item = list_find(l, before);
13301 if (item == NULL)
13302 {
13303 EMSGN(_(e_listidx), before);
13304 l = NULL;
13305 }
13306 }
13307 if (l != NULL)
13308 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013309 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013310 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013311 }
13312 }
13313}
13314
13315/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013316 * "invert(expr)" function
13317 */
13318 static void
13319f_invert(argvars, rettv)
13320 typval_T *argvars;
13321 typval_T *rettv;
13322{
13323 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13324}
13325
13326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 * "isdirectory()" function
13328 */
13329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013331 typval_T *argvars;
13332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335}
13336
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013337/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013338 * "islocked()" function
13339 */
13340 static void
13341f_islocked(argvars, rettv)
13342 typval_T *argvars;
13343 typval_T *rettv;
13344{
13345 lval_T lv;
13346 char_u *end;
13347 dictitem_T *di;
13348
13349 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013350 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13351 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013352 if (end != NULL && lv.ll_name != NULL)
13353 {
13354 if (*end != NUL)
13355 EMSG(_(e_trailing));
13356 else
13357 {
13358 if (lv.ll_tv == NULL)
13359 {
13360 if (check_changedtick(lv.ll_name))
13361 rettv->vval.v_number = 1; /* always locked */
13362 else
13363 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013364 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013365 if (di != NULL)
13366 {
13367 /* Consider a variable locked when:
13368 * 1. the variable itself is locked
13369 * 2. the value of the variable is locked.
13370 * 3. the List or Dict value is locked.
13371 */
13372 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13373 || tv_islocked(&di->di_tv));
13374 }
13375 }
13376 }
13377 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013378 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013379 else if (lv.ll_newkey != NULL)
13380 EMSG2(_(e_dictkey), lv.ll_newkey);
13381 else if (lv.ll_list != NULL)
13382 /* List item. */
13383 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13384 else
13385 /* Dictionary item. */
13386 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13387 }
13388 }
13389
13390 clear_lval(&lv);
13391}
13392
Bram Moolenaar33570922005-01-25 22:26:29 +000013393static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013394
13395/*
13396 * Turn a dict into a list:
13397 * "what" == 0: list of keys
13398 * "what" == 1: list of values
13399 * "what" == 2: list of items
13400 */
13401 static void
13402dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 typval_T *argvars;
13404 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013405 int what;
13406{
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 list_T *l2;
13408 dictitem_T *di;
13409 hashitem_T *hi;
13410 listitem_T *li;
13411 listitem_T *li2;
13412 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013413 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013414
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415 if (argvars[0].v_type != VAR_DICT)
13416 {
13417 EMSG(_(e_dictreq));
13418 return;
13419 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013420 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013421 return;
13422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013423 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013424 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013426 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013427 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013429 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013431 --todo;
13432 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013434 li = listitem_alloc();
13435 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013437 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013438
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013439 if (what == 0)
13440 {
13441 /* keys() */
13442 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013443 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013444 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13445 }
13446 else if (what == 1)
13447 {
13448 /* values() */
13449 copy_tv(&di->di_tv, &li->li_tv);
13450 }
13451 else
13452 {
13453 /* items() */
13454 l2 = list_alloc();
13455 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013456 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013457 li->li_tv.vval.v_list = l2;
13458 if (l2 == NULL)
13459 break;
13460 ++l2->lv_refcount;
13461
13462 li2 = listitem_alloc();
13463 if (li2 == NULL)
13464 break;
13465 list_append(l2, li2);
13466 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013467 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013468 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13469
13470 li2 = listitem_alloc();
13471 if (li2 == NULL)
13472 break;
13473 list_append(l2, li2);
13474 copy_tv(&di->di_tv, &li2->li_tv);
13475 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013476 }
13477 }
13478}
13479
13480/*
13481 * "items(dict)" function
13482 */
13483 static void
13484f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013485 typval_T *argvars;
13486 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013487{
13488 dict_list(argvars, rettv, 2);
13489}
13490
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013492 * "join()" function
13493 */
13494 static void
13495f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013496 typval_T *argvars;
13497 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013498{
13499 garray_T ga;
13500 char_u *sep;
13501
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013502 if (argvars[0].v_type != VAR_LIST)
13503 {
13504 EMSG(_(e_listreq));
13505 return;
13506 }
13507 if (argvars[0].vval.v_list == NULL)
13508 return;
13509 if (argvars[1].v_type == VAR_UNKNOWN)
13510 sep = (char_u *)" ";
13511 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013512 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513
13514 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515
13516 if (sep != NULL)
13517 {
13518 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013519 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013520 ga_append(&ga, NUL);
13521 rettv->vval.v_string = (char_u *)ga.ga_data;
13522 }
13523 else
13524 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013525}
13526
13527/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013528 * "keys()" function
13529 */
13530 static void
13531f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013532 typval_T *argvars;
13533 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013534{
13535 dict_list(argvars, rettv, 0);
13536}
13537
13538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 * "last_buffer_nr()" function.
13540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013543 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545{
13546 int n = 0;
13547 buf_T *buf;
13548
13549 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13550 if (n < buf->b_fnum)
13551 n = buf->b_fnum;
13552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013554}
13555
13556/*
13557 * "len()" function
13558 */
13559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013563{
13564 switch (argvars[0].v_type)
13565 {
13566 case VAR_STRING:
13567 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568 rettv->vval.v_number = (varnumber_T)STRLEN(
13569 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 break;
13571 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013574 case VAR_DICT:
13575 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13576 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013577 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013578 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 break;
13580 }
13581}
13582
Bram Moolenaar33570922005-01-25 22:26:29 +000013583static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584
13585 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013587 typval_T *argvars;
13588 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 int type;
13590{
13591#ifdef FEAT_LIBCALL
13592 char_u *string_in;
13593 char_u **string_result;
13594 int nr_result;
13595#endif
13596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013598 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013600
13601 if (check_restricted() || check_secure())
13602 return;
13603
13604#ifdef FEAT_LIBCALL
13605 /* The first two args must be strings, otherwise its meaningless */
13606 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13607 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013608 string_in = NULL;
13609 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013610 string_in = argvars[2].vval.v_string;
13611 if (type == VAR_NUMBER)
13612 string_result = NULL;
13613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013615 if (mch_libcall(argvars[0].vval.v_string,
13616 argvars[1].vval.v_string,
13617 string_in,
13618 argvars[2].vval.v_number,
13619 string_result,
13620 &nr_result) == OK
13621 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 }
13624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
13627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013628 * "libcall()" function
13629 */
13630 static void
13631f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634{
13635 libcall_common(argvars, rettv, VAR_STRING);
13636}
13637
13638/*
13639 * "libcallnr()" function
13640 */
13641 static void
13642f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013643 typval_T *argvars;
13644 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013645{
13646 libcall_common(argvars, rettv, VAR_NUMBER);
13647}
13648
13649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650 * "line(string)" function
13651 */
13652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013654 typval_T *argvars;
13655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656{
13657 linenr_T lnum = 0;
13658 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013659 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013661 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 if (fp != NULL)
13663 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665}
13666
13667/*
13668 * "line2byte(lnum)" function
13669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674{
13675#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677#else
13678 linenr_T lnum;
13679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13685 if (rettv->vval.v_number >= 0)
13686 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687#endif
13688}
13689
13690/*
13691 * "lispindent(lnum)" function
13692 */
13693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013695 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697{
13698#ifdef FEAT_LISP
13699 pos_T pos;
13700 linenr_T lnum;
13701
13702 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13705 {
13706 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 curwin->w_cursor = pos;
13709 }
13710 else
13711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713}
13714
13715/*
13716 * "localtime()" function
13717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013719f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013720 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724}
13725
Bram Moolenaar33570922005-01-25 22:26:29 +000013726static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727
13728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013730 typval_T *argvars;
13731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 int exact;
13733{
13734 char_u *keys;
13735 char_u *which;
13736 char_u buf[NUMBUFLEN];
13737 char_u *keys_buf = NULL;
13738 char_u *rhs;
13739 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013740 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013741 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013742 mapblock_T *mp;
13743 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744
13745 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 rettv->v_type = VAR_STRING;
13747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013749 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 if (*keys == NUL)
13751 return;
13752
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013753 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013755 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013757 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013758 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013759 if (argvars[3].v_type != VAR_UNKNOWN)
13760 get_dict = get_tv_number(&argvars[3]);
13761 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 else
13764 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 if (which == NULL)
13766 return;
13767
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 mode = get_map_mode(&which, 0);
13769
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013770 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013771 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013773
13774 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013776 /* Return a string. */
13777 if (rhs != NULL)
13778 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779
Bram Moolenaarbd743252010-10-20 21:23:33 +020013780 }
13781 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13782 {
13783 /* Return a dictionary. */
13784 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13785 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13786 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787
Bram Moolenaarbd743252010-10-20 21:23:33 +020013788 dict_add_nr_str(dict, "lhs", 0L, lhs);
13789 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13790 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13791 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13792 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13793 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13794 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013795 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013796 dict_add_nr_str(dict, "mode", 0L, mapmode);
13797
13798 vim_free(lhs);
13799 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 }
13801}
13802
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013803#ifdef FEAT_FLOAT
13804/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013805 * "log()" function
13806 */
13807 static void
13808f_log(argvars, rettv)
13809 typval_T *argvars;
13810 typval_T *rettv;
13811{
13812 float_T f;
13813
13814 rettv->v_type = VAR_FLOAT;
13815 if (get_float_arg(argvars, &f) == OK)
13816 rettv->vval.v_float = log(f);
13817 else
13818 rettv->vval.v_float = 0.0;
13819}
13820
13821/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013822 * "log10()" function
13823 */
13824 static void
13825f_log10(argvars, rettv)
13826 typval_T *argvars;
13827 typval_T *rettv;
13828{
13829 float_T f;
13830
13831 rettv->v_type = VAR_FLOAT;
13832 if (get_float_arg(argvars, &f) == OK)
13833 rettv->vval.v_float = log10(f);
13834 else
13835 rettv->vval.v_float = 0.0;
13836}
13837#endif
13838
Bram Moolenaar1dced572012-04-05 16:54:08 +020013839#ifdef FEAT_LUA
13840/*
13841 * "luaeval()" function
13842 */
13843 static void
13844f_luaeval(argvars, rettv)
13845 typval_T *argvars;
13846 typval_T *rettv;
13847{
13848 char_u *str;
13849 char_u buf[NUMBUFLEN];
13850
13851 str = get_tv_string_buf(&argvars[0], buf);
13852 do_luaeval(str, argvars + 1, rettv);
13853}
13854#endif
13855
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013857 * "map()" function
13858 */
13859 static void
13860f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013861 typval_T *argvars;
13862 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013863{
13864 filter_map(argvars, rettv, TRUE);
13865}
13866
13867/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013868 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 */
13870 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T *argvars;
13873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013875 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876}
13877
13878/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 */
13881 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013882f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 typval_T *argvars;
13884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013886 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887}
13888
Bram Moolenaar33570922005-01-25 22:26:29 +000013889static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
13891 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013892find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *argvars;
13894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 int type;
13896{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013897 char_u *str = NULL;
13898 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899 char_u *pat;
13900 regmatch_T regmatch;
13901 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013902 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 char_u *save_cpo;
13904 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013905 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013906 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013907 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013908 list_T *l = NULL;
13909 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013911 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912
13913 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13914 save_cpo = p_cpo;
13915 p_cpo = (char_u *)"";
13916
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013917 rettv->vval.v_number = -1;
13918 if (type == 3)
13919 {
13920 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013921 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013922 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013923 }
13924 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926 rettv->v_type = VAR_STRING;
13927 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013930 if (argvars[0].v_type == VAR_LIST)
13931 {
13932 if ((l = argvars[0].vval.v_list) == NULL)
13933 goto theend;
13934 li = l->lv_first;
13935 }
13936 else
13937 expr = str = get_tv_string(&argvars[0]);
13938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13940 if (pat == NULL)
13941 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013942
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013943 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013945 int error = FALSE;
13946
13947 start = get_tv_number_chk(&argvars[2], &error);
13948 if (error)
13949 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013950 if (l != NULL)
13951 {
13952 li = list_find(l, start);
13953 if (li == NULL)
13954 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013955 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956 }
13957 else
13958 {
13959 if (start < 0)
13960 start = 0;
13961 if (start > (long)STRLEN(str))
13962 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013963 /* When "count" argument is there ignore matches before "start",
13964 * otherwise skip part of the string. Differs when pattern is "^"
13965 * or "\<". */
13966 if (argvars[3].v_type != VAR_UNKNOWN)
13967 startcol = start;
13968 else
13969 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013971
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013972 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 nth = get_tv_number_chk(&argvars[3], &error);
13974 if (error)
13975 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976 }
13977
13978 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13979 if (regmatch.regprog != NULL)
13980 {
13981 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013982
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013983 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013984 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013985 if (l != NULL)
13986 {
13987 if (li == NULL)
13988 {
13989 match = FALSE;
13990 break;
13991 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013992 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013993 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013994 if (str == NULL)
13995 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013996 }
13997
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013998 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013999
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014000 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014001 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014002 if (l == NULL && !match)
14003 break;
14004
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014005 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 if (l != NULL)
14007 {
14008 li = li->li_next;
14009 ++idx;
14010 }
14011 else
14012 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014013#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014014 startcol = (colnr_T)(regmatch.startp[0]
14015 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014016#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014017 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014018#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014019 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014020 }
14021
14022 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014024 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014025 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014026 int i;
14027
14028 /* return list with matched string and submatches */
14029 for (i = 0; i < NSUBEXP; ++i)
14030 {
14031 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014032 {
14033 if (list_append_string(rettv->vval.v_list,
14034 (char_u *)"", 0) == FAIL)
14035 break;
14036 }
14037 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014038 regmatch.startp[i],
14039 (int)(regmatch.endp[i] - regmatch.startp[i]))
14040 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014041 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014042 }
14043 }
14044 else if (type == 2)
14045 {
14046 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014047 if (l != NULL)
14048 copy_tv(&li->li_tv, rettv);
14049 else
14050 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014052 }
14053 else if (l != NULL)
14054 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 else
14056 {
14057 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 (varnumber_T)(regmatch.startp[0] - str);
14060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014063 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 }
14065 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014066 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 }
14068
14069theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014070 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 p_cpo = save_cpo;
14072}
14073
14074/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014075 * "match()" function
14076 */
14077 static void
14078f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014079 typval_T *argvars;
14080 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014081{
14082 find_some_match(argvars, rettv, 1);
14083}
14084
14085/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014086 * "matchadd()" function
14087 */
14088 static void
14089f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014090 typval_T *argvars UNUSED;
14091 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014092{
14093#ifdef FEAT_SEARCH_EXTRA
14094 char_u buf[NUMBUFLEN];
14095 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14096 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14097 int prio = 10; /* default priority */
14098 int id = -1;
14099 int error = FALSE;
14100
14101 rettv->vval.v_number = -1;
14102
14103 if (grp == NULL || pat == NULL)
14104 return;
14105 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014106 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014107 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014108 if (argvars[3].v_type != VAR_UNKNOWN)
14109 id = get_tv_number_chk(&argvars[3], &error);
14110 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014111 if (error == TRUE)
14112 return;
14113 if (id >= 1 && id <= 3)
14114 {
14115 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14116 return;
14117 }
14118
14119 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14120#endif
14121}
14122
14123/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014124 * "matcharg()" function
14125 */
14126 static void
14127f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014128 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014129 typval_T *rettv;
14130{
14131 if (rettv_list_alloc(rettv) == OK)
14132 {
14133#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014134 int id = get_tv_number(&argvars[0]);
14135 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014136
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014137 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014138 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014139 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14140 {
14141 list_append_string(rettv->vval.v_list,
14142 syn_id2name(m->hlg_id), -1);
14143 list_append_string(rettv->vval.v_list, m->pattern, -1);
14144 }
14145 else
14146 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014147 list_append_string(rettv->vval.v_list, NULL, -1);
14148 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014149 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014150 }
14151#endif
14152 }
14153}
14154
14155/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014156 * "matchdelete()" function
14157 */
14158 static void
14159f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014160 typval_T *argvars UNUSED;
14161 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014162{
14163#ifdef FEAT_SEARCH_EXTRA
14164 rettv->vval.v_number = match_delete(curwin,
14165 (int)get_tv_number(&argvars[0]), TRUE);
14166#endif
14167}
14168
14169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170 * "matchend()" function
14171 */
14172 static void
14173f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 typval_T *argvars;
14175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176{
14177 find_some_match(argvars, rettv, 0);
14178}
14179
14180/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014181 * "matchlist()" function
14182 */
14183 static void
14184f_matchlist(argvars, rettv)
14185 typval_T *argvars;
14186 typval_T *rettv;
14187{
14188 find_some_match(argvars, rettv, 3);
14189}
14190
14191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014192 * "matchstr()" function
14193 */
14194 static void
14195f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014196 typval_T *argvars;
14197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014198{
14199 find_some_match(argvars, rettv, 2);
14200}
14201
Bram Moolenaar33570922005-01-25 22:26:29 +000014202static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014203
14204 static void
14205max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014208 int domax;
14209{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014210 long n = 0;
14211 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014213
14214 if (argvars[0].v_type == VAR_LIST)
14215 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 list_T *l;
14217 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014218
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014219 l = argvars[0].vval.v_list;
14220 if (l != NULL)
14221 {
14222 li = l->lv_first;
14223 if (li != NULL)
14224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014226 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014227 {
14228 li = li->li_next;
14229 if (li == NULL)
14230 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014232 if (domax ? i > n : i < n)
14233 n = i;
14234 }
14235 }
14236 }
14237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014238 else if (argvars[0].v_type == VAR_DICT)
14239 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014240 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014241 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014242 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014243 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014244
14245 d = argvars[0].vval.v_dict;
14246 if (d != NULL)
14247 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014248 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014251 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014253 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014255 if (first)
14256 {
14257 n = i;
14258 first = FALSE;
14259 }
14260 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261 n = i;
14262 }
14263 }
14264 }
14265 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014266 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014267 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014269}
14270
14271/*
14272 * "max()" function
14273 */
14274 static void
14275f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014276 typval_T *argvars;
14277 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014278{
14279 max_min(argvars, rettv, TRUE);
14280}
14281
14282/*
14283 * "min()" function
14284 */
14285 static void
14286f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014289{
14290 max_min(argvars, rettv, FALSE);
14291}
14292
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014293static int mkdir_recurse __ARGS((char_u *dir, int prot));
14294
14295/*
14296 * Create the directory in which "dir" is located, and higher levels when
14297 * needed.
14298 */
14299 static int
14300mkdir_recurse(dir, prot)
14301 char_u *dir;
14302 int prot;
14303{
14304 char_u *p;
14305 char_u *updir;
14306 int r = FAIL;
14307
14308 /* Get end of directory name in "dir".
14309 * We're done when it's "/" or "c:/". */
14310 p = gettail_sep(dir);
14311 if (p <= get_past_head(dir))
14312 return OK;
14313
14314 /* If the directory exists we're done. Otherwise: create it.*/
14315 updir = vim_strnsave(dir, (int)(p - dir));
14316 if (updir == NULL)
14317 return FAIL;
14318 if (mch_isdir(updir))
14319 r = OK;
14320 else if (mkdir_recurse(updir, prot) == OK)
14321 r = vim_mkdir_emsg(updir, prot);
14322 vim_free(updir);
14323 return r;
14324}
14325
14326#ifdef vim_mkdir
14327/*
14328 * "mkdir()" function
14329 */
14330 static void
14331f_mkdir(argvars, rettv)
14332 typval_T *argvars;
14333 typval_T *rettv;
14334{
14335 char_u *dir;
14336 char_u buf[NUMBUFLEN];
14337 int prot = 0755;
14338
14339 rettv->vval.v_number = FAIL;
14340 if (check_restricted() || check_secure())
14341 return;
14342
14343 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014344 if (*dir == NUL)
14345 rettv->vval.v_number = FAIL;
14346 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014347 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014348 if (*gettail(dir) == NUL)
14349 /* remove trailing slashes */
14350 *gettail_sep(dir) = NUL;
14351
14352 if (argvars[1].v_type != VAR_UNKNOWN)
14353 {
14354 if (argvars[2].v_type != VAR_UNKNOWN)
14355 prot = get_tv_number_chk(&argvars[2], NULL);
14356 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14357 mkdir_recurse(dir, prot);
14358 }
14359 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014360 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014361}
14362#endif
14363
Bram Moolenaar0d660222005-01-07 21:51:51 +000014364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 * "mode()" function
14366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014369 typval_T *argvars;
14370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014372 char_u buf[3];
14373
14374 buf[1] = NUL;
14375 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376
14377#ifdef FEAT_VISUAL
14378 if (VIsual_active)
14379 {
14380 if (VIsual_select)
14381 buf[0] = VIsual_mode + 's' - 'v';
14382 else
14383 buf[0] = VIsual_mode;
14384 }
14385 else
14386#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014387 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14388 || State == CONFIRM)
14389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014391 if (State == ASKMORE)
14392 buf[1] = 'm';
14393 else if (State == CONFIRM)
14394 buf[1] = '?';
14395 }
14396 else if (State == EXTERNCMD)
14397 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 else if (State & INSERT)
14399 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014400#ifdef FEAT_VREPLACE
14401 if (State & VREPLACE_FLAG)
14402 {
14403 buf[0] = 'R';
14404 buf[1] = 'v';
14405 }
14406 else
14407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 if (State & REPLACE_FLAG)
14409 buf[0] = 'R';
14410 else
14411 buf[0] = 'i';
14412 }
14413 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014416 if (exmode_active)
14417 buf[1] = 'v';
14418 }
14419 else if (exmode_active)
14420 {
14421 buf[0] = 'c';
14422 buf[1] = 'e';
14423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014427 if (finish_op)
14428 buf[1] = 'o';
14429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014431 /* Clear out the minor mode when the argument is not a non-zero number or
14432 * non-empty string. */
14433 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014434 buf[1] = NUL;
14435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->vval.v_string = vim_strsave(buf);
14437 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438}
14439
Bram Moolenaar429fa852013-04-15 12:27:36 +020014440#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014441/*
14442 * "mzeval()" function
14443 */
14444 static void
14445f_mzeval(argvars, rettv)
14446 typval_T *argvars;
14447 typval_T *rettv;
14448{
14449 char_u *str;
14450 char_u buf[NUMBUFLEN];
14451
14452 str = get_tv_string_buf(&argvars[0], buf);
14453 do_mzeval(str, rettv);
14454}
Bram Moolenaar75676462013-01-30 14:55:42 +010014455
14456 void
14457mzscheme_call_vim(name, args, rettv)
14458 char_u *name;
14459 typval_T *args;
14460 typval_T *rettv;
14461{
14462 typval_T argvars[3];
14463
14464 argvars[0].v_type = VAR_STRING;
14465 argvars[0].vval.v_string = name;
14466 copy_tv(args, &argvars[1]);
14467 argvars[2].v_type = VAR_UNKNOWN;
14468 f_call(argvars, rettv);
14469 clear_tv(&argvars[1]);
14470}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014471#endif
14472
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014474 * "nextnonblank()" function
14475 */
14476 static void
14477f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014478 typval_T *argvars;
14479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480{
14481 linenr_T lnum;
14482
14483 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014485 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014486 {
14487 lnum = 0;
14488 break;
14489 }
14490 if (*skipwhite(ml_get(lnum)) != NUL)
14491 break;
14492 }
14493 rettv->vval.v_number = lnum;
14494}
14495
14496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 * "nr2char()" function
14498 */
14499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 typval_T *argvars;
14502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 char_u buf[NUMBUFLEN];
14505
14506#ifdef FEAT_MBYTE
14507 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014508 {
14509 int utf8 = 0;
14510
14511 if (argvars[1].v_type != VAR_UNKNOWN)
14512 utf8 = get_tv_number_chk(&argvars[1], NULL);
14513 if (utf8)
14514 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14515 else
14516 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 else
14519#endif
14520 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 buf[1] = NUL;
14523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->v_type = VAR_STRING;
14525 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014526}
14527
14528/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014529 * "or(expr, expr)" function
14530 */
14531 static void
14532f_or(argvars, rettv)
14533 typval_T *argvars;
14534 typval_T *rettv;
14535{
14536 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14537 | get_tv_number_chk(&argvars[1], NULL);
14538}
14539
14540/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014541 * "pathshorten()" function
14542 */
14543 static void
14544f_pathshorten(argvars, rettv)
14545 typval_T *argvars;
14546 typval_T *rettv;
14547{
14548 char_u *p;
14549
14550 rettv->v_type = VAR_STRING;
14551 p = get_tv_string_chk(&argvars[0]);
14552 if (p == NULL)
14553 rettv->vval.v_string = NULL;
14554 else
14555 {
14556 p = vim_strsave(p);
14557 rettv->vval.v_string = p;
14558 if (p != NULL)
14559 shorten_dir(p);
14560 }
14561}
14562
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014563#ifdef FEAT_FLOAT
14564/*
14565 * "pow()" function
14566 */
14567 static void
14568f_pow(argvars, rettv)
14569 typval_T *argvars;
14570 typval_T *rettv;
14571{
14572 float_T fx, fy;
14573
14574 rettv->v_type = VAR_FLOAT;
14575 if (get_float_arg(argvars, &fx) == OK
14576 && get_float_arg(&argvars[1], &fy) == OK)
14577 rettv->vval.v_float = pow(fx, fy);
14578 else
14579 rettv->vval.v_float = 0.0;
14580}
14581#endif
14582
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014583/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584 * "prevnonblank()" function
14585 */
14586 static void
14587f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T *argvars;
14589 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590{
14591 linenr_T lnum;
14592
14593 lnum = get_tv_lnum(argvars);
14594 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14595 lnum = 0;
14596 else
14597 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14598 --lnum;
14599 rettv->vval.v_number = lnum;
14600}
14601
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014602#ifdef HAVE_STDARG_H
14603/* This dummy va_list is here because:
14604 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14605 * - locally in the function results in a "used before set" warning
14606 * - using va_start() to initialize it gives "function with fixed args" error */
14607static va_list ap;
14608#endif
14609
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014611 * "printf()" function
14612 */
14613 static void
14614f_printf(argvars, rettv)
14615 typval_T *argvars;
14616 typval_T *rettv;
14617{
14618 rettv->v_type = VAR_STRING;
14619 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014620#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014621 {
14622 char_u buf[NUMBUFLEN];
14623 int len;
14624 char_u *s;
14625 int saved_did_emsg = did_emsg;
14626 char *fmt;
14627
14628 /* Get the required length, allocate the buffer and do it for real. */
14629 did_emsg = FALSE;
14630 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014631 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014632 if (!did_emsg)
14633 {
14634 s = alloc(len + 1);
14635 if (s != NULL)
14636 {
14637 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014638 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014639 }
14640 }
14641 did_emsg |= saved_did_emsg;
14642 }
14643#endif
14644}
14645
14646/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014647 * "pumvisible()" function
14648 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014649 static void
14650f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014651 typval_T *argvars UNUSED;
14652 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014653{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014654#ifdef FEAT_INS_EXPAND
14655 if (pum_visible())
14656 rettv->vval.v_number = 1;
14657#endif
14658}
14659
Bram Moolenaardb913952012-06-29 12:54:53 +020014660#ifdef FEAT_PYTHON3
14661/*
14662 * "py3eval()" function
14663 */
14664 static void
14665f_py3eval(argvars, rettv)
14666 typval_T *argvars;
14667 typval_T *rettv;
14668{
14669 char_u *str;
14670 char_u buf[NUMBUFLEN];
14671
14672 str = get_tv_string_buf(&argvars[0], buf);
14673 do_py3eval(str, rettv);
14674}
14675#endif
14676
14677#ifdef FEAT_PYTHON
14678/*
14679 * "pyeval()" function
14680 */
14681 static void
14682f_pyeval(argvars, rettv)
14683 typval_T *argvars;
14684 typval_T *rettv;
14685{
14686 char_u *str;
14687 char_u buf[NUMBUFLEN];
14688
14689 str = get_tv_string_buf(&argvars[0], buf);
14690 do_pyeval(str, rettv);
14691}
14692#endif
14693
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014694/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014695 * "range()" function
14696 */
14697 static void
14698f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014699 typval_T *argvars;
14700 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014701{
14702 long start;
14703 long end;
14704 long stride = 1;
14705 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014709 if (argvars[1].v_type == VAR_UNKNOWN)
14710 {
14711 end = start - 1;
14712 start = 0;
14713 }
14714 else
14715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014719 }
14720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 if (error)
14722 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014723 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014724 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014725 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014726 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014727 else
14728 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014729 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014730 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014731 if (list_append_number(rettv->vval.v_list,
14732 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014733 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734 }
14735}
14736
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014737/*
14738 * "readfile()" function
14739 */
14740 static void
14741f_readfile(argvars, rettv)
14742 typval_T *argvars;
14743 typval_T *rettv;
14744{
14745 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747 char_u *fname;
14748 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014749 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14750 int io_size = sizeof(buf);
14751 int readlen; /* size of last fread() */
14752 char_u *prev = NULL; /* previously read bytes, if any */
14753 long prevlen = 0; /* length of data in prev */
14754 long prevsize = 0; /* size of prev buffer */
14755 long maxline = MAXLNUM;
14756 long cnt = 0;
14757 char_u *p; /* position in buf */
14758 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014760 if (argvars[1].v_type != VAR_UNKNOWN)
14761 {
14762 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14763 binary = TRUE;
14764 if (argvars[2].v_type != VAR_UNKNOWN)
14765 maxline = get_tv_number(&argvars[2]);
14766 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014768 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770
14771 /* Always open the file in binary mode, library functions have a mind of
14772 * their own about CR-LF conversion. */
14773 fname = get_tv_string(&argvars[0]);
14774 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14775 {
14776 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14777 return;
14778 }
14779
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014780 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014783
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014784 /* This for loop processes what was read, but is also entered at end
14785 * of file so that either:
14786 * - an incomplete line gets written
14787 * - a "binary" file gets an empty line at the end if it ends in a
14788 * newline. */
14789 for (p = buf, start = buf;
14790 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14791 ++p)
14792 {
14793 if (*p == '\n' || readlen <= 0)
14794 {
14795 listitem_T *li;
14796 char_u *s = NULL;
14797 long_u len = p - start;
14798
14799 /* Finished a line. Remove CRs before NL. */
14800 if (readlen > 0 && !binary)
14801 {
14802 while (len > 0 && start[len - 1] == '\r')
14803 --len;
14804 /* removal may cross back to the "prev" string */
14805 if (len == 0)
14806 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14807 --prevlen;
14808 }
14809 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014810 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014811 else
14812 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014813 /* Change "prev" buffer to be the right size. This way
14814 * the bytes are only copied once, and very long lines are
14815 * allocated only once. */
14816 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014817 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014818 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014819 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014820 prev = NULL; /* the list will own the string */
14821 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014822 }
14823 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014824 if (s == NULL)
14825 {
14826 do_outofmem_msg((long_u) prevlen + len + 1);
14827 failed = TRUE;
14828 break;
14829 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014830
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014831 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 {
14833 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014834 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 break;
14836 }
14837 li->li_tv.v_type = VAR_STRING;
14838 li->li_tv.v_lock = 0;
14839 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014840 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014842 start = p + 1; /* step over newline */
14843 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014844 break;
14845 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014846 else if (*p == NUL)
14847 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014848#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014849 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14850 * when finding the BF and check the previous two bytes. */
14851 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014852 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014853 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14854 * + 1, these may be in the "prev" string. */
14855 char_u back1 = p >= buf + 1 ? p[-1]
14856 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14857 char_u back2 = p >= buf + 2 ? p[-2]
14858 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14859 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014861 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014863 char_u *dest = p - 2;
14864
14865 /* Usually a BOM is at the beginning of a file, and so at
14866 * the beginning of a line; then we can just step over it.
14867 */
14868 if (start == dest)
14869 start = p + 1;
14870 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014871 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014872 /* have to shuffle buf to close gap */
14873 int adjust_prevlen = 0;
14874
14875 if (dest < buf)
14876 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014877 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014878 dest = buf;
14879 }
14880 if (readlen > p - buf + 1)
14881 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14882 readlen -= 3 - adjust_prevlen;
14883 prevlen -= adjust_prevlen;
14884 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014885 }
14886 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014887 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014888#endif
14889 } /* for */
14890
14891 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14892 break;
14893 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014894 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014895 /* There's part of a line in buf, store it in "prev". */
14896 if (p - start + prevlen >= prevsize)
14897 {
14898 /* need bigger "prev" buffer */
14899 char_u *newprev;
14900
14901 /* A common use case is ordinary text files and "prev" gets a
14902 * fragment of a line, so the first allocation is made
14903 * small, to avoid repeatedly 'allocing' large and
14904 * 'reallocing' small. */
14905 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014906 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014907 else
14908 {
14909 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014910 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 prevsize = grow50pc > growmin ? grow50pc : growmin;
14912 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014913 newprev = prev == NULL ? alloc(prevsize)
14914 : vim_realloc(prev, prevsize);
14915 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014916 {
14917 do_outofmem_msg((long_u)prevsize);
14918 failed = TRUE;
14919 break;
14920 }
14921 prev = newprev;
14922 }
14923 /* Add the line part to end of "prev". */
14924 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014925 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014926 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014928
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014929 /*
14930 * For a negative line count use only the lines at the end of the file,
14931 * free the rest.
14932 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014933 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014934 while (cnt > -maxline)
14935 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014936 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014937 --cnt;
14938 }
14939
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014940 if (failed)
14941 {
14942 list_free(rettv->vval.v_list, TRUE);
14943 /* readfile doc says an empty list is returned on error */
14944 rettv->vval.v_list = list_alloc();
14945 }
14946
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014947 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014948 fclose(fd);
14949}
14950
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014951#if defined(FEAT_RELTIME)
14952static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14953
14954/*
14955 * Convert a List to proftime_T.
14956 * Return FAIL when there is something wrong.
14957 */
14958 static int
14959list2proftime(arg, tm)
14960 typval_T *arg;
14961 proftime_T *tm;
14962{
14963 long n1, n2;
14964 int error = FALSE;
14965
14966 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14967 || arg->vval.v_list->lv_len != 2)
14968 return FAIL;
14969 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14970 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14971# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014972 tm->HighPart = n1;
14973 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014974# else
14975 tm->tv_sec = n1;
14976 tm->tv_usec = n2;
14977# endif
14978 return error ? FAIL : OK;
14979}
14980#endif /* FEAT_RELTIME */
14981
14982/*
14983 * "reltime()" function
14984 */
14985 static void
14986f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014987 typval_T *argvars UNUSED;
14988 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014989{
14990#ifdef FEAT_RELTIME
14991 proftime_T res;
14992 proftime_T start;
14993
14994 if (argvars[0].v_type == VAR_UNKNOWN)
14995 {
14996 /* No arguments: get current time. */
14997 profile_start(&res);
14998 }
14999 else if (argvars[1].v_type == VAR_UNKNOWN)
15000 {
15001 if (list2proftime(&argvars[0], &res) == FAIL)
15002 return;
15003 profile_end(&res);
15004 }
15005 else
15006 {
15007 /* Two arguments: compute the difference. */
15008 if (list2proftime(&argvars[0], &start) == FAIL
15009 || list2proftime(&argvars[1], &res) == FAIL)
15010 return;
15011 profile_sub(&res, &start);
15012 }
15013
15014 if (rettv_list_alloc(rettv) == OK)
15015 {
15016 long n1, n2;
15017
15018# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015019 n1 = res.HighPart;
15020 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015021# else
15022 n1 = res.tv_sec;
15023 n2 = res.tv_usec;
15024# endif
15025 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15026 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15027 }
15028#endif
15029}
15030
15031/*
15032 * "reltimestr()" function
15033 */
15034 static void
15035f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015036 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015037 typval_T *rettv;
15038{
15039#ifdef FEAT_RELTIME
15040 proftime_T tm;
15041#endif
15042
15043 rettv->v_type = VAR_STRING;
15044 rettv->vval.v_string = NULL;
15045#ifdef FEAT_RELTIME
15046 if (list2proftime(&argvars[0], &tm) == OK)
15047 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15048#endif
15049}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015050
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15052static void make_connection __ARGS((void));
15053static int check_connection __ARGS((void));
15054
15055 static void
15056make_connection()
15057{
15058 if (X_DISPLAY == NULL
15059# ifdef FEAT_GUI
15060 && !gui.in_use
15061# endif
15062 )
15063 {
15064 x_force_connect = TRUE;
15065 setup_term_clip();
15066 x_force_connect = FALSE;
15067 }
15068}
15069
15070 static int
15071check_connection()
15072{
15073 make_connection();
15074 if (X_DISPLAY == NULL)
15075 {
15076 EMSG(_("E240: No connection to Vim server"));
15077 return FAIL;
15078 }
15079 return OK;
15080}
15081#endif
15082
15083#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015084static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085
15086 static void
15087remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 typval_T *argvars;
15089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 int expr;
15091{
15092 char_u *server_name;
15093 char_u *keys;
15094 char_u *r = NULL;
15095 char_u buf[NUMBUFLEN];
15096# ifdef WIN32
15097 HWND w;
15098# else
15099 Window w;
15100# endif
15101
15102 if (check_restricted() || check_secure())
15103 return;
15104
15105# ifdef FEAT_X11
15106 if (check_connection() == FAIL)
15107 return;
15108# endif
15109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015110 server_name = get_tv_string_chk(&argvars[0]);
15111 if (server_name == NULL)
15112 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 keys = get_tv_string_buf(&argvars[1], buf);
15114# ifdef WIN32
15115 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15116# else
15117 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15118 < 0)
15119# endif
15120 {
15121 if (r != NULL)
15122 EMSG(r); /* sending worked but evaluation failed */
15123 else
15124 EMSG2(_("E241: Unable to send to %s"), server_name);
15125 return;
15126 }
15127
15128 rettv->vval.v_string = r;
15129
15130 if (argvars[2].v_type != VAR_UNKNOWN)
15131 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015133 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015136 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 v.di_tv.v_type = VAR_STRING;
15138 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 idvar = get_tv_string_chk(&argvars[2]);
15140 if (idvar != NULL)
15141 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143 }
15144}
15145#endif
15146
15147/*
15148 * "remote_expr()" function
15149 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 static void
15151f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154{
15155 rettv->v_type = VAR_STRING;
15156 rettv->vval.v_string = NULL;
15157#ifdef FEAT_CLIENTSERVER
15158 remote_common(argvars, rettv, TRUE);
15159#endif
15160}
15161
15162/*
15163 * "remote_foreground()" function
15164 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165 static void
15166f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015167 typval_T *argvars UNUSED;
15168 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015170#ifdef FEAT_CLIENTSERVER
15171# ifdef WIN32
15172 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 {
15174 char_u *server_name = get_tv_string_chk(&argvars[0]);
15175
15176 if (server_name != NULL)
15177 serverForeground(server_name);
15178 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179# else
15180 /* Send a foreground() expression to the server. */
15181 argvars[1].v_type = VAR_STRING;
15182 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15183 argvars[2].v_type = VAR_UNKNOWN;
15184 remote_common(argvars, rettv, TRUE);
15185 vim_free(argvars[1].vval.v_string);
15186# endif
15187#endif
15188}
15189
Bram Moolenaar0d660222005-01-07 21:51:51 +000015190 static void
15191f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015192 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194{
15195#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015196 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197 char_u *s = NULL;
15198# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015199 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015201 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015202
15203 if (check_restricted() || check_secure())
15204 {
15205 rettv->vval.v_number = -1;
15206 return;
15207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015208 serverid = get_tv_string_chk(&argvars[0]);
15209 if (serverid == NULL)
15210 {
15211 rettv->vval.v_number = -1;
15212 return; /* type error; errmsg already given */
15213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015215 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015216 if (n == 0)
15217 rettv->vval.v_number = -1;
15218 else
15219 {
15220 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15221 rettv->vval.v_number = (s != NULL);
15222 }
15223# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 if (check_connection() == FAIL)
15225 return;
15226
15227 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229# endif
15230
15231 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 char_u *retvar;
15234
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 v.di_tv.v_type = VAR_STRING;
15236 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 retvar = get_tv_string_chk(&argvars[1]);
15238 if (retvar != NULL)
15239 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241 }
15242#else
15243 rettv->vval.v_number = -1;
15244#endif
15245}
15246
Bram Moolenaar0d660222005-01-07 21:51:51 +000015247 static void
15248f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015250 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251{
15252 char_u *r = NULL;
15253
15254#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 char_u *serverid = get_tv_string_chk(&argvars[0]);
15256
15257 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258 {
15259# ifdef WIN32
15260 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015261 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015262
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015263 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015264 if (n != 0)
15265 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15266 if (r == NULL)
15267# else
15268 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015270# endif
15271 EMSG(_("E277: Unable to read a server reply"));
15272 }
15273#endif
15274 rettv->v_type = VAR_STRING;
15275 rettv->vval.v_string = r;
15276}
15277
15278/*
15279 * "remote_send()" function
15280 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015281 static void
15282f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015283 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015285{
15286 rettv->v_type = VAR_STRING;
15287 rettv->vval.v_string = NULL;
15288#ifdef FEAT_CLIENTSERVER
15289 remote_common(argvars, rettv, FALSE);
15290#endif
15291}
15292
15293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015294 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015295 */
15296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015297f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015298 typval_T *argvars;
15299 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300{
Bram Moolenaar33570922005-01-25 22:26:29 +000015301 list_T *l;
15302 listitem_T *item, *item2;
15303 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015305 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 dict_T *d;
15308 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015309 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015310
Bram Moolenaar8c711452005-01-14 21:53:12 +000015311 if (argvars[0].v_type == VAR_DICT)
15312 {
15313 if (argvars[2].v_type != VAR_UNKNOWN)
15314 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015315 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015316 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 key = get_tv_string_chk(&argvars[1]);
15319 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 di = dict_find(d, key, -1);
15322 if (di == NULL)
15323 EMSG2(_(e_dictkey), key);
15324 else
15325 {
15326 *rettv = di->di_tv;
15327 init_tv(&di->di_tv);
15328 dictitem_remove(d, di);
15329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015331 }
15332 }
15333 else if (argvars[0].v_type != VAR_LIST)
15334 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015335 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015336 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015338 int error = FALSE;
15339
15340 idx = get_tv_number_chk(&argvars[1], &error);
15341 if (error)
15342 ; /* type error: do nothing, errmsg already given */
15343 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015344 EMSGN(_(e_listidx), idx);
15345 else
15346 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015347 if (argvars[2].v_type == VAR_UNKNOWN)
15348 {
15349 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015350 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015351 *rettv = item->li_tv;
15352 vim_free(item);
15353 }
15354 else
15355 {
15356 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 end = get_tv_number_chk(&argvars[2], &error);
15358 if (error)
15359 ; /* type error: do nothing */
15360 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015361 EMSGN(_(e_listidx), end);
15362 else
15363 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015364 int cnt = 0;
15365
15366 for (li = item; li != NULL; li = li->li_next)
15367 {
15368 ++cnt;
15369 if (li == item2)
15370 break;
15371 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015372 if (li == NULL) /* didn't find "item2" after "item" */
15373 EMSG(_(e_invrange));
15374 else
15375 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015376 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015377 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015378 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015379 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015380 l->lv_first = item;
15381 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015382 item->li_prev = NULL;
15383 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015384 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015385 }
15386 }
15387 }
15388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015389 }
15390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392
15393/*
15394 * "rename({from}, {to})" function
15395 */
15396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015398 typval_T *argvars;
15399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
15401 char_u buf[NUMBUFLEN];
15402
15403 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15407 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408}
15409
15410/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015411 * "repeat()" function
15412 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015413 static void
15414f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015415 typval_T *argvars;
15416 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015417{
15418 char_u *p;
15419 int n;
15420 int slen;
15421 int len;
15422 char_u *r;
15423 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015424
15425 n = get_tv_number(&argvars[1]);
15426 if (argvars[0].v_type == VAR_LIST)
15427 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015428 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015429 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015430 if (list_extend(rettv->vval.v_list,
15431 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015433 }
15434 else
15435 {
15436 p = get_tv_string(&argvars[0]);
15437 rettv->v_type = VAR_STRING;
15438 rettv->vval.v_string = NULL;
15439
15440 slen = (int)STRLEN(p);
15441 len = slen * n;
15442 if (len <= 0)
15443 return;
15444
15445 r = alloc(len + 1);
15446 if (r != NULL)
15447 {
15448 for (i = 0; i < n; i++)
15449 mch_memmove(r + i * slen, p, (size_t)slen);
15450 r[len] = NUL;
15451 }
15452
15453 rettv->vval.v_string = r;
15454 }
15455}
15456
15457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 * "resolve()" function
15459 */
15460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015462 typval_T *argvars;
15463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464{
15465 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015466#ifdef HAVE_READLINK
15467 char_u *buf = NULL;
15468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471#ifdef FEAT_SHORTCUT
15472 {
15473 char_u *v = NULL;
15474
15475 v = mch_resolve_shortcut(p);
15476 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015477 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 }
15481#else
15482# ifdef HAVE_READLINK
15483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 char_u *cpy;
15485 int len;
15486 char_u *remain = NULL;
15487 char_u *q;
15488 int is_relative_to_current = FALSE;
15489 int has_trailing_pathsep = FALSE;
15490 int limit = 100;
15491
15492 p = vim_strsave(p);
15493
15494 if (p[0] == '.' && (vim_ispathsep(p[1])
15495 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15496 is_relative_to_current = TRUE;
15497
15498 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015499 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015502 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504
15505 q = getnextcomp(p);
15506 if (*q != NUL)
15507 {
15508 /* Separate the first path component in "p", and keep the
15509 * remainder (beginning with the path separator). */
15510 remain = vim_strsave(q - 1);
15511 q[-1] = NUL;
15512 }
15513
Bram Moolenaard9462e32011-04-11 21:35:11 +020015514 buf = alloc(MAXPATHL + 1);
15515 if (buf == NULL)
15516 goto fail;
15517
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 for (;;)
15519 {
15520 for (;;)
15521 {
15522 len = readlink((char *)p, (char *)buf, MAXPATHL);
15523 if (len <= 0)
15524 break;
15525 buf[len] = NUL;
15526
15527 if (limit-- == 0)
15528 {
15529 vim_free(p);
15530 vim_free(remain);
15531 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 goto fail;
15534 }
15535
15536 /* Ensure that the result will have a trailing path separator
15537 * if the argument has one. */
15538 if (remain == NULL && has_trailing_pathsep)
15539 add_pathsep(buf);
15540
15541 /* Separate the first path component in the link value and
15542 * concatenate the remainders. */
15543 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15544 if (*q != NUL)
15545 {
15546 if (remain == NULL)
15547 remain = vim_strsave(q - 1);
15548 else
15549 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015550 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 if (cpy != NULL)
15552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 vim_free(remain);
15554 remain = cpy;
15555 }
15556 }
15557 q[-1] = NUL;
15558 }
15559
15560 q = gettail(p);
15561 if (q > p && *q == NUL)
15562 {
15563 /* Ignore trailing path separator. */
15564 q[-1] = NUL;
15565 q = gettail(p);
15566 }
15567 if (q > p && !mch_isFullName(buf))
15568 {
15569 /* symlink is relative to directory of argument */
15570 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15571 if (cpy != NULL)
15572 {
15573 STRCPY(cpy, p);
15574 STRCPY(gettail(cpy), buf);
15575 vim_free(p);
15576 p = cpy;
15577 }
15578 }
15579 else
15580 {
15581 vim_free(p);
15582 p = vim_strsave(buf);
15583 }
15584 }
15585
15586 if (remain == NULL)
15587 break;
15588
15589 /* Append the first path component of "remain" to "p". */
15590 q = getnextcomp(remain + 1);
15591 len = q - remain - (*q != NUL);
15592 cpy = vim_strnsave(p, STRLEN(p) + len);
15593 if (cpy != NULL)
15594 {
15595 STRNCAT(cpy, remain, len);
15596 vim_free(p);
15597 p = cpy;
15598 }
15599 /* Shorten "remain". */
15600 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015601 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 else
15603 {
15604 vim_free(remain);
15605 remain = NULL;
15606 }
15607 }
15608
15609 /* If the result is a relative path name, make it explicitly relative to
15610 * the current directory if and only if the argument had this form. */
15611 if (!vim_ispathsep(*p))
15612 {
15613 if (is_relative_to_current
15614 && *p != NUL
15615 && !(p[0] == '.'
15616 && (p[1] == NUL
15617 || vim_ispathsep(p[1])
15618 || (p[1] == '.'
15619 && (p[2] == NUL
15620 || vim_ispathsep(p[2]))))))
15621 {
15622 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015623 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 if (cpy != NULL)
15625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 vim_free(p);
15627 p = cpy;
15628 }
15629 }
15630 else if (!is_relative_to_current)
15631 {
15632 /* Strip leading "./". */
15633 q = p;
15634 while (q[0] == '.' && vim_ispathsep(q[1]))
15635 q += 2;
15636 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015637 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 }
15639 }
15640
15641 /* Ensure that the result will have no trailing path separator
15642 * if the argument had none. But keep "/" or "//". */
15643 if (!has_trailing_pathsep)
15644 {
15645 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015646 if (after_pathsep(p, q))
15647 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015650 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 }
15652# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015653 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654# endif
15655#endif
15656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015657 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658
15659#ifdef HAVE_READLINK
15660fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015661 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664}
15665
15666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 */
15669 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 typval_T *argvars;
15672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673{
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 list_T *l;
15675 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 if (argvars[0].v_type != VAR_LIST)
15678 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015679 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015680 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681 {
15682 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015683 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015684 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015685 while (li != NULL)
15686 {
15687 ni = li->li_prev;
15688 list_append(l, li);
15689 li = ni;
15690 }
15691 rettv->vval.v_list = l;
15692 rettv->v_type = VAR_LIST;
15693 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015694 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696}
15697
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015698#define SP_NOMOVE 0x01 /* don't move cursor */
15699#define SP_REPEAT 0x02 /* repeat to find outer pair */
15700#define SP_RETCOUNT 0x04 /* return matchcount */
15701#define SP_SETPCMARK 0x08 /* set previous context mark */
15702#define SP_START 0x10 /* accept match at start position */
15703#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15704#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015705
Bram Moolenaar33570922005-01-25 22:26:29 +000015706static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015707
15708/*
15709 * Get flags for a search function.
15710 * Possibly sets "p_ws".
15711 * Returns BACKWARD, FORWARD or zero (for an error).
15712 */
15713 static int
15714get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015715 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 int *flagsp;
15717{
15718 int dir = FORWARD;
15719 char_u *flags;
15720 char_u nbuf[NUMBUFLEN];
15721 int mask;
15722
15723 if (varp->v_type != VAR_UNKNOWN)
15724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015725 flags = get_tv_string_buf_chk(varp, nbuf);
15726 if (flags == NULL)
15727 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015728 while (*flags != NUL)
15729 {
15730 switch (*flags)
15731 {
15732 case 'b': dir = BACKWARD; break;
15733 case 'w': p_ws = TRUE; break;
15734 case 'W': p_ws = FALSE; break;
15735 default: mask = 0;
15736 if (flagsp != NULL)
15737 switch (*flags)
15738 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015739 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015740 case 'e': mask = SP_END; break;
15741 case 'm': mask = SP_RETCOUNT; break;
15742 case 'n': mask = SP_NOMOVE; break;
15743 case 'p': mask = SP_SUBPAT; break;
15744 case 'r': mask = SP_REPEAT; break;
15745 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746 }
15747 if (mask == 0)
15748 {
15749 EMSG2(_(e_invarg2), flags);
15750 dir = 0;
15751 }
15752 else
15753 *flagsp |= mask;
15754 }
15755 if (dir == 0)
15756 break;
15757 ++flags;
15758 }
15759 }
15760 return dir;
15761}
15762
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015764 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015766 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015767search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015768 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015769 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015770 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015772 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 char_u *pat;
15774 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015775 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 int save_p_ws = p_ws;
15777 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015778 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015779 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015780 proftime_T tm;
15781#ifdef FEAT_RELTIME
15782 long time_limit = 0;
15783#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015784 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015785 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015788 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015789 if (dir == 0)
15790 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015791 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015792 if (flags & SP_START)
15793 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015794 if (flags & SP_END)
15795 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015796
Bram Moolenaar76929292008-01-06 19:07:36 +000015797 /* Optional arguments: line number to stop searching and timeout. */
15798 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015799 {
15800 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15801 if (lnum_stop < 0)
15802 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015803#ifdef FEAT_RELTIME
15804 if (argvars[3].v_type != VAR_UNKNOWN)
15805 {
15806 time_limit = get_tv_number_chk(&argvars[3], NULL);
15807 if (time_limit < 0)
15808 goto theend;
15809 }
15810#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015811 }
15812
Bram Moolenaar76929292008-01-06 19:07:36 +000015813#ifdef FEAT_RELTIME
15814 /* Set the time limit, if there is one. */
15815 profile_setlimit(time_limit, &tm);
15816#endif
15817
Bram Moolenaar231334e2005-07-25 20:46:57 +000015818 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015819 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015820 * Check to make sure only those flags are set.
15821 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15822 * flags cannot be set. Check for that condition also.
15823 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015824 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015825 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015828 goto theend;
15829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015831 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015832 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015833 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015834 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015836 if (flags & SP_SUBPAT)
15837 retval = subpatnum;
15838 else
15839 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015840 if (flags & SP_SETPCMARK)
15841 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015843 if (match_pos != NULL)
15844 {
15845 /* Store the match cursor position */
15846 match_pos->lnum = pos.lnum;
15847 match_pos->col = pos.col + 1;
15848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 /* "/$" will put the cursor after the end of the line, may need to
15850 * correct that here */
15851 check_cursor();
15852 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015853
15854 /* If 'n' flag is used: restore cursor position. */
15855 if (flags & SP_NOMOVE)
15856 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015857 else
15858 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015859theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015861
15862 return retval;
15863}
15864
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015865#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015866
15867/*
15868 * round() is not in C90, use ceil() or floor() instead.
15869 */
15870 float_T
15871vim_round(f)
15872 float_T f;
15873{
15874 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15875}
15876
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015877/*
15878 * "round({float})" function
15879 */
15880 static void
15881f_round(argvars, rettv)
15882 typval_T *argvars;
15883 typval_T *rettv;
15884{
15885 float_T f;
15886
15887 rettv->v_type = VAR_FLOAT;
15888 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015889 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015890 else
15891 rettv->vval.v_float = 0.0;
15892}
15893#endif
15894
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015895/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015896 * "screenattr()" function
15897 */
15898 static void
15899f_screenattr(argvars, rettv)
15900 typval_T *argvars UNUSED;
15901 typval_T *rettv;
15902{
15903 int row;
15904 int col;
15905 int c;
15906
15907 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15908 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15909 if (row < 0 || row >= screen_Rows
15910 || col < 0 || col >= screen_Columns)
15911 c = -1;
15912 else
15913 c = ScreenAttrs[LineOffset[row] + col];
15914 rettv->vval.v_number = c;
15915}
15916
15917/*
15918 * "screenchar()" function
15919 */
15920 static void
15921f_screenchar(argvars, rettv)
15922 typval_T *argvars UNUSED;
15923 typval_T *rettv;
15924{
15925 int row;
15926 int col;
15927 int off;
15928 int c;
15929
15930 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15931 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15932 if (row < 0 || row >= screen_Rows
15933 || col < 0 || col >= screen_Columns)
15934 c = -1;
15935 else
15936 {
15937 off = LineOffset[row] + col;
15938#ifdef FEAT_MBYTE
15939 if (enc_utf8 && ScreenLinesUC[off] != 0)
15940 c = ScreenLinesUC[off];
15941 else
15942#endif
15943 c = ScreenLines[off];
15944 }
15945 rettv->vval.v_number = c;
15946}
15947
15948/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015949 * "screencol()" function
15950 *
15951 * First column is 1 to be consistent with virtcol().
15952 */
15953 static void
15954f_screencol(argvars, rettv)
15955 typval_T *argvars UNUSED;
15956 typval_T *rettv;
15957{
15958 rettv->vval.v_number = screen_screencol() + 1;
15959}
15960
15961/*
15962 * "screenrow()" function
15963 */
15964 static void
15965f_screenrow(argvars, rettv)
15966 typval_T *argvars UNUSED;
15967 typval_T *rettv;
15968{
15969 rettv->vval.v_number = screen_screenrow() + 1;
15970}
15971
15972/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973 * "search()" function
15974 */
15975 static void
15976f_search(argvars, rettv)
15977 typval_T *argvars;
15978 typval_T *rettv;
15979{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015980 int flags = 0;
15981
15982 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983}
15984
Bram Moolenaar071d4272004-06-13 20:20:40 +000015985/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015986 * "searchdecl()" function
15987 */
15988 static void
15989f_searchdecl(argvars, rettv)
15990 typval_T *argvars;
15991 typval_T *rettv;
15992{
15993 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015994 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015995 int error = FALSE;
15996 char_u *name;
15997
15998 rettv->vval.v_number = 1; /* default: FAIL */
15999
16000 name = get_tv_string_chk(&argvars[0]);
16001 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016002 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016003 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016004 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16005 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16006 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016007 if (!error && name != NULL)
16008 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016009 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016010}
16011
16012/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016013 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016015 static int
16016searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016018 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019{
16020 char_u *spat, *mpat, *epat;
16021 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 int dir;
16024 int flags = 0;
16025 char_u nbuf1[NUMBUFLEN];
16026 char_u nbuf2[NUMBUFLEN];
16027 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016028 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016029 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016030 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 spat = get_tv_string_chk(&argvars[0]);
16034 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16035 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16036 if (spat == NULL || mpat == NULL || epat == NULL)
16037 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038
Bram Moolenaar071d4272004-06-13 20:20:40 +000016039 /* Handle the optional fourth argument: flags */
16040 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016041 if (dir == 0)
16042 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016043
16044 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016045 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16046 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016047 if ((flags & (SP_END | SP_SUBPAT)) != 0
16048 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016049 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016050 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016051 goto theend;
16052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016054 /* Using 'r' implies 'W', otherwise it doesn't work. */
16055 if (flags & SP_REPEAT)
16056 p_ws = FALSE;
16057
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016058 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016059 if (argvars[3].v_type == VAR_UNKNOWN
16060 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 skip = (char_u *)"";
16062 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016064 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016065 if (argvars[5].v_type != VAR_UNKNOWN)
16066 {
16067 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16068 if (lnum_stop < 0)
16069 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016070#ifdef FEAT_RELTIME
16071 if (argvars[6].v_type != VAR_UNKNOWN)
16072 {
16073 time_limit = get_tv_number_chk(&argvars[6], NULL);
16074 if (time_limit < 0)
16075 goto theend;
16076 }
16077#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016078 }
16079 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 if (skip == NULL)
16081 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016083 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016084 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016085
16086theend:
16087 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016088
16089 return retval;
16090}
16091
16092/*
16093 * "searchpair()" function
16094 */
16095 static void
16096f_searchpair(argvars, rettv)
16097 typval_T *argvars;
16098 typval_T *rettv;
16099{
16100 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16101}
16102
16103/*
16104 * "searchpairpos()" function
16105 */
16106 static void
16107f_searchpairpos(argvars, rettv)
16108 typval_T *argvars;
16109 typval_T *rettv;
16110{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016111 pos_T match_pos;
16112 int lnum = 0;
16113 int col = 0;
16114
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016115 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016116 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016117
16118 if (searchpair_cmn(argvars, &match_pos) > 0)
16119 {
16120 lnum = match_pos.lnum;
16121 col = match_pos.col;
16122 }
16123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016124 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16125 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016126}
16127
16128/*
16129 * Search for a start/middle/end thing.
16130 * Used by searchpair(), see its documentation for the details.
16131 * Returns 0 or -1 for no match,
16132 */
16133 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016134do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16135 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016136 char_u *spat; /* start pattern */
16137 char_u *mpat; /* middle pattern */
16138 char_u *epat; /* end pattern */
16139 int dir; /* BACKWARD or FORWARD */
16140 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016141 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016142 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016144 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016145{
16146 char_u *save_cpo;
16147 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16148 long retval = 0;
16149 pos_T pos;
16150 pos_T firstpos;
16151 pos_T foundpos;
16152 pos_T save_cursor;
16153 pos_T save_pos;
16154 int n;
16155 int r;
16156 int nest = 1;
16157 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016158 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016159 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016160
16161 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16162 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016163 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016164
Bram Moolenaar76929292008-01-06 19:07:36 +000016165#ifdef FEAT_RELTIME
16166 /* Set the time limit, if there is one. */
16167 profile_setlimit(time_limit, &tm);
16168#endif
16169
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016170 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16171 * start/middle/end (pat3, for the top pair). */
16172 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16173 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16174 if (pat2 == NULL || pat3 == NULL)
16175 goto theend;
16176 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16177 if (*mpat == NUL)
16178 STRCPY(pat3, pat2);
16179 else
16180 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16181 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016182 if (flags & SP_START)
16183 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016184
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 save_cursor = curwin->w_cursor;
16186 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016187 clearpos(&firstpos);
16188 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 pat = pat3;
16190 for (;;)
16191 {
16192 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016193 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16195 /* didn't find it or found the first match again: FAIL */
16196 break;
16197
16198 if (firstpos.lnum == 0)
16199 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016200 if (equalpos(pos, foundpos))
16201 {
16202 /* Found the same position again. Can happen with a pattern that
16203 * has "\zs" at the end and searching backwards. Advance one
16204 * character and try again. */
16205 if (dir == BACKWARD)
16206 decl(&pos);
16207 else
16208 incl(&pos);
16209 }
16210 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016212 /* clear the start flag to avoid getting stuck here */
16213 options &= ~SEARCH_START;
16214
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 /* If the skip pattern matches, ignore this match. */
16216 if (*skip != NUL)
16217 {
16218 save_pos = curwin->w_cursor;
16219 curwin->w_cursor = pos;
16220 r = eval_to_bool(skip, &err, NULL, FALSE);
16221 curwin->w_cursor = save_pos;
16222 if (err)
16223 {
16224 /* Evaluating {skip} caused an error, break here. */
16225 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016226 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 break;
16228 }
16229 if (r)
16230 continue;
16231 }
16232
16233 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16234 {
16235 /* Found end when searching backwards or start when searching
16236 * forward: nested pair. */
16237 ++nest;
16238 pat = pat2; /* nested, don't search for middle */
16239 }
16240 else
16241 {
16242 /* Found end when searching forward or start when searching
16243 * backward: end of (nested) pair; or found middle in outer pair. */
16244 if (--nest == 1)
16245 pat = pat3; /* outer level, search for middle */
16246 }
16247
16248 if (nest == 0)
16249 {
16250 /* Found the match: return matchcount or line number. */
16251 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016252 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016254 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016255 if (flags & SP_SETPCMARK)
16256 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 curwin->w_cursor = pos;
16258 if (!(flags & SP_REPEAT))
16259 break;
16260 nest = 1; /* search for next unmatched */
16261 }
16262 }
16263
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016264 if (match_pos != NULL)
16265 {
16266 /* Store the match cursor position */
16267 match_pos->lnum = curwin->w_cursor.lnum;
16268 match_pos->col = curwin->w_cursor.col + 1;
16269 }
16270
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016272 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 curwin->w_cursor = save_cursor;
16274
16275theend:
16276 vim_free(pat2);
16277 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016278 if (p_cpo == empty_option)
16279 p_cpo = save_cpo;
16280 else
16281 /* Darn, evaluating the {skip} expression changed the value. */
16282 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016283
16284 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285}
16286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016287/*
16288 * "searchpos()" function
16289 */
16290 static void
16291f_searchpos(argvars, rettv)
16292 typval_T *argvars;
16293 typval_T *rettv;
16294{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016295 pos_T match_pos;
16296 int lnum = 0;
16297 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016298 int n;
16299 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016302 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016303
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016304 n = search_cmn(argvars, &match_pos, &flags);
16305 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016306 {
16307 lnum = match_pos.lnum;
16308 col = match_pos.col;
16309 }
16310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016311 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16312 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016313 if (flags & SP_SUBPAT)
16314 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016315}
16316
16317
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318 static void
16319f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323#ifdef FEAT_CLIENTSERVER
16324 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016325 char_u *server = get_tv_string_chk(&argvars[0]);
16326 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016329 if (server == NULL || reply == NULL)
16330 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331 if (check_restricted() || check_secure())
16332 return;
16333# ifdef FEAT_X11
16334 if (check_connection() == FAIL)
16335 return;
16336# endif
16337
16338 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 EMSG(_("E258: Unable to send to client"));
16341 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 rettv->vval.v_number = 0;
16344#else
16345 rettv->vval.v_number = -1;
16346#endif
16347}
16348
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 static void
16350f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353{
16354 char_u *r = NULL;
16355
16356#ifdef FEAT_CLIENTSERVER
16357# ifdef WIN32
16358 r = serverGetVimNames();
16359# else
16360 make_connection();
16361 if (X_DISPLAY != NULL)
16362 r = serverGetVimNames(X_DISPLAY);
16363# endif
16364#endif
16365 rettv->v_type = VAR_STRING;
16366 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367}
16368
16369/*
16370 * "setbufvar()" function
16371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016373f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016374 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016375 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
16377 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 char_u nbuf[NUMBUFLEN];
16382
16383 if (check_restricted() || check_secure())
16384 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016385 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16386 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016387 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 varp = &argvars[2];
16389
16390 if (buf != NULL && varname != NULL && varp != NULL)
16391 {
16392 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394
16395 if (*varname == '&')
16396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 long numval;
16398 char_u *strval;
16399 int error = FALSE;
16400
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 numval = get_tv_number_chk(varp, &error);
16403 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 if (!error && strval != NULL)
16405 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 }
16407 else
16408 {
16409 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16410 if (bufvarname != NULL)
16411 {
16412 STRCPY(bufvarname, "b:");
16413 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016414 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 vim_free(bufvarname);
16416 }
16417 }
16418
16419 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422}
16423
16424/*
16425 * "setcmdpos()" function
16426 */
16427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016428f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 typval_T *argvars;
16430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 int pos = (int)get_tv_number(&argvars[0]) - 1;
16433
16434 if (pos >= 0)
16435 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436}
16437
16438/*
16439 * "setline()" function
16440 */
16441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016442f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016443 typval_T *argvars;
16444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445{
16446 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016447 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016448 list_T *l = NULL;
16449 listitem_T *li = NULL;
16450 long added = 0;
16451 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016453 lnum = get_tv_lnum(&argvars[0]);
16454 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016456 l = argvars[1].vval.v_list;
16457 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016459 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016460 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016461
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016462 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016463 for (;;)
16464 {
16465 if (l != NULL)
16466 {
16467 /* list argument, get next string */
16468 if (li == NULL)
16469 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016471 li = li->li_next;
16472 }
16473
16474 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016475 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016476 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016477
16478 /* When coming here from Insert mode, sync undo, so that this can be
16479 * undone separately from what was previously inserted. */
16480 if (u_sync_once == 2)
16481 {
16482 u_sync_once = 1; /* notify that u_sync() was called */
16483 u_sync(TRUE);
16484 }
16485
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016486 if (lnum <= curbuf->b_ml.ml_line_count)
16487 {
16488 /* existing line, replace it */
16489 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16490 {
16491 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016492 if (lnum == curwin->w_cursor.lnum)
16493 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016494 rettv->vval.v_number = 0; /* OK */
16495 }
16496 }
16497 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16498 {
16499 /* lnum is one past the last line, append the line */
16500 ++added;
16501 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16502 rettv->vval.v_number = 0; /* OK */
16503 }
16504
16505 if (l == NULL) /* only one string argument */
16506 break;
16507 ++lnum;
16508 }
16509
16510 if (added > 0)
16511 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512}
16513
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016514static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16515
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016517 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016518 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016519 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016520set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016521 win_T *wp UNUSED;
16522 typval_T *list_arg UNUSED;
16523 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016524 typval_T *rettv;
16525{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016526#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016527 char_u *act;
16528 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016529#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016530
Bram Moolenaar2641f772005-03-25 21:58:17 +000016531 rettv->vval.v_number = -1;
16532
16533#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016534 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016535 EMSG(_(e_listreq));
16536 else
16537 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016538 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016539
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016540 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016541 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016542 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 if (act == NULL)
16544 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016545 if (*act == 'a' || *act == 'r')
16546 action = *act;
16547 }
16548
Bram Moolenaar81484f42012-12-05 15:16:47 +010016549 if (l != NULL && set_errorlist(wp, l, action,
16550 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016551 rettv->vval.v_number = 0;
16552 }
16553#endif
16554}
16555
16556/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016557 * "setloclist()" function
16558 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016559 static void
16560f_setloclist(argvars, rettv)
16561 typval_T *argvars;
16562 typval_T *rettv;
16563{
16564 win_T *win;
16565
16566 rettv->vval.v_number = -1;
16567
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016568 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016569 if (win != NULL)
16570 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16571}
16572
16573/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016574 * "setmatches()" function
16575 */
16576 static void
16577f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016578 typval_T *argvars UNUSED;
16579 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016580{
16581#ifdef FEAT_SEARCH_EXTRA
16582 list_T *l;
16583 listitem_T *li;
16584 dict_T *d;
16585
16586 rettv->vval.v_number = -1;
16587 if (argvars[0].v_type != VAR_LIST)
16588 {
16589 EMSG(_(e_listreq));
16590 return;
16591 }
16592 if ((l = argvars[0].vval.v_list) != NULL)
16593 {
16594
16595 /* To some extent make sure that we are dealing with a list from
16596 * "getmatches()". */
16597 li = l->lv_first;
16598 while (li != NULL)
16599 {
16600 if (li->li_tv.v_type != VAR_DICT
16601 || (d = li->li_tv.vval.v_dict) == NULL)
16602 {
16603 EMSG(_(e_invarg));
16604 return;
16605 }
16606 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16607 && dict_find(d, (char_u *)"pattern", -1) != NULL
16608 && dict_find(d, (char_u *)"priority", -1) != NULL
16609 && dict_find(d, (char_u *)"id", -1) != NULL))
16610 {
16611 EMSG(_(e_invarg));
16612 return;
16613 }
16614 li = li->li_next;
16615 }
16616
16617 clear_matches(curwin);
16618 li = l->lv_first;
16619 while (li != NULL)
16620 {
16621 d = li->li_tv.vval.v_dict;
16622 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16623 get_dict_string(d, (char_u *)"pattern", FALSE),
16624 (int)get_dict_number(d, (char_u *)"priority"),
16625 (int)get_dict_number(d, (char_u *)"id"));
16626 li = li->li_next;
16627 }
16628 rettv->vval.v_number = 0;
16629 }
16630#endif
16631}
16632
16633/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016634 * "setpos()" function
16635 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016636 static void
16637f_setpos(argvars, rettv)
16638 typval_T *argvars;
16639 typval_T *rettv;
16640{
16641 pos_T pos;
16642 int fnum;
16643 char_u *name;
16644
Bram Moolenaar08250432008-02-13 11:42:46 +000016645 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016646 name = get_tv_string_chk(argvars);
16647 if (name != NULL)
16648 {
16649 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16650 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016651 if (--pos.col < 0)
16652 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016653 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016654 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016655 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016656 if (fnum == curbuf->b_fnum)
16657 {
16658 curwin->w_cursor = pos;
16659 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016660 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016661 }
16662 else
16663 EMSG(_(e_invarg));
16664 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016665 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16666 {
16667 /* set mark */
16668 if (setmark_pos(name[1], &pos, fnum) == OK)
16669 rettv->vval.v_number = 0;
16670 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016671 else
16672 EMSG(_(e_invarg));
16673 }
16674 }
16675}
16676
16677/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016678 * "setqflist()" function
16679 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016680 static void
16681f_setqflist(argvars, rettv)
16682 typval_T *argvars;
16683 typval_T *rettv;
16684{
16685 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16686}
16687
16688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 * "setreg()" function
16690 */
16691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016693 typval_T *argvars;
16694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695{
16696 int regname;
16697 char_u *strregname;
16698 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016700 int append;
16701 char_u yank_type;
16702 long block_len;
16703
16704 block_len = -1;
16705 yank_type = MAUTO;
16706 append = FALSE;
16707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 if (strregname == NULL)
16712 return; /* type error; errmsg already given */
16713 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 if (regname == 0 || regname == '@')
16715 regname = '"';
16716 else if (regname == '=')
16717 return;
16718
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016719 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 stropt = get_tv_string_chk(&argvars[2]);
16722 if (stropt == NULL)
16723 return; /* type error */
16724 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 switch (*stropt)
16726 {
16727 case 'a': case 'A': /* append */
16728 append = TRUE;
16729 break;
16730 case 'v': case 'c': /* character-wise selection */
16731 yank_type = MCHAR;
16732 break;
16733 case 'V': case 'l': /* line-wise selection */
16734 yank_type = MLINE;
16735 break;
16736#ifdef FEAT_VISUAL
16737 case 'b': case Ctrl_V: /* block-wise selection */
16738 yank_type = MBLOCK;
16739 if (VIM_ISDIGIT(stropt[1]))
16740 {
16741 ++stropt;
16742 block_len = getdigits(&stropt) - 1;
16743 --stropt;
16744 }
16745 break;
16746#endif
16747 }
16748 }
16749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 strval = get_tv_string_chk(&argvars[1]);
16751 if (strval != NULL)
16752 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755}
16756
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016757/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016758 * "settabvar()" function
16759 */
16760 static void
16761f_settabvar(argvars, rettv)
16762 typval_T *argvars;
16763 typval_T *rettv;
16764{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016765#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016766 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016767 tabpage_T *tp;
16768#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016769 char_u *varname, *tabvarname;
16770 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016771
16772 rettv->vval.v_number = 0;
16773
16774 if (check_restricted() || check_secure())
16775 return;
16776
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016777#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016778 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016779#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016780 varname = get_tv_string_chk(&argvars[1]);
16781 varp = &argvars[2];
16782
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016783 if (varname != NULL && varp != NULL
16784#ifdef FEAT_WINDOWS
16785 && tp != NULL
16786#endif
16787 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016788 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016789#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016790 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016791 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016792#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016793
16794 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16795 if (tabvarname != NULL)
16796 {
16797 STRCPY(tabvarname, "t:");
16798 STRCPY(tabvarname + 2, varname);
16799 set_var(tabvarname, varp, TRUE);
16800 vim_free(tabvarname);
16801 }
16802
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016803#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016804 /* Restore current tabpage */
16805 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016806 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016807#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016808 }
16809}
16810
16811/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016812 * "settabwinvar()" function
16813 */
16814 static void
16815f_settabwinvar(argvars, rettv)
16816 typval_T *argvars;
16817 typval_T *rettv;
16818{
16819 setwinvar(argvars, rettv, 1);
16820}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821
16822/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016823 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016826f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 typval_T *argvars;
16828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016830 setwinvar(argvars, rettv, 0);
16831}
16832
16833/*
16834 * "setwinvar()" and "settabwinvar()" functions
16835 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016836
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016837 static void
16838setwinvar(argvars, rettv, off)
16839 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016840 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016841 int off;
16842{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 win_T *win;
16844#ifdef FEAT_WINDOWS
16845 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016846 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847#endif
16848 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016851 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852
16853 if (check_restricted() || check_secure())
16854 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016855
16856#ifdef FEAT_WINDOWS
16857 if (off == 1)
16858 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16859 else
16860 tp = curtab;
16861#endif
16862 win = find_win_by_nr(&argvars[off], tp);
16863 varname = get_tv_string_chk(&argvars[off + 1]);
16864 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
16866 if (win != NULL && varname != NULL && varp != NULL)
16867 {
16868#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016869 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871#endif
16872
16873 if (*varname == '&')
16874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016875 long numval;
16876 char_u *strval;
16877 int error = FALSE;
16878
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016880 numval = get_tv_number_chk(varp, &error);
16881 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 if (!error && strval != NULL)
16883 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 }
16885 else
16886 {
16887 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16888 if (winvarname != NULL)
16889 {
16890 STRCPY(winvarname, "w:");
16891 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016892 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 vim_free(winvarname);
16894 }
16895 }
16896
16897#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016898 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899#endif
16900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901}
16902
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016903#ifdef FEAT_CRYPT
16904/*
16905 * "sha256({string})" function
16906 */
16907 static void
16908f_sha256(argvars, rettv)
16909 typval_T *argvars;
16910 typval_T *rettv;
16911{
16912 char_u *p;
16913
16914 p = get_tv_string(&argvars[0]);
16915 rettv->vval.v_string = vim_strsave(
16916 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16917 rettv->v_type = VAR_STRING;
16918}
16919#endif /* FEAT_CRYPT */
16920
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016922 * "shellescape({string})" function
16923 */
16924 static void
16925f_shellescape(argvars, rettv)
16926 typval_T *argvars;
16927 typval_T *rettv;
16928{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016929 rettv->vval.v_string = vim_strsave_shellescape(
16930 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016931 rettv->v_type = VAR_STRING;
16932}
16933
16934/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016935 * shiftwidth() function
16936 */
16937 static void
16938f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016939 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016940 typval_T *rettv;
16941{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016942 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016943}
16944
16945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947 */
16948 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016950 typval_T *argvars;
16951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 p = get_tv_string(&argvars[0]);
16956 rettv->vval.v_string = vim_strsave(p);
16957 simplify_filename(rettv->vval.v_string); /* simplify in place */
16958 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959}
16960
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016961#ifdef FEAT_FLOAT
16962/*
16963 * "sin()" function
16964 */
16965 static void
16966f_sin(argvars, rettv)
16967 typval_T *argvars;
16968 typval_T *rettv;
16969{
16970 float_T f;
16971
16972 rettv->v_type = VAR_FLOAT;
16973 if (get_float_arg(argvars, &f) == OK)
16974 rettv->vval.v_float = sin(f);
16975 else
16976 rettv->vval.v_float = 0.0;
16977}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016978
16979/*
16980 * "sinh()" function
16981 */
16982 static void
16983f_sinh(argvars, rettv)
16984 typval_T *argvars;
16985 typval_T *rettv;
16986{
16987 float_T f;
16988
16989 rettv->v_type = VAR_FLOAT;
16990 if (get_float_arg(argvars, &f) == OK)
16991 rettv->vval.v_float = sinh(f);
16992 else
16993 rettv->vval.v_float = 0.0;
16994}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016995#endif
16996
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997static int
16998#ifdef __BORLANDC__
16999 _RTLENTRYF
17000#endif
17001 item_compare __ARGS((const void *s1, const void *s2));
17002static int
17003#ifdef __BORLANDC__
17004 _RTLENTRYF
17005#endif
17006 item_compare2 __ARGS((const void *s1, const void *s2));
17007
17008static int item_compare_ic;
17009static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017010static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012#define ITEM_COMPARE_FAIL 999
17013
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 static int
17018#ifdef __BORLANDC__
17019_RTLENTRYF
17020#endif
17021item_compare(s1, s2)
17022 const void *s1;
17023 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 char_u *p1, *p2;
17026 char_u *tofree1, *tofree2;
17027 int res;
17028 char_u numbuf1[NUMBUFLEN];
17029 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017031 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17032 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017033 if (p1 == NULL)
17034 p1 = (char_u *)"";
17035 if (p2 == NULL)
17036 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017037 if (item_compare_ic)
17038 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 res = STRCMP(p1, p2);
17041 vim_free(tofree1);
17042 vim_free(tofree2);
17043 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044}
17045
17046 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047#ifdef __BORLANDC__
17048_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050item_compare2(s1, s2)
17051 const void *s1;
17052 const void *s2;
17053{
17054 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017055 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017056 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017059 /* shortcut after failure in previous call; compare all items equal */
17060 if (item_compare_func_err)
17061 return 0;
17062
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017063 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17064 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17066 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067
17068 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017069 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017070 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17071 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 clear_tv(&argv[0]);
17073 clear_tv(&argv[1]);
17074
17075 if (res == FAIL)
17076 res = ITEM_COMPARE_FAIL;
17077 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17079 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017080 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 clear_tv(&rettv);
17082 return res;
17083}
17084
17085/*
17086 * "sort({list})" function
17087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 typval_T *argvars;
17091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092{
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 list_T *l;
17094 listitem_T *li;
17095 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 long len;
17097 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 if (argvars[0].v_type != VAR_LIST)
17100 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 else
17102 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017104 if (l == NULL || tv_check_lock(l->lv_lock,
17105 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 return;
17107 rettv->vval.v_list = l;
17108 rettv->v_type = VAR_LIST;
17109 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111 len = list_len(l);
17112 if (len <= 1)
17113 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115 item_compare_ic = FALSE;
17116 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017117 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 if (argvars[1].v_type != VAR_UNKNOWN)
17119 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017120 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 else
17124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017125 int error = FALSE;
17126
17127 i = get_tv_number_chk(&argvars[1], &error);
17128 if (error)
17129 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 if (i == 1)
17131 item_compare_ic = TRUE;
17132 else
17133 item_compare_func = get_tv_string(&argvars[1]);
17134 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017135
17136 if (argvars[2].v_type != VAR_UNKNOWN)
17137 {
17138 /* optional third argument: {dict} */
17139 if (argvars[2].v_type != VAR_DICT)
17140 {
17141 EMSG(_(e_dictreq));
17142 return;
17143 }
17144 item_compare_selfdict = argvars[2].vval.v_dict;
17145 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017149 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 if (ptrs == NULL)
17151 return;
17152 i = 0;
17153 for (li = l->lv_first; li != NULL; li = li->li_next)
17154 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 /* test the compare function */
17158 if (item_compare_func != NULL
17159 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17160 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017161 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163 {
17164 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017165 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 item_compare_func == NULL ? item_compare : item_compare2);
17167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017168 if (!item_compare_func_err)
17169 {
17170 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017171 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017172 l->lv_len = 0;
17173 for (i = 0; i < len; ++i)
17174 list_append(l, ptrs[i]);
17175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 }
17177
17178 vim_free(ptrs);
17179 }
17180}
17181
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017182/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017183 * "soundfold({word})" function
17184 */
17185 static void
17186f_soundfold(argvars, rettv)
17187 typval_T *argvars;
17188 typval_T *rettv;
17189{
17190 char_u *s;
17191
17192 rettv->v_type = VAR_STRING;
17193 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017194#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017195 rettv->vval.v_string = eval_soundfold(s);
17196#else
17197 rettv->vval.v_string = vim_strsave(s);
17198#endif
17199}
17200
17201/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017202 * "spellbadword()" function
17203 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017204 static void
17205f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017206 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017207 typval_T *rettv;
17208{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017209 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017210 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017211 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017213 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017214 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017215
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017216#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017217 if (argvars[0].v_type == VAR_UNKNOWN)
17218 {
17219 /* Find the start and length of the badly spelled word. */
17220 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17221 if (len != 0)
17222 word = ml_get_cursor();
17223 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017224 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017225 {
17226 char_u *str = get_tv_string_chk(&argvars[0]);
17227 int capcol = -1;
17228
17229 if (str != NULL)
17230 {
17231 /* Check the argument for spelling. */
17232 while (*str != NUL)
17233 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017234 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017235 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017236 {
17237 word = str;
17238 break;
17239 }
17240 str += len;
17241 }
17242 }
17243 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017244#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017246 list_append_string(rettv->vval.v_list, word, len);
17247 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017248 attr == HLF_SPB ? "bad" :
17249 attr == HLF_SPR ? "rare" :
17250 attr == HLF_SPL ? "local" :
17251 attr == HLF_SPC ? "caps" :
17252 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017253}
17254
17255/*
17256 * "spellsuggest()" function
17257 */
17258 static void
17259f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017260 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017261 typval_T *rettv;
17262{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017263#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017264 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017265 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017266 int maxcount;
17267 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017268 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017269 listitem_T *li;
17270 int need_capital = FALSE;
17271#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017272
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017273 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017274 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017275
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017276#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017277 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017278 {
17279 str = get_tv_string(&argvars[0]);
17280 if (argvars[1].v_type != VAR_UNKNOWN)
17281 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017282 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017283 if (maxcount <= 0)
17284 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017285 if (argvars[2].v_type != VAR_UNKNOWN)
17286 {
17287 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17288 if (typeerr)
17289 return;
17290 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017291 }
17292 else
17293 maxcount = 25;
17294
Bram Moolenaar4770d092006-01-12 23:22:24 +000017295 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017296
17297 for (i = 0; i < ga.ga_len; ++i)
17298 {
17299 str = ((char_u **)ga.ga_data)[i];
17300
17301 li = listitem_alloc();
17302 if (li == NULL)
17303 vim_free(str);
17304 else
17305 {
17306 li->li_tv.v_type = VAR_STRING;
17307 li->li_tv.v_lock = 0;
17308 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017309 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017310 }
17311 }
17312 ga_clear(&ga);
17313 }
17314#endif
17315}
17316
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017318f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 typval_T *argvars;
17320 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321{
17322 char_u *str;
17323 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017324 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017325 regmatch_T regmatch;
17326 char_u patbuf[NUMBUFLEN];
17327 char_u *save_cpo;
17328 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017330 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017332
17333 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17334 save_cpo = p_cpo;
17335 p_cpo = (char_u *)"";
17336
17337 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017338 if (argvars[1].v_type != VAR_UNKNOWN)
17339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17341 if (pat == NULL)
17342 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017343 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017344 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017345 }
17346 if (pat == NULL || *pat == NUL)
17347 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017349 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017351 if (typeerr)
17352 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17355 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017357 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017358 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017360 if (*str == NUL)
17361 match = FALSE; /* empty item at the end */
17362 else
17363 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364 if (match)
17365 end = regmatch.startp[0];
17366 else
17367 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017368 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17369 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017371 if (list_append_string(rettv->vval.v_list, str,
17372 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374 }
17375 if (!match)
17376 break;
17377 /* Advance to just after the match. */
17378 if (regmatch.endp[0] > str)
17379 col = 0;
17380 else
17381 {
17382 /* Don't get stuck at the same match. */
17383#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017384 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017385#else
17386 col = 1;
17387#endif
17388 }
17389 str = regmatch.endp[0];
17390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar473de612013-06-08 18:19:48 +020017392 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396}
17397
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398#ifdef FEAT_FLOAT
17399/*
17400 * "sqrt()" function
17401 */
17402 static void
17403f_sqrt(argvars, rettv)
17404 typval_T *argvars;
17405 typval_T *rettv;
17406{
17407 float_T f;
17408
17409 rettv->v_type = VAR_FLOAT;
17410 if (get_float_arg(argvars, &f) == OK)
17411 rettv->vval.v_float = sqrt(f);
17412 else
17413 rettv->vval.v_float = 0.0;
17414}
17415
17416/*
17417 * "str2float()" function
17418 */
17419 static void
17420f_str2float(argvars, rettv)
17421 typval_T *argvars;
17422 typval_T *rettv;
17423{
17424 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17425
17426 if (*p == '+')
17427 p = skipwhite(p + 1);
17428 (void)string2float(p, &rettv->vval.v_float);
17429 rettv->v_type = VAR_FLOAT;
17430}
17431#endif
17432
Bram Moolenaar2c932302006-03-18 21:42:09 +000017433/*
17434 * "str2nr()" function
17435 */
17436 static void
17437f_str2nr(argvars, rettv)
17438 typval_T *argvars;
17439 typval_T *rettv;
17440{
17441 int base = 10;
17442 char_u *p;
17443 long n;
17444
17445 if (argvars[1].v_type != VAR_UNKNOWN)
17446 {
17447 base = get_tv_number(&argvars[1]);
17448 if (base != 8 && base != 10 && base != 16)
17449 {
17450 EMSG(_(e_invarg));
17451 return;
17452 }
17453 }
17454
17455 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017456 if (*p == '+')
17457 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017458 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17459 rettv->vval.v_number = n;
17460}
17461
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462#ifdef HAVE_STRFTIME
17463/*
17464 * "strftime({format}[, {time}])" function
17465 */
17466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017467f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017468 typval_T *argvars;
17469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470{
17471 char_u result_buf[256];
17472 struct tm *curtime;
17473 time_t seconds;
17474 char_u *p;
17475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017478 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017479 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 seconds = time(NULL);
17481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 curtime = localtime(&seconds);
17484 /* MSVC returns NULL for an invalid value of seconds. */
17485 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 else
17488 {
17489# ifdef FEAT_MBYTE
17490 vimconv_T conv;
17491 char_u *enc;
17492
17493 conv.vc_type = CONV_NONE;
17494 enc = enc_locale();
17495 convert_setup(&conv, p_enc, enc);
17496 if (conv.vc_type != CONV_NONE)
17497 p = string_convert(&conv, p, NULL);
17498# endif
17499 if (p != NULL)
17500 (void)strftime((char *)result_buf, sizeof(result_buf),
17501 (char *)p, curtime);
17502 else
17503 result_buf[0] = NUL;
17504
17505# ifdef FEAT_MBYTE
17506 if (conv.vc_type != CONV_NONE)
17507 vim_free(p);
17508 convert_setup(&conv, enc, p_enc);
17509 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 else
17512# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514
17515# ifdef FEAT_MBYTE
17516 /* Release conversion descriptors */
17517 convert_setup(&conv, NULL, NULL);
17518 vim_free(enc);
17519# endif
17520 }
17521}
17522#endif
17523
17524/*
17525 * "stridx()" function
17526 */
17527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017529 typval_T *argvars;
17530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531{
17532 char_u buf[NUMBUFLEN];
17533 char_u *needle;
17534 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017535 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017537 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 needle = get_tv_string_chk(&argvars[1]);
17540 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017542 if (needle == NULL || haystack == NULL)
17543 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544
Bram Moolenaar33570922005-01-25 22:26:29 +000017545 if (argvars[2].v_type != VAR_UNKNOWN)
17546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 int error = FALSE;
17548
17549 start_idx = get_tv_number_chk(&argvars[2], &error);
17550 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017552 if (start_idx >= 0)
17553 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 }
17555
17556 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17557 if (pos != NULL)
17558 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559}
17560
17561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017562 * "string()" function
17563 */
17564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017566 typval_T *argvars;
17567 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017568{
17569 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017573 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017574 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017575 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017576 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577}
17578
17579/*
17580 * "strlen()" function
17581 */
17582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017584 typval_T *argvars;
17585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017587 rettv->vval.v_number = (varnumber_T)(STRLEN(
17588 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589}
17590
17591/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017592 * "strchars()" function
17593 */
17594 static void
17595f_strchars(argvars, rettv)
17596 typval_T *argvars;
17597 typval_T *rettv;
17598{
17599 char_u *s = get_tv_string(&argvars[0]);
17600#ifdef FEAT_MBYTE
17601 varnumber_T len = 0;
17602
17603 while (*s != NUL)
17604 {
17605 mb_cptr2char_adv(&s);
17606 ++len;
17607 }
17608 rettv->vval.v_number = len;
17609#else
17610 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17611#endif
17612}
17613
17614/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017615 * "strdisplaywidth()" function
17616 */
17617 static void
17618f_strdisplaywidth(argvars, rettv)
17619 typval_T *argvars;
17620 typval_T *rettv;
17621{
17622 char_u *s = get_tv_string(&argvars[0]);
17623 int col = 0;
17624
17625 if (argvars[1].v_type != VAR_UNKNOWN)
17626 col = get_tv_number(&argvars[1]);
17627
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017628 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017629}
17630
17631/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017632 * "strwidth()" function
17633 */
17634 static void
17635f_strwidth(argvars, rettv)
17636 typval_T *argvars;
17637 typval_T *rettv;
17638{
17639 char_u *s = get_tv_string(&argvars[0]);
17640
17641 rettv->vval.v_number = (varnumber_T)(
17642#ifdef FEAT_MBYTE
17643 mb_string2cells(s, -1)
17644#else
17645 STRLEN(s)
17646#endif
17647 );
17648}
17649
17650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 * "strpart()" function
17652 */
17653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 typval_T *argvars;
17656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657{
17658 char_u *p;
17659 int n;
17660 int len;
17661 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 slen = (int)STRLEN(p);
17666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017667 n = get_tv_number_chk(&argvars[1], &error);
17668 if (error)
17669 len = 0;
17670 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 else
17673 len = slen - n; /* default len: all bytes that are available. */
17674
17675 /*
17676 * Only return the overlap between the specified part and the actual
17677 * string.
17678 */
17679 if (n < 0)
17680 {
17681 len += n;
17682 n = 0;
17683 }
17684 else if (n > slen)
17685 n = slen;
17686 if (len < 0)
17687 len = 0;
17688 else if (n + len > slen)
17689 len = slen - n;
17690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017691 rettv->v_type = VAR_STRING;
17692 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693}
17694
17695/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017696 * "strridx()" function
17697 */
17698 static void
17699f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 typval_T *argvars;
17701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702{
17703 char_u buf[NUMBUFLEN];
17704 char_u *needle;
17705 char_u *haystack;
17706 char_u *rest;
17707 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017708 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017710 needle = get_tv_string_chk(&argvars[1]);
17711 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712
17713 rettv->vval.v_number = -1;
17714 if (needle == NULL || haystack == NULL)
17715 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017716
17717 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017718 if (argvars[2].v_type != VAR_UNKNOWN)
17719 {
17720 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017722 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017723 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017724 }
17725 else
17726 end_idx = haystack_len;
17727
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017729 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017730 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017731 lastmatch = haystack + end_idx;
17732 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017735 for (rest = haystack; *rest != '\0'; ++rest)
17736 {
17737 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017738 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 break;
17740 lastmatch = rest;
17741 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017742 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017743
17744 if (lastmatch == NULL)
17745 rettv->vval.v_number = -1;
17746 else
17747 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17748}
17749
17750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 * "strtrans()" function
17752 */
17753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 typval_T *argvars;
17756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758 rettv->v_type = VAR_STRING;
17759 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760}
17761
17762/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 * "submatch()" function
17764 */
17765 static void
17766f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 typval_T *argvars;
17768 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017769{
17770 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 rettv->vval.v_string =
17772 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773}
17774
17775/*
17776 * "substitute()" function
17777 */
17778 static void
17779f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017780 typval_T *argvars;
17781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017782{
17783 char_u patbuf[NUMBUFLEN];
17784 char_u subbuf[NUMBUFLEN];
17785 char_u flagsbuf[NUMBUFLEN];
17786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 char_u *str = get_tv_string_chk(&argvars[0]);
17788 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17789 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17790 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17791
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017793 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17794 rettv->vval.v_string = NULL;
17795 else
17796 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017797}
17798
17799/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017800 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017803f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017804 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806{
17807 int id = 0;
17808#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017809 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 long col;
17811 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017812 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 lnum = get_tv_lnum(argvars); /* -1 on type error */
17815 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17816 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017818 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017819 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017820 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821#endif
17822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017823 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824}
17825
17826/*
17827 * "synIDattr(id, what [, mode])" function
17828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017830f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017831 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833{
17834 char_u *p = NULL;
17835#ifdef FEAT_SYN_HL
17836 int id;
17837 char_u *what;
17838 char_u *mode;
17839 char_u modebuf[NUMBUFLEN];
17840 int modec;
17841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017842 id = get_tv_number(&argvars[0]);
17843 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017844 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017848 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 modec = 0; /* replace invalid with current */
17850 }
17851 else
17852 {
17853#ifdef FEAT_GUI
17854 if (gui.in_use)
17855 modec = 'g';
17856 else
17857#endif
17858 if (t_colors > 1)
17859 modec = 'c';
17860 else
17861 modec = 't';
17862 }
17863
17864
17865 switch (TOLOWER_ASC(what[0]))
17866 {
17867 case 'b':
17868 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17869 p = highlight_color(id, what, modec);
17870 else /* bold */
17871 p = highlight_has_attr(id, HL_BOLD, modec);
17872 break;
17873
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017874 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 p = highlight_color(id, what, modec);
17876 break;
17877
17878 case 'i':
17879 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17880 p = highlight_has_attr(id, HL_INVERSE, modec);
17881 else /* italic */
17882 p = highlight_has_attr(id, HL_ITALIC, modec);
17883 break;
17884
17885 case 'n': /* name */
17886 p = get_highlight_name(NULL, id - 1);
17887 break;
17888
17889 case 'r': /* reverse */
17890 p = highlight_has_attr(id, HL_INVERSE, modec);
17891 break;
17892
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017893 case 's':
17894 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17895 p = highlight_color(id, what, modec);
17896 else /* standout */
17897 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 break;
17899
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017900 case 'u':
17901 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17902 /* underline */
17903 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17904 else
17905 /* undercurl */
17906 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 break;
17908 }
17909
17910 if (p != NULL)
17911 p = vim_strsave(p);
17912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017913 rettv->v_type = VAR_STRING;
17914 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
17918 * "synIDtrans(id)" function
17919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017921f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924{
17925 int id;
17926
17927#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017928 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929
17930 if (id > 0)
17931 id = syn_get_final_id(id);
17932 else
17933#endif
17934 id = 0;
17935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937}
17938
17939/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017940 * "synconcealed(lnum, col)" function
17941 */
17942 static void
17943f_synconcealed(argvars, rettv)
17944 typval_T *argvars UNUSED;
17945 typval_T *rettv;
17946{
17947#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17948 long lnum;
17949 long col;
17950 int syntax_flags = 0;
17951 int cchar;
17952 int matchid = 0;
17953 char_u str[NUMBUFLEN];
17954#endif
17955
17956 rettv->v_type = VAR_LIST;
17957 rettv->vval.v_list = NULL;
17958
17959#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17960 lnum = get_tv_lnum(argvars); /* -1 on type error */
17961 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17962
17963 vim_memset(str, NUL, sizeof(str));
17964
17965 if (rettv_list_alloc(rettv) != FAIL)
17966 {
17967 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17968 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17969 && curwin->w_p_cole > 0)
17970 {
17971 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17972 syntax_flags = get_syntax_info(&matchid);
17973
17974 /* get the conceal character */
17975 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17976 {
17977 cchar = syn_get_sub_char();
17978 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17979 cchar = lcs_conceal;
17980 if (cchar != NUL)
17981 {
17982# ifdef FEAT_MBYTE
17983 if (has_mbyte)
17984 (*mb_char2bytes)(cchar, str);
17985 else
17986# endif
17987 str[0] = cchar;
17988 }
17989 }
17990 }
17991
17992 list_append_number(rettv->vval.v_list,
17993 (syntax_flags & HL_CONCEAL) != 0);
17994 /* -1 to auto-determine strlen */
17995 list_append_string(rettv->vval.v_list, str, -1);
17996 list_append_number(rettv->vval.v_list, matchid);
17997 }
17998#endif
17999}
18000
18001/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018002 * "synstack(lnum, col)" function
18003 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018004 static void
18005f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018006 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018007 typval_T *rettv;
18008{
18009#ifdef FEAT_SYN_HL
18010 long lnum;
18011 long col;
18012 int i;
18013 int id;
18014#endif
18015
18016 rettv->v_type = VAR_LIST;
18017 rettv->vval.v_list = NULL;
18018
18019#ifdef FEAT_SYN_HL
18020 lnum = get_tv_lnum(argvars); /* -1 on type error */
18021 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18022
18023 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018024 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018025 && rettv_list_alloc(rettv) != FAIL)
18026 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018027 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018028 for (i = 0; ; ++i)
18029 {
18030 id = syn_get_stack_item(i);
18031 if (id < 0)
18032 break;
18033 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18034 break;
18035 }
18036 }
18037#endif
18038}
18039
18040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 * "system()" function
18042 */
18043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *argvars;
18046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018048 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018050 char_u *infile = NULL;
18051 char_u buf[NUMBUFLEN];
18052 int err = FALSE;
18053 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018055 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018056 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018058 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018059 {
18060 /*
18061 * Write the string to a temp file, to be used for input of the shell
18062 * command.
18063 */
18064 if ((infile = vim_tempname('i')) == NULL)
18065 {
18066 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018067 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018068 }
18069
18070 fd = mch_fopen((char *)infile, WRITEBIN);
18071 if (fd == NULL)
18072 {
18073 EMSG2(_(e_notopen), infile);
18074 goto done;
18075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018076 p = get_tv_string_buf_chk(&argvars[1], buf);
18077 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018078 {
18079 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018081 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018082 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18083 err = TRUE;
18084 if (fclose(fd) != 0)
18085 err = TRUE;
18086 if (err)
18087 {
18088 EMSG(_("E677: Error writing temp file"));
18089 goto done;
18090 }
18091 }
18092
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018093 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18094 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018095
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096#ifdef USE_CR
18097 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018098 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 {
18100 char_u *s;
18101
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018102 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 {
18104 if (*s == CAR)
18105 *s = NL;
18106 }
18107 }
18108#else
18109# ifdef USE_CRNL
18110 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018111 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 {
18113 char_u *s, *d;
18114
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018115 d = res;
18116 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 {
18118 if (s[0] == CAR && s[1] == NL)
18119 ++s;
18120 *d++ = *s;
18121 }
18122 *d = NUL;
18123 }
18124# endif
18125#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018126
18127done:
18128 if (infile != NULL)
18129 {
18130 mch_remove(infile);
18131 vim_free(infile);
18132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 rettv->v_type = VAR_STRING;
18134 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018138 * "tabpagebuflist()" function
18139 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018140 static void
18141f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018142 typval_T *argvars UNUSED;
18143 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018144{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018145#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018146 tabpage_T *tp;
18147 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018148
18149 if (argvars[0].v_type == VAR_UNKNOWN)
18150 wp = firstwin;
18151 else
18152 {
18153 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18154 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018155 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018156 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018157 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018158 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018159 for (; wp != NULL; wp = wp->w_next)
18160 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018161 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018162 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018163 }
18164#endif
18165}
18166
18167
18168/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018169 * "tabpagenr()" function
18170 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018171 static void
18172f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018173 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018174 typval_T *rettv;
18175{
18176 int nr = 1;
18177#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018178 char_u *arg;
18179
18180 if (argvars[0].v_type != VAR_UNKNOWN)
18181 {
18182 arg = get_tv_string_chk(&argvars[0]);
18183 nr = 0;
18184 if (arg != NULL)
18185 {
18186 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018187 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018188 else
18189 EMSG2(_(e_invexpr2), arg);
18190 }
18191 }
18192 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018193 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018194#endif
18195 rettv->vval.v_number = nr;
18196}
18197
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018198
18199#ifdef FEAT_WINDOWS
18200static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18201
18202/*
18203 * Common code for tabpagewinnr() and winnr().
18204 */
18205 static int
18206get_winnr(tp, argvar)
18207 tabpage_T *tp;
18208 typval_T *argvar;
18209{
18210 win_T *twin;
18211 int nr = 1;
18212 win_T *wp;
18213 char_u *arg;
18214
18215 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18216 if (argvar->v_type != VAR_UNKNOWN)
18217 {
18218 arg = get_tv_string_chk(argvar);
18219 if (arg == NULL)
18220 nr = 0; /* type error; errmsg already given */
18221 else if (STRCMP(arg, "$") == 0)
18222 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18223 else if (STRCMP(arg, "#") == 0)
18224 {
18225 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18226 if (twin == NULL)
18227 nr = 0;
18228 }
18229 else
18230 {
18231 EMSG2(_(e_invexpr2), arg);
18232 nr = 0;
18233 }
18234 }
18235
18236 if (nr > 0)
18237 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18238 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018239 {
18240 if (wp == NULL)
18241 {
18242 /* didn't find it in this tabpage */
18243 nr = 0;
18244 break;
18245 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018246 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018247 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018248 return nr;
18249}
18250#endif
18251
18252/*
18253 * "tabpagewinnr()" function
18254 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018255 static void
18256f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018257 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018258 typval_T *rettv;
18259{
18260 int nr = 1;
18261#ifdef FEAT_WINDOWS
18262 tabpage_T *tp;
18263
18264 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18265 if (tp == NULL)
18266 nr = 0;
18267 else
18268 nr = get_winnr(tp, &argvars[1]);
18269#endif
18270 rettv->vval.v_number = nr;
18271}
18272
18273
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018274/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018275 * "tagfiles()" function
18276 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018277 static void
18278f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018279 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018280 typval_T *rettv;
18281{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018282 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018283 tagname_T tn;
18284 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018285
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018286 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018287 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018288 fname = alloc(MAXPATHL);
18289 if (fname == NULL)
18290 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018292 for (first = TRUE; ; first = FALSE)
18293 if (get_tagfname(&tn, first, fname) == FAIL
18294 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018295 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018296 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018297 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018298}
18299
18300/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018301 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018302 */
18303 static void
18304f_taglist(argvars, rettv)
18305 typval_T *argvars;
18306 typval_T *rettv;
18307{
18308 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018309
18310 tag_pattern = get_tv_string(&argvars[0]);
18311
18312 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018313 if (*tag_pattern == NUL)
18314 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018315
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018316 if (rettv_list_alloc(rettv) == OK)
18317 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018318}
18319
18320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 * "tempname()" function
18322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018324f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018325 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
18328 static int x = 'A';
18329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018330 rettv->v_type = VAR_STRING;
18331 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332
18333 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18334 * names. Skip 'I' and 'O', they are used for shell redirection. */
18335 do
18336 {
18337 if (x == 'Z')
18338 x = '0';
18339 else if (x == '9')
18340 x = 'A';
18341 else
18342 {
18343#ifdef EBCDIC
18344 if (x == 'I')
18345 x = 'J';
18346 else if (x == 'R')
18347 x = 'S';
18348 else
18349#endif
18350 ++x;
18351 }
18352 } while (x == 'I' || x == 'O');
18353}
18354
18355/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018356 * "test(list)" function: Just checking the walls...
18357 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018358 static void
18359f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018360 typval_T *argvars UNUSED;
18361 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018362{
18363 /* Used for unit testing. Change the code below to your liking. */
18364#if 0
18365 listitem_T *li;
18366 list_T *l;
18367 char_u *bad, *good;
18368
18369 if (argvars[0].v_type != VAR_LIST)
18370 return;
18371 l = argvars[0].vval.v_list;
18372 if (l == NULL)
18373 return;
18374 li = l->lv_first;
18375 if (li == NULL)
18376 return;
18377 bad = get_tv_string(&li->li_tv);
18378 li = li->li_next;
18379 if (li == NULL)
18380 return;
18381 good = get_tv_string(&li->li_tv);
18382 rettv->vval.v_number = test_edit_score(bad, good);
18383#endif
18384}
18385
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018386#ifdef FEAT_FLOAT
18387/*
18388 * "tan()" function
18389 */
18390 static void
18391f_tan(argvars, rettv)
18392 typval_T *argvars;
18393 typval_T *rettv;
18394{
18395 float_T f;
18396
18397 rettv->v_type = VAR_FLOAT;
18398 if (get_float_arg(argvars, &f) == OK)
18399 rettv->vval.v_float = tan(f);
18400 else
18401 rettv->vval.v_float = 0.0;
18402}
18403
18404/*
18405 * "tanh()" function
18406 */
18407 static void
18408f_tanh(argvars, rettv)
18409 typval_T *argvars;
18410 typval_T *rettv;
18411{
18412 float_T f;
18413
18414 rettv->v_type = VAR_FLOAT;
18415 if (get_float_arg(argvars, &f) == OK)
18416 rettv->vval.v_float = tanh(f);
18417 else
18418 rettv->vval.v_float = 0.0;
18419}
18420#endif
18421
Bram Moolenaard52d9742005-08-21 22:20:28 +000018422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 * "tolower(string)" function
18424 */
18425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018426f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018427 typval_T *argvars;
18428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429{
18430 char_u *p;
18431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 p = vim_strsave(get_tv_string(&argvars[0]));
18433 rettv->v_type = VAR_STRING;
18434 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435
18436 if (p != NULL)
18437 while (*p != NUL)
18438 {
18439#ifdef FEAT_MBYTE
18440 int l;
18441
18442 if (enc_utf8)
18443 {
18444 int c, lc;
18445
18446 c = utf_ptr2char(p);
18447 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018448 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449 /* TODO: reallocate string when byte count changes. */
18450 if (utf_char2len(lc) == l)
18451 utf_char2bytes(lc, p);
18452 p += l;
18453 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018454 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 p += l; /* skip multi-byte character */
18456 else
18457#endif
18458 {
18459 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18460 ++p;
18461 }
18462 }
18463}
18464
18465/*
18466 * "toupper(string)" function
18467 */
18468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018469f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018470 typval_T *argvars;
18471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018474 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475}
18476
18477/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018478 * "tr(string, fromstr, tostr)" function
18479 */
18480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018481f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018482 typval_T *argvars;
18483 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018484{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018485 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018486 char_u *fromstr;
18487 char_u *tostr;
18488 char_u *p;
18489#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018490 int inlen;
18491 int fromlen;
18492 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018493 int idx;
18494 char_u *cpstr;
18495 int cplen;
18496 int first = TRUE;
18497#endif
18498 char_u buf[NUMBUFLEN];
18499 char_u buf2[NUMBUFLEN];
18500 garray_T ga;
18501
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018502 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018503 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18504 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018505
18506 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018507 rettv->v_type = VAR_STRING;
18508 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018509 if (fromstr == NULL || tostr == NULL)
18510 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018511 ga_init2(&ga, (int)sizeof(char), 80);
18512
18513#ifdef FEAT_MBYTE
18514 if (!has_mbyte)
18515#endif
18516 /* not multi-byte: fromstr and tostr must be the same length */
18517 if (STRLEN(fromstr) != STRLEN(tostr))
18518 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018519#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018520error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018521#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018522 EMSG2(_(e_invarg2), fromstr);
18523 ga_clear(&ga);
18524 return;
18525 }
18526
18527 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018528 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018529 {
18530#ifdef FEAT_MBYTE
18531 if (has_mbyte)
18532 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018533 inlen = (*mb_ptr2len)(in_str);
18534 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018535 cplen = inlen;
18536 idx = 0;
18537 for (p = fromstr; *p != NUL; p += fromlen)
18538 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018539 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018540 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018541 {
18542 for (p = tostr; *p != NUL; p += tolen)
18543 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018544 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545 if (idx-- == 0)
18546 {
18547 cplen = tolen;
18548 cpstr = p;
18549 break;
18550 }
18551 }
18552 if (*p == NUL) /* tostr is shorter than fromstr */
18553 goto error;
18554 break;
18555 }
18556 ++idx;
18557 }
18558
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018559 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018560 {
18561 /* Check that fromstr and tostr have the same number of
18562 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018563 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018564 first = FALSE;
18565 for (p = tostr; *p != NUL; p += tolen)
18566 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018567 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018568 --idx;
18569 }
18570 if (idx != 0)
18571 goto error;
18572 }
18573
18574 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018575 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018576 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018577
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018578 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018579 }
18580 else
18581#endif
18582 {
18583 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018584 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018585 if (p != NULL)
18586 ga_append(&ga, tostr[p - fromstr]);
18587 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018588 ga_append(&ga, *in_str);
18589 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018590 }
18591 }
18592
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018593 /* add a terminating NUL */
18594 ga_grow(&ga, 1);
18595 ga_append(&ga, NUL);
18596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018598}
18599
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018600#ifdef FEAT_FLOAT
18601/*
18602 * "trunc({float})" function
18603 */
18604 static void
18605f_trunc(argvars, rettv)
18606 typval_T *argvars;
18607 typval_T *rettv;
18608{
18609 float_T f;
18610
18611 rettv->v_type = VAR_FLOAT;
18612 if (get_float_arg(argvars, &f) == OK)
18613 /* trunc() is not in C90, use floor() or ceil() instead. */
18614 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18615 else
18616 rettv->vval.v_float = 0.0;
18617}
18618#endif
18619
Bram Moolenaar8299df92004-07-10 09:47:34 +000018620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 * "type(expr)" function
18622 */
18623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 typval_T *argvars;
18626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018628 int n;
18629
18630 switch (argvars[0].v_type)
18631 {
18632 case VAR_NUMBER: n = 0; break;
18633 case VAR_STRING: n = 1; break;
18634 case VAR_FUNC: n = 2; break;
18635 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018636 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018637#ifdef FEAT_FLOAT
18638 case VAR_FLOAT: n = 5; break;
18639#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018640 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18641 }
18642 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643}
18644
18645/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018646 * "undofile(name)" function
18647 */
18648 static void
18649f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018650 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018651 typval_T *rettv;
18652{
18653 rettv->v_type = VAR_STRING;
18654#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018655 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018656 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018657
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018658 if (*fname == NUL)
18659 {
18660 /* If there is no file name there will be no undo file. */
18661 rettv->vval.v_string = NULL;
18662 }
18663 else
18664 {
18665 char_u *ffname = FullName_save(fname, FALSE);
18666
18667 if (ffname != NULL)
18668 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18669 vim_free(ffname);
18670 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018671 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018672#else
18673 rettv->vval.v_string = NULL;
18674#endif
18675}
18676
18677/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018678 * "undotree()" function
18679 */
18680 static void
18681f_undotree(argvars, rettv)
18682 typval_T *argvars UNUSED;
18683 typval_T *rettv;
18684{
18685 if (rettv_dict_alloc(rettv) == OK)
18686 {
18687 dict_T *dict = rettv->vval.v_dict;
18688 list_T *list;
18689
Bram Moolenaar730cde92010-06-27 05:18:54 +020018690 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018691 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018692 dict_add_nr_str(dict, "save_last",
18693 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018694 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18695 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018696 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018697
18698 list = list_alloc();
18699 if (list != NULL)
18700 {
18701 u_eval_tree(curbuf->b_u_oldhead, list);
18702 dict_add_list(dict, "entries", list);
18703 }
18704 }
18705}
18706
18707/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018708 * "values(dict)" function
18709 */
18710 static void
18711f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018712 typval_T *argvars;
18713 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018714{
18715 dict_list(argvars, rettv, 1);
18716}
18717
18718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 * "virtcol(string)" function
18720 */
18721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018723 typval_T *argvars;
18724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725{
18726 colnr_T vcol = 0;
18727 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018728 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018730 fp = var2fpos(&argvars[0], FALSE, &fnum);
18731 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18732 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 {
18734 getvvcol(curwin, fp, NULL, NULL, &vcol);
18735 ++vcol;
18736 }
18737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018738 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739}
18740
18741/*
18742 * "visualmode()" function
18743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018746 typval_T *argvars UNUSED;
18747 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748{
18749#ifdef FEAT_VISUAL
18750 char_u str[2];
18751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 str[0] = curbuf->b_visual_mode_eval;
18754 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756
18757 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018758 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760#endif
18761}
18762
18763/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018764 * "wildmenumode()" function
18765 */
18766 static void
18767f_wildmenumode(argvars, rettv)
18768 typval_T *argvars UNUSED;
18769 typval_T *rettv UNUSED;
18770{
18771#ifdef FEAT_WILDMENU
18772 if (wild_menu_showing)
18773 rettv->vval.v_number = 1;
18774#endif
18775}
18776
18777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 * "winbufnr(nr)" function
18779 */
18780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 typval_T *argvars;
18783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784{
18785 win_T *wp;
18786
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018787 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018791 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
18794/*
18795 * "wincol()" function
18796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801{
18802 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804}
18805
18806/*
18807 * "winheight(nr)" function
18808 */
18809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018810f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018811 typval_T *argvars;
18812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813{
18814 win_T *wp;
18815
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018816 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018818 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821}
18822
18823/*
18824 * "winline()" function
18825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830{
18831 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018832 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833}
18834
18835/*
18836 * "winnr()" function
18837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018840 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842{
18843 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018844
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018846 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849}
18850
18851/*
18852 * "winrestcmd()" function
18853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018855f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858{
18859#ifdef FEAT_WINDOWS
18860 win_T *wp;
18861 int winnr = 1;
18862 garray_T ga;
18863 char_u buf[50];
18864
18865 ga_init2(&ga, (int)sizeof(char), 70);
18866 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18867 {
18868 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18869 ga_concat(&ga, buf);
18870# ifdef FEAT_VERTSPLIT
18871 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18872 ga_concat(&ga, buf);
18873# endif
18874 ++winnr;
18875 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018876 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018878 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883}
18884
18885/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018886 * "winrestview()" function
18887 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018888 static void
18889f_winrestview(argvars, rettv)
18890 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018891 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018892{
18893 dict_T *dict;
18894
18895 if (argvars[0].v_type != VAR_DICT
18896 || (dict = argvars[0].vval.v_dict) == NULL)
18897 EMSG(_(e_invarg));
18898 else
18899 {
18900 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18901 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18902#ifdef FEAT_VIRTUALEDIT
18903 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18904#endif
18905 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018906 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018907
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018908 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018909#ifdef FEAT_DIFF
18910 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18911#endif
18912 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18913 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18914
18915 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018916 win_new_height(curwin, curwin->w_height);
18917# ifdef FEAT_VERTSPLIT
18918 win_new_width(curwin, W_WIDTH(curwin));
18919# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018920 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018921
18922 if (curwin->w_topline == 0)
18923 curwin->w_topline = 1;
18924 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18925 curwin->w_topline = curbuf->b_ml.ml_line_count;
18926#ifdef FEAT_DIFF
18927 check_topfill(curwin, TRUE);
18928#endif
18929 }
18930}
18931
18932/*
18933 * "winsaveview()" function
18934 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018935 static void
18936f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018937 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018938 typval_T *rettv;
18939{
18940 dict_T *dict;
18941
Bram Moolenaara800b422010-06-27 01:15:55 +020018942 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018943 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018944 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018945
18946 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18947 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18948#ifdef FEAT_VIRTUALEDIT
18949 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18950#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018951 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018952 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18953
18954 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18955#ifdef FEAT_DIFF
18956 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18957#endif
18958 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18959 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18960}
18961
18962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 * "winwidth(nr)" function
18964 */
18965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018966f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018967 typval_T *argvars;
18968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969{
18970 win_T *wp;
18971
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018972 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 else
18976#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018977 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018979 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980#endif
18981}
18982
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018984 * "writefile()" function
18985 */
18986 static void
18987f_writefile(argvars, rettv)
18988 typval_T *argvars;
18989 typval_T *rettv;
18990{
18991 int binary = FALSE;
18992 char_u *fname;
18993 FILE *fd;
18994 listitem_T *li;
18995 char_u *s;
18996 int ret = 0;
18997 int c;
18998
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018999 if (check_restricted() || check_secure())
19000 return;
19001
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019002 if (argvars[0].v_type != VAR_LIST)
19003 {
19004 EMSG2(_(e_listarg), "writefile()");
19005 return;
19006 }
19007 if (argvars[0].vval.v_list == NULL)
19008 return;
19009
19010 if (argvars[2].v_type != VAR_UNKNOWN
19011 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19012 binary = TRUE;
19013
19014 /* Always open the file in binary mode, library functions have a mind of
19015 * their own about CR-LF conversion. */
19016 fname = get_tv_string(&argvars[1]);
19017 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19018 {
19019 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19020 ret = -1;
19021 }
19022 else
19023 {
19024 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19025 li = li->li_next)
19026 {
19027 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19028 {
19029 if (*s == '\n')
19030 c = putc(NUL, fd);
19031 else
19032 c = putc(*s, fd);
19033 if (c == EOF)
19034 {
19035 ret = -1;
19036 break;
19037 }
19038 }
19039 if (!binary || li->li_next != NULL)
19040 if (putc('\n', fd) == EOF)
19041 {
19042 ret = -1;
19043 break;
19044 }
19045 if (ret < 0)
19046 {
19047 EMSG(_(e_write));
19048 break;
19049 }
19050 }
19051 fclose(fd);
19052 }
19053
19054 rettv->vval.v_number = ret;
19055}
19056
19057/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019058 * "xor(expr, expr)" function
19059 */
19060 static void
19061f_xor(argvars, rettv)
19062 typval_T *argvars;
19063 typval_T *rettv;
19064{
19065 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19066 ^ get_tv_number_chk(&argvars[1], NULL);
19067}
19068
19069
19070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019072 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 */
19074 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019075var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019076 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019077 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019078 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019080 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019082 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
Bram Moolenaara5525202006-03-02 22:52:09 +000019084 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019085 if (varp->v_type == VAR_LIST)
19086 {
19087 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019088 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019089 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019090 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019091
19092 l = varp->vval.v_list;
19093 if (l == NULL)
19094 return NULL;
19095
19096 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019097 pos.lnum = list_find_nr(l, 0L, &error);
19098 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019099 return NULL; /* invalid line number */
19100
19101 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019102 pos.col = list_find_nr(l, 1L, &error);
19103 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019104 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019105 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019106
19107 /* We accept "$" for the column number: last column. */
19108 li = list_find(l, 1L);
19109 if (li != NULL && li->li_tv.v_type == VAR_STRING
19110 && li->li_tv.vval.v_string != NULL
19111 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19112 pos.col = len + 1;
19113
Bram Moolenaara5525202006-03-02 22:52:09 +000019114 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019115 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019116 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019117 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019118
Bram Moolenaara5525202006-03-02 22:52:09 +000019119#ifdef FEAT_VIRTUALEDIT
19120 /* Get the virtual offset. Defaults to zero. */
19121 pos.coladd = list_find_nr(l, 2L, &error);
19122 if (error)
19123 pos.coladd = 0;
19124#endif
19125
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019126 return &pos;
19127 }
19128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019129 name = get_tv_string_chk(varp);
19130 if (name == NULL)
19131 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019132 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019134#ifdef FEAT_VISUAL
19135 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19136 {
19137 if (VIsual_active)
19138 return &VIsual;
19139 return &curwin->w_cursor;
19140 }
19141#endif
19142 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019144 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19146 return NULL;
19147 return pp;
19148 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019149
19150#ifdef FEAT_VIRTUALEDIT
19151 pos.coladd = 0;
19152#endif
19153
Bram Moolenaar477933c2007-07-17 14:32:23 +000019154 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019155 {
19156 pos.col = 0;
19157 if (name[1] == '0') /* "w0": first visible line */
19158 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019159 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019160 pos.lnum = curwin->w_topline;
19161 return &pos;
19162 }
19163 else if (name[1] == '$') /* "w$": last visible line */
19164 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019165 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019166 pos.lnum = curwin->w_botline - 1;
19167 return &pos;
19168 }
19169 }
19170 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019172 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 {
19174 pos.lnum = curbuf->b_ml.ml_line_count;
19175 pos.col = 0;
19176 }
19177 else
19178 {
19179 pos.lnum = curwin->w_cursor.lnum;
19180 pos.col = (colnr_T)STRLEN(ml_get_curline());
19181 }
19182 return &pos;
19183 }
19184 return NULL;
19185}
19186
19187/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019188 * Convert list in "arg" into a position and optional file number.
19189 * When "fnump" is NULL there is no file number, only 3 items.
19190 * Note that the column is passed on as-is, the caller may want to decrement
19191 * it to use 1 for the first column.
19192 * Return FAIL when conversion is not possible, doesn't check the position for
19193 * validity.
19194 */
19195 static int
19196list2fpos(arg, posp, fnump)
19197 typval_T *arg;
19198 pos_T *posp;
19199 int *fnump;
19200{
19201 list_T *l = arg->vval.v_list;
19202 long i = 0;
19203 long n;
19204
Bram Moolenaarbde35262006-07-23 20:12:24 +000019205 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19206 * when "fnump" isn't NULL and "coladd" is optional. */
19207 if (arg->v_type != VAR_LIST
19208 || l == NULL
19209 || l->lv_len < (fnump == NULL ? 2 : 3)
19210 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019211 return FAIL;
19212
19213 if (fnump != NULL)
19214 {
19215 n = list_find_nr(l, i++, NULL); /* fnum */
19216 if (n < 0)
19217 return FAIL;
19218 if (n == 0)
19219 n = curbuf->b_fnum; /* current buffer */
19220 *fnump = n;
19221 }
19222
19223 n = list_find_nr(l, i++, NULL); /* lnum */
19224 if (n < 0)
19225 return FAIL;
19226 posp->lnum = n;
19227
19228 n = list_find_nr(l, i++, NULL); /* col */
19229 if (n < 0)
19230 return FAIL;
19231 posp->col = n;
19232
19233#ifdef FEAT_VIRTUALEDIT
19234 n = list_find_nr(l, i, NULL);
19235 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019236 posp->coladd = 0;
19237 else
19238 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019239#endif
19240
19241 return OK;
19242}
19243
19244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 * Get the length of an environment variable name.
19246 * Advance "arg" to the first character after the name.
19247 * Return 0 for error.
19248 */
19249 static int
19250get_env_len(arg)
19251 char_u **arg;
19252{
19253 char_u *p;
19254 int len;
19255
19256 for (p = *arg; vim_isIDc(*p); ++p)
19257 ;
19258 if (p == *arg) /* no name found */
19259 return 0;
19260
19261 len = (int)(p - *arg);
19262 *arg = p;
19263 return len;
19264}
19265
19266/*
19267 * Get the length of the name of a function or internal variable.
19268 * "arg" is advanced to the first non-white character after the name.
19269 * Return 0 if something is wrong.
19270 */
19271 static int
19272get_id_len(arg)
19273 char_u **arg;
19274{
19275 char_u *p;
19276 int len;
19277
19278 /* Find the end of the name. */
19279 for (p = *arg; eval_isnamec(*p); ++p)
19280 ;
19281 if (p == *arg) /* no name found */
19282 return 0;
19283
19284 len = (int)(p - *arg);
19285 *arg = skipwhite(p);
19286
19287 return len;
19288}
19289
19290/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019291 * Get the length of the name of a variable or function.
19292 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019294 * Return -1 if curly braces expansion failed.
19295 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 * If the name contains 'magic' {}'s, expand them and return the
19297 * expanded name in an allocated string via 'alias' - caller must free.
19298 */
19299 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019300get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 char_u **arg;
19302 char_u **alias;
19303 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019304 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305{
19306 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 char_u *p;
19308 char_u *expr_start;
19309 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310
19311 *alias = NULL; /* default to no alias */
19312
19313 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19314 && (*arg)[2] == (int)KE_SNR)
19315 {
19316 /* hard coded <SNR>, already translated */
19317 *arg += 3;
19318 return get_id_len(arg) + 3;
19319 }
19320 len = eval_fname_script(*arg);
19321 if (len > 0)
19322 {
19323 /* literal "<SID>", "s:" or "<SNR>" */
19324 *arg += len;
19325 }
19326
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019330 p = find_name_end(*arg, &expr_start, &expr_end,
19331 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 if (expr_start != NULL)
19333 {
19334 char_u *temp_string;
19335
19336 if (!evaluate)
19337 {
19338 len += (int)(p - *arg);
19339 *arg = skipwhite(p);
19340 return len;
19341 }
19342
19343 /*
19344 * Include any <SID> etc in the expanded string:
19345 * Thus the -len here.
19346 */
19347 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19348 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019349 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 *alias = temp_string;
19351 *arg = skipwhite(p);
19352 return (int)STRLEN(temp_string);
19353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354
19355 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019356 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 EMSG2(_(e_invexpr2), *arg);
19358
19359 return len;
19360}
19361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019362/*
19363 * Find the end of a variable or function name, taking care of magic braces.
19364 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19365 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019366 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019367 * Return a pointer to just after the name. Equal to "arg" if there is no
19368 * valid name.
19369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019371find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 char_u *arg;
19373 char_u **expr_start;
19374 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019375 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 int mb_nest = 0;
19378 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 char_u *p;
19380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383 *expr_start = NULL;
19384 *expr_end = NULL;
19385 }
19386
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019387 /* Quick check for valid starting character. */
19388 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19389 return arg;
19390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019391 for (p = arg; *p != NUL
19392 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019393 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019394 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019395 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019396 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019397 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019398 if (*p == '\'')
19399 {
19400 /* skip over 'string' to avoid counting [ and ] inside it. */
19401 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19402 ;
19403 if (*p == NUL)
19404 break;
19405 }
19406 else if (*p == '"')
19407 {
19408 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19409 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19410 if (*p == '\\' && p[1] != NUL)
19411 ++p;
19412 if (*p == NUL)
19413 break;
19414 }
19415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019416 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418 if (*p == '[')
19419 ++br_nest;
19420 else if (*p == ']')
19421 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426 if (*p == '{')
19427 {
19428 mb_nest++;
19429 if (expr_start != NULL && *expr_start == NULL)
19430 *expr_start = p;
19431 }
19432 else if (*p == '}')
19433 {
19434 mb_nest--;
19435 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19436 *expr_end = p;
19437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 }
19440
19441 return p;
19442}
19443
19444/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019445 * Expands out the 'magic' {}'s in a variable/function name.
19446 * Note that this can call itself recursively, to deal with
19447 * constructs like foo{bar}{baz}{bam}
19448 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19449 * "in_start" ^
19450 * "expr_start" ^
19451 * "expr_end" ^
19452 * "in_end" ^
19453 *
19454 * Returns a new allocated string, which the caller must free.
19455 * Returns NULL for failure.
19456 */
19457 static char_u *
19458make_expanded_name(in_start, expr_start, expr_end, in_end)
19459 char_u *in_start;
19460 char_u *expr_start;
19461 char_u *expr_end;
19462 char_u *in_end;
19463{
19464 char_u c1;
19465 char_u *retval = NULL;
19466 char_u *temp_result;
19467 char_u *nextcmd = NULL;
19468
19469 if (expr_end == NULL || in_end == NULL)
19470 return NULL;
19471 *expr_start = NUL;
19472 *expr_end = NUL;
19473 c1 = *in_end;
19474 *in_end = NUL;
19475
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019476 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019477 if (temp_result != NULL && nextcmd == NULL)
19478 {
19479 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19480 + (in_end - expr_end) + 1));
19481 if (retval != NULL)
19482 {
19483 STRCPY(retval, in_start);
19484 STRCAT(retval, temp_result);
19485 STRCAT(retval, expr_end + 1);
19486 }
19487 }
19488 vim_free(temp_result);
19489
19490 *in_end = c1; /* put char back for error messages */
19491 *expr_start = '{';
19492 *expr_end = '}';
19493
19494 if (retval != NULL)
19495 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019496 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019497 if (expr_start != NULL)
19498 {
19499 /* Further expansion! */
19500 temp_result = make_expanded_name(retval, expr_start,
19501 expr_end, temp_result);
19502 vim_free(retval);
19503 retval = temp_result;
19504 }
19505 }
19506
19507 return retval;
19508}
19509
19510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019512 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513 */
19514 static int
19515eval_isnamec(c)
19516 int c;
19517{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019518 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19519}
19520
19521/*
19522 * Return TRUE if character "c" can be used as the first character in a
19523 * variable or function name (excluding '{' and '}').
19524 */
19525 static int
19526eval_isnamec1(c)
19527 int c;
19528{
19529 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530}
19531
19532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 * Set number v: variable to "val".
19534 */
19535 void
19536set_vim_var_nr(idx, val)
19537 int idx;
19538 long val;
19539{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019540 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541}
19542
19543/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019544 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 */
19546 long
19547get_vim_var_nr(idx)
19548 int idx;
19549{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019550 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551}
19552
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019553/*
19554 * Get string v: variable value. Uses a static buffer, can only be used once.
19555 */
19556 char_u *
19557get_vim_var_str(idx)
19558 int idx;
19559{
19560 return get_tv_string(&vimvars[idx].vv_tv);
19561}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019562
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019564 * Get List v: variable value. Caller must take care of reference count when
19565 * needed.
19566 */
19567 list_T *
19568get_vim_var_list(idx)
19569 int idx;
19570{
19571 return vimvars[idx].vv_list;
19572}
19573
19574/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019575 * Set v:char to character "c".
19576 */
19577 void
19578set_vim_var_char(c)
19579 int c;
19580{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019581 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019582
19583#ifdef FEAT_MBYTE
19584 if (has_mbyte)
19585 buf[(*mb_char2bytes)(c, buf)] = NUL;
19586 else
19587#endif
19588 {
19589 buf[0] = c;
19590 buf[1] = NUL;
19591 }
19592 set_vim_var_string(VV_CHAR, buf, -1);
19593}
19594
19595/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019596 * Set v:count to "count" and v:count1 to "count1".
19597 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 */
19599 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019600set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 long count;
19602 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019603 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019605 if (set_prevcount)
19606 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019607 vimvars[VV_COUNT].vv_nr = count;
19608 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609}
19610
19611/*
19612 * Set string v: variable to a copy of "val".
19613 */
19614 void
19615set_vim_var_string(idx, val, len)
19616 int idx;
19617 char_u *val;
19618 int len; /* length of "val" to use or -1 (whole string) */
19619{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019620 /* Need to do this (at least) once, since we can't initialize a union.
19621 * Will always be invoked when "v:progname" is set. */
19622 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19623
Bram Moolenaare9a41262005-01-15 22:18:47 +000019624 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019626 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019628 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019630 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019631}
19632
19633/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019634 * Set List v: variable to "val".
19635 */
19636 void
19637set_vim_var_list(idx, val)
19638 int idx;
19639 list_T *val;
19640{
19641 list_unref(vimvars[idx].vv_list);
19642 vimvars[idx].vv_list = val;
19643 if (val != NULL)
19644 ++val->lv_refcount;
19645}
19646
19647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648 * Set v:register if needed.
19649 */
19650 void
19651set_reg_var(c)
19652 int c;
19653{
19654 char_u regname;
19655
19656 if (c == 0 || c == ' ')
19657 regname = '"';
19658 else
19659 regname = c;
19660 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019661 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 set_vim_var_string(VV_REG, &regname, 1);
19663}
19664
19665/*
19666 * Get or set v:exception. If "oldval" == NULL, return the current value.
19667 * Otherwise, restore the value to "oldval" and return NULL.
19668 * Must always be called in pairs to save and restore v:exception! Does not
19669 * take care of memory allocations.
19670 */
19671 char_u *
19672v_exception(oldval)
19673 char_u *oldval;
19674{
19675 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019676 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677
Bram Moolenaare9a41262005-01-15 22:18:47 +000019678 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 return NULL;
19680}
19681
19682/*
19683 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19684 * Otherwise, restore the value to "oldval" and return NULL.
19685 * Must always be called in pairs to save and restore v:throwpoint! Does not
19686 * take care of memory allocations.
19687 */
19688 char_u *
19689v_throwpoint(oldval)
19690 char_u *oldval;
19691{
19692 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019693 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694
Bram Moolenaare9a41262005-01-15 22:18:47 +000019695 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 return NULL;
19697}
19698
19699#if defined(FEAT_AUTOCMD) || defined(PROTO)
19700/*
19701 * Set v:cmdarg.
19702 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19703 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19704 * Must always be called in pairs!
19705 */
19706 char_u *
19707set_cmdarg(eap, oldarg)
19708 exarg_T *eap;
19709 char_u *oldarg;
19710{
19711 char_u *oldval;
19712 char_u *newval;
19713 unsigned len;
19714
Bram Moolenaare9a41262005-01-15 22:18:47 +000019715 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019716 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019718 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019719 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019720 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 }
19722
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019723 if (eap->force_bin == FORCE_BIN)
19724 len = 6;
19725 else if (eap->force_bin == FORCE_NOBIN)
19726 len = 8;
19727 else
19728 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019729
19730 if (eap->read_edit)
19731 len += 7;
19732
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019733 if (eap->force_ff != 0)
19734 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19735# ifdef FEAT_MBYTE
19736 if (eap->force_enc != 0)
19737 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019738 if (eap->bad_char != 0)
19739 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019740# endif
19741
19742 newval = alloc(len + 1);
19743 if (newval == NULL)
19744 return NULL;
19745
19746 if (eap->force_bin == FORCE_BIN)
19747 sprintf((char *)newval, " ++bin");
19748 else if (eap->force_bin == FORCE_NOBIN)
19749 sprintf((char *)newval, " ++nobin");
19750 else
19751 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019752
19753 if (eap->read_edit)
19754 STRCAT(newval, " ++edit");
19755
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019756 if (eap->force_ff != 0)
19757 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19758 eap->cmd + eap->force_ff);
19759# ifdef FEAT_MBYTE
19760 if (eap->force_enc != 0)
19761 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19762 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019763 if (eap->bad_char == BAD_KEEP)
19764 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19765 else if (eap->bad_char == BAD_DROP)
19766 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19767 else if (eap->bad_char != 0)
19768 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019769# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019770 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019771 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772}
19773#endif
19774
19775/*
19776 * Get the value of internal variable "name".
19777 * Return OK or FAIL.
19778 */
19779 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019780get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781 char_u *name;
19782 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019784 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019785 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786{
19787 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 typval_T *tv = NULL;
19789 typval_T atv;
19790 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
19793 /* truncate the name, so that we can use strcmp() */
19794 cc = name[len];
19795 name[len] = NUL;
19796
19797 /*
19798 * Check for "b:changedtick".
19799 */
19800 if (STRCMP(name, "b:changedtick") == 0)
19801 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019802 atv.v_type = VAR_NUMBER;
19803 atv.vval.v_number = curbuf->b_changedtick;
19804 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805 }
19806
19807 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 * Check for user-defined variables.
19809 */
19810 else
19811 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019812 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 }
19816
Bram Moolenaare9a41262005-01-15 22:18:47 +000019817 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019819 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 ret = FAIL;
19822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019823 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019824 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825
19826 name[len] = cc;
19827
19828 return ret;
19829}
19830
19831/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019832 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19833 * Also handle function call with Funcref variable: func(expr)
19834 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19835 */
19836 static int
19837handle_subscript(arg, rettv, evaluate, verbose)
19838 char_u **arg;
19839 typval_T *rettv;
19840 int evaluate; /* do more than finding the end */
19841 int verbose; /* give error messages */
19842{
19843 int ret = OK;
19844 dict_T *selfdict = NULL;
19845 char_u *s;
19846 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019847 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019848
19849 while (ret == OK
19850 && (**arg == '['
19851 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019852 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019853 && !vim_iswhite(*(*arg - 1)))
19854 {
19855 if (**arg == '(')
19856 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019857 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019858 if (evaluate)
19859 {
19860 functv = *rettv;
19861 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019862
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019863 /* Invoke the function. Recursive! */
19864 s = functv.vval.v_string;
19865 }
19866 else
19867 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019868 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019869 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19870 &len, evaluate, selfdict);
19871
19872 /* Clear the funcref afterwards, so that deleting it while
19873 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019874 if (evaluate)
19875 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019876
19877 /* Stop the expression evaluation when immediately aborting on
19878 * error, or when an interrupt occurred or an exception was thrown
19879 * but not caught. */
19880 if (aborting())
19881 {
19882 if (ret == OK)
19883 clear_tv(rettv);
19884 ret = FAIL;
19885 }
19886 dict_unref(selfdict);
19887 selfdict = NULL;
19888 }
19889 else /* **arg == '[' || **arg == '.' */
19890 {
19891 dict_unref(selfdict);
19892 if (rettv->v_type == VAR_DICT)
19893 {
19894 selfdict = rettv->vval.v_dict;
19895 if (selfdict != NULL)
19896 ++selfdict->dv_refcount;
19897 }
19898 else
19899 selfdict = NULL;
19900 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19901 {
19902 clear_tv(rettv);
19903 ret = FAIL;
19904 }
19905 }
19906 }
19907 dict_unref(selfdict);
19908 return ret;
19909}
19910
19911/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019912 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019913 * value).
19914 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019915 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019916alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019917{
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919}
19920
19921/*
19922 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 * The string "s" must have been allocated, it is consumed.
19924 * Return NULL for out of memory, the variable otherwise.
19925 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019926 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019927alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 char_u *s;
19929{
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 rettv = alloc_tv();
19933 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935 rettv->v_type = VAR_STRING;
19936 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 }
19938 else
19939 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941}
19942
19943/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019944 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019946 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949{
19950 if (varp != NULL)
19951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019952 switch (varp->v_type)
19953 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019955 func_unref(varp->vval.v_string);
19956 /*FALLTHROUGH*/
19957 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019958 vim_free(varp->vval.v_string);
19959 break;
19960 case VAR_LIST:
19961 list_unref(varp->vval.v_list);
19962 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019963 case VAR_DICT:
19964 dict_unref(varp->vval.v_dict);
19965 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019966 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019967#ifdef FEAT_FLOAT
19968 case VAR_FLOAT:
19969#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019970 case VAR_UNKNOWN:
19971 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019972 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019973 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019974 break;
19975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 vim_free(varp);
19977 }
19978}
19979
19980/*
19981 * Free the memory for a variable value and set the value to NULL or 0.
19982 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019983 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019985 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986{
19987 if (varp != NULL)
19988 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019989 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019992 func_unref(varp->vval.v_string);
19993 /*FALLTHROUGH*/
19994 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 vim_free(varp->vval.v_string);
19996 varp->vval.v_string = NULL;
19997 break;
19998 case VAR_LIST:
19999 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020000 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020002 case VAR_DICT:
20003 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020004 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020005 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020007 varp->vval.v_number = 0;
20008 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020009#ifdef FEAT_FLOAT
20010 case VAR_FLOAT:
20011 varp->vval.v_float = 0.0;
20012 break;
20013#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020014 case VAR_UNKNOWN:
20015 break;
20016 default:
20017 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020019 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
20021}
20022
20023/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 * Set the value of a variable to NULL without freeing items.
20025 */
20026 static void
20027init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020028 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029{
20030 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032}
20033
20034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 * Get the number value of a variable.
20036 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020037 * For incompatible types, return 0.
20038 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20039 * caller of incompatible types: it sets *denote to TRUE if "denote"
20040 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 */
20042 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020043get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020044 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020046 int error = FALSE;
20047
20048 return get_tv_number_chk(varp, &error); /* return 0L on error */
20049}
20050
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020051 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020052get_tv_number_chk(varp, denote)
20053 typval_T *varp;
20054 int *denote;
20055{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020056 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020058 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020060 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020061 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020062#ifdef FEAT_FLOAT
20063 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020064 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020065 break;
20066#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020067 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020068 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020069 break;
20070 case VAR_STRING:
20071 if (varp->vval.v_string != NULL)
20072 vim_str2nr(varp->vval.v_string, NULL, NULL,
20073 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020074 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020075 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020076 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020077 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020079 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020080 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020081 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020082 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020083 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020085 if (denote == NULL) /* useful for values that must be unsigned */
20086 n = -1;
20087 else
20088 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090}
20091
20092/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020093 * Get the lnum from the first argument.
20094 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020095 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 */
20097 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100{
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 linenr_T lnum;
20103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020104 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 if (lnum == 0) /* no valid number, try using line() */
20106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107 rettv.v_type = VAR_NUMBER;
20108 f_line(argvars, &rettv);
20109 lnum = rettv.vval.v_number;
20110 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 }
20112 return lnum;
20113}
20114
20115/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020116 * Get the lnum from the first argument.
20117 * Also accepts "$", then "buf" is used.
20118 * Returns 0 on error.
20119 */
20120 static linenr_T
20121get_tv_lnum_buf(argvars, buf)
20122 typval_T *argvars;
20123 buf_T *buf;
20124{
20125 if (argvars[0].v_type == VAR_STRING
20126 && argvars[0].vval.v_string != NULL
20127 && argvars[0].vval.v_string[0] == '$'
20128 && buf != NULL)
20129 return buf->b_ml.ml_line_count;
20130 return get_tv_number_chk(&argvars[0], NULL);
20131}
20132
20133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 * Get the string value of a variable.
20135 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020136 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20137 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 * If the String variable has never been set, return an empty string.
20139 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020140 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20141 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 */
20143 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020144get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146{
20147 static char_u mybuf[NUMBUFLEN];
20148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150}
20151
20152 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020155 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020157 char_u *res = get_tv_string_buf_chk(varp, buf);
20158
20159 return res != NULL ? res : (char_u *)"";
20160}
20161
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020162 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020163get_tv_string_chk(varp)
20164 typval_T *varp;
20165{
20166 static char_u mybuf[NUMBUFLEN];
20167
20168 return get_tv_string_buf_chk(varp, mybuf);
20169}
20170
20171 static char_u *
20172get_tv_string_buf_chk(varp, buf)
20173 typval_T *varp;
20174 char_u *buf;
20175{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020176 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020178 case VAR_NUMBER:
20179 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20180 return buf;
20181 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020182 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 break;
20184 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020185 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020186 break;
20187 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020188 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020189 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020190#ifdef FEAT_FLOAT
20191 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020192 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020193 break;
20194#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020195 case VAR_STRING:
20196 if (varp->vval.v_string != NULL)
20197 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020198 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020199 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020201 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020203 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204}
20205
20206/*
20207 * Find variable "name" in the list of variables.
20208 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020209 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020210 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020213 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020214find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020217 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221
Bram Moolenaara7043832005-01-21 11:56:39 +000020222 ht = find_var_ht(name, &varname);
20223 if (htp != NULL)
20224 *htp = ht;
20225 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020227 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020231 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020232 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020233 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020235find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020237 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020238 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020239 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020240{
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 hashitem_T *hi;
20242
20243 if (*varname == NUL)
20244 {
20245 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020246 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020248 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 case 'g': return &globvars_var;
20250 case 'v': return &vimvars_var;
20251 case 'b': return &curbuf->b_bufvar;
20252 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020253#ifdef FEAT_WINDOWS
20254 case 't': return &curtab->tp_winvar;
20255#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020256 case 'l': return current_funccal == NULL
20257 ? NULL : &current_funccal->l_vars_var;
20258 case 'a': return current_funccal == NULL
20259 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020260 }
20261 return NULL;
20262 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020263
20264 hi = hash_find(ht, varname);
20265 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020266 {
20267 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020268 * worked find the variable again. Don't auto-load a script if it was
20269 * loaded already, otherwise it would be loaded every time when
20270 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020271 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020272 {
20273 /* Note: script_autoload() may make "hi" invalid. It must either
20274 * be obtained again or not used. */
20275 if (!script_autoload(varname, FALSE) || aborting())
20276 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020277 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020278 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020279 if (HASHITEM_EMPTY(hi))
20280 return NULL;
20281 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020283}
20284
20285/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020286 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020287 * Set "varname" to the start of name without ':'.
20288 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020290find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 char_u *name;
20292 char_u **varname;
20293{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020294 hashitem_T *hi;
20295
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 if (name[1] != ':')
20297 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020298 /* The name must not start with a colon or #. */
20299 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 return NULL;
20301 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020302
20303 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020304 hi = hash_find(&compat_hashtab, name);
20305 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020306 return &compat_hashtab;
20307
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 return &globvarht; /* global variable */
20310 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 }
20312 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020313 if (*name == 'g') /* global variable */
20314 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020315 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20316 */
20317 if (vim_strchr(name + 2, ':') != NULL
20318 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020319 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020321 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020323 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020324#ifdef FEAT_WINDOWS
20325 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020326 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020327#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020328 if (*name == 'v') /* v: variable */
20329 return &vimvarht;
20330 if (*name == 'a' && current_funccal != NULL) /* function argument */
20331 return &current_funccal->l_avars.dv_hashtab;
20332 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20333 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 if (*name == 's' /* script variable */
20335 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20336 return &SCRIPT_VARS(current_SID);
20337 return NULL;
20338}
20339
20340/*
20341 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020342 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 * Returns NULL when it doesn't exist.
20344 */
20345 char_u *
20346get_var_value(name)
20347 char_u *name;
20348{
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020351 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 if (v == NULL)
20353 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020354 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355}
20356
20357/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 * sourcing this script and when executing functions defined in the script.
20360 */
20361 void
20362new_script_vars(id)
20363 scid_T id;
20364{
Bram Moolenaara7043832005-01-21 11:56:39 +000020365 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 hashtab_T *ht;
20367 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020368
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20370 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020371 /* Re-allocating ga_data means that an ht_array pointing to
20372 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020373 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020374 for (i = 1; i <= ga_scripts.ga_len; ++i)
20375 {
20376 ht = &SCRIPT_VARS(i);
20377 if (ht->ht_mask == HT_INIT_SIZE - 1)
20378 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020379 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020381 }
20382
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 while (ga_scripts.ga_len < id)
20384 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020385 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020386 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020387 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 }
20390 }
20391}
20392
20393/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20395 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 */
20397 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020398init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 dict_T *dict;
20400 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020401 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402{
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020404 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020405 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020406 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020407 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020408 dict_var->di_tv.vval.v_dict = dict;
20409 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020410 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20412 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413}
20414
20415/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020416 * Unreference a dictionary initialized by init_var_dict().
20417 */
20418 void
20419unref_var_dict(dict)
20420 dict_T *dict;
20421{
20422 /* Now the dict needs to be freed if no one else is using it, go back to
20423 * normal reference counting. */
20424 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20425 dict_unref(dict);
20426}
20427
20428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020430 * Frees all allocated variables and the value they contain.
20431 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 */
20433 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020434vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020435 hashtab_T *ht;
20436{
20437 vars_clear_ext(ht, TRUE);
20438}
20439
20440/*
20441 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20442 */
20443 static void
20444vars_clear_ext(ht, free_val)
20445 hashtab_T *ht;
20446 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447{
Bram Moolenaara7043832005-01-21 11:56:39 +000020448 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 hashitem_T *hi;
20450 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020453 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020454 for (hi = ht->ht_array; todo > 0; ++hi)
20455 {
20456 if (!HASHITEM_EMPTY(hi))
20457 {
20458 --todo;
20459
Bram Moolenaar33570922005-01-25 22:26:29 +000020460 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020461 * ht_array might change then. hash_clear() takes care of it
20462 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020463 v = HI2DI(hi);
20464 if (free_val)
20465 clear_tv(&v->di_tv);
20466 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20467 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020468 }
20469 }
20470 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020471 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472}
20473
Bram Moolenaara7043832005-01-21 11:56:39 +000020474/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020475 * Delete a variable from hashtab "ht" at item "hi".
20476 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020479delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 hashtab_T *ht;
20481 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482{
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020484
20485 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020486 clear_tv(&di->di_tv);
20487 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488}
20489
20490/*
20491 * List the value of one internal variable.
20492 */
20493 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020494list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020495 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020497 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020499 char_u *tofree;
20500 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020501 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020502
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020503 current_copyID += COPYID_INC;
20504 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020506 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020507 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508}
20509
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020511list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 char_u *prefix;
20513 char_u *name;
20514 int type;
20515 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020516 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517{
Bram Moolenaar31859182007-08-14 20:41:13 +000020518 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20519 msg_start();
20520 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 if (name != NULL) /* "a:" vars don't have a name stored */
20522 msg_puts(name);
20523 msg_putchar(' ');
20524 msg_advance(22);
20525 if (type == VAR_NUMBER)
20526 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020527 else if (type == VAR_FUNC)
20528 msg_putchar('*');
20529 else if (type == VAR_LIST)
20530 {
20531 msg_putchar('[');
20532 if (*string == '[')
20533 ++string;
20534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020535 else if (type == VAR_DICT)
20536 {
20537 msg_putchar('{');
20538 if (*string == '{')
20539 ++string;
20540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541 else
20542 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020543
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020545
20546 if (type == VAR_FUNC)
20547 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020548 if (*first)
20549 {
20550 msg_clr_eos();
20551 *first = FALSE;
20552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553}
20554
20555/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020556 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 * If the variable already exists, the value is updated.
20558 * Otherwise the variable is created.
20559 */
20560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020561set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020564 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565{
Bram Moolenaar33570922005-01-25 22:26:29 +000020566 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020570 ht = find_var_ht(name, &varname);
20571 if (ht == NULL || *varname == NUL)
20572 {
20573 EMSG2(_(e_illvar), name);
20574 return;
20575 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020576 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020577
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020578 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20579 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020580
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020584 if (var_check_ro(v->di_flags, name)
20585 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 return;
20587 if (v->di_tv.v_type != tv->v_type
20588 && !((v->di_tv.v_type == VAR_STRING
20589 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020590 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020591 || tv->v_type == VAR_NUMBER))
20592#ifdef FEAT_FLOAT
20593 && !((v->di_tv.v_type == VAR_NUMBER
20594 || v->di_tv.v_type == VAR_FLOAT)
20595 && (tv->v_type == VAR_NUMBER
20596 || tv->v_type == VAR_FLOAT))
20597#endif
20598 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020599 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020600 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020601 return;
20602 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020603
20604 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020605 * Handle setting internal v: variables separately: we don't change
20606 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020607 */
20608 if (ht == &vimvarht)
20609 {
20610 if (v->di_tv.v_type == VAR_STRING)
20611 {
20612 vim_free(v->di_tv.vval.v_string);
20613 if (copy || tv->v_type != VAR_STRING)
20614 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20615 else
20616 {
20617 /* Take over the string to avoid an extra alloc/free. */
20618 v->di_tv.vval.v_string = tv->vval.v_string;
20619 tv->vval.v_string = NULL;
20620 }
20621 }
20622 else if (v->di_tv.v_type != VAR_NUMBER)
20623 EMSG2(_(e_intern2), "set_var()");
20624 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020626 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020627 if (STRCMP(varname, "searchforward") == 0)
20628 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020629#ifdef FEAT_SEARCH_EXTRA
20630 else if (STRCMP(varname, "hlsearch") == 0)
20631 {
20632 no_hlsearch = !v->di_tv.vval.v_number;
20633 redraw_all_later(SOME_VALID);
20634 }
20635#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020636 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020637 return;
20638 }
20639
20640 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 }
20642 else /* add a new variable */
20643 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020644 /* Can't add "v:" variable. */
20645 if (ht == &vimvarht)
20646 {
20647 EMSG2(_(e_illvar), name);
20648 return;
20649 }
20650
Bram Moolenaar92124a32005-06-17 22:03:40 +000020651 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020652 if (!valid_varname(varname))
20653 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020654
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020655 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20656 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020657 if (v == NULL)
20658 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020659 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020660 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020662 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 return;
20664 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020665 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020667
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020668 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020670 else
20671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020672 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020673 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676}
20677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020678/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020679 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020680 * Also give an error message.
20681 */
20682 static int
20683var_check_ro(flags, name)
20684 int flags;
20685 char_u *name;
20686{
20687 if (flags & DI_FLAGS_RO)
20688 {
20689 EMSG2(_(e_readonlyvar), name);
20690 return TRUE;
20691 }
20692 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20693 {
20694 EMSG2(_(e_readonlysbx), name);
20695 return TRUE;
20696 }
20697 return FALSE;
20698}
20699
20700/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020701 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20702 * Also give an error message.
20703 */
20704 static int
20705var_check_fixed(flags, name)
20706 int flags;
20707 char_u *name;
20708{
20709 if (flags & DI_FLAGS_FIX)
20710 {
20711 EMSG2(_("E795: Cannot delete variable %s"), name);
20712 return TRUE;
20713 }
20714 return FALSE;
20715}
20716
20717/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020718 * Check if a funcref is assigned to a valid variable name.
20719 * Return TRUE and give an error if not.
20720 */
20721 static int
20722var_check_func_name(name, new_var)
20723 char_u *name; /* points to start of variable name */
20724 int new_var; /* TRUE when creating the variable */
20725{
20726 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20727 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20728 ? name[2] : name[0]))
20729 {
20730 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20731 name);
20732 return TRUE;
20733 }
20734 /* Don't allow hiding a function. When "v" is not NULL we might be
20735 * assigning another function to the same var, the type is checked
20736 * below. */
20737 if (new_var && function_exists(name))
20738 {
20739 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20740 name);
20741 return TRUE;
20742 }
20743 return FALSE;
20744}
20745
20746/*
20747 * Check if a variable name is valid.
20748 * Return FALSE and give an error if not.
20749 */
20750 static int
20751valid_varname(varname)
20752 char_u *varname;
20753{
20754 char_u *p;
20755
20756 for (p = varname; *p != NUL; ++p)
20757 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20758 && *p != AUTOLOAD_CHAR)
20759 {
20760 EMSG2(_(e_illvar), varname);
20761 return FALSE;
20762 }
20763 return TRUE;
20764}
20765
20766/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020767 * Return TRUE if typeval "tv" is set to be locked (immutable).
20768 * Also give an error message, using "name".
20769 */
20770 static int
20771tv_check_lock(lock, name)
20772 int lock;
20773 char_u *name;
20774{
20775 if (lock & VAR_LOCKED)
20776 {
20777 EMSG2(_("E741: Value is locked: %s"),
20778 name == NULL ? (char_u *)_("Unknown") : name);
20779 return TRUE;
20780 }
20781 if (lock & VAR_FIXED)
20782 {
20783 EMSG2(_("E742: Cannot change value of %s"),
20784 name == NULL ? (char_u *)_("Unknown") : name);
20785 return TRUE;
20786 }
20787 return FALSE;
20788}
20789
20790/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020791 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020792 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020793 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020794 * It is OK for "from" and "to" to point to the same item. This is used to
20795 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020796 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020797 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020798copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020799 typval_T *from;
20800 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020802 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020803 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020804 switch (from->v_type)
20805 {
20806 case VAR_NUMBER:
20807 to->vval.v_number = from->vval.v_number;
20808 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020809#ifdef FEAT_FLOAT
20810 case VAR_FLOAT:
20811 to->vval.v_float = from->vval.v_float;
20812 break;
20813#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020814 case VAR_STRING:
20815 case VAR_FUNC:
20816 if (from->vval.v_string == NULL)
20817 to->vval.v_string = NULL;
20818 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020819 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020820 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020821 if (from->v_type == VAR_FUNC)
20822 func_ref(to->vval.v_string);
20823 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 break;
20825 case VAR_LIST:
20826 if (from->vval.v_list == NULL)
20827 to->vval.v_list = NULL;
20828 else
20829 {
20830 to->vval.v_list = from->vval.v_list;
20831 ++to->vval.v_list->lv_refcount;
20832 }
20833 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020834 case VAR_DICT:
20835 if (from->vval.v_dict == NULL)
20836 to->vval.v_dict = NULL;
20837 else
20838 {
20839 to->vval.v_dict = from->vval.v_dict;
20840 ++to->vval.v_dict->dv_refcount;
20841 }
20842 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020843 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020844 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020845 break;
20846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847}
20848
20849/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020850 * Make a copy of an item.
20851 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020852 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20853 * reference to an already copied list/dict can be used.
20854 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020855 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020856 static int
20857item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020858 typval_T *from;
20859 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020860 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020861 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020862{
20863 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020864 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865
Bram Moolenaar33570922005-01-25 22:26:29 +000020866 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020867 {
20868 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020869 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020870 }
20871 ++recurse;
20872
20873 switch (from->v_type)
20874 {
20875 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020876#ifdef FEAT_FLOAT
20877 case VAR_FLOAT:
20878#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020879 case VAR_STRING:
20880 case VAR_FUNC:
20881 copy_tv(from, to);
20882 break;
20883 case VAR_LIST:
20884 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020885 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020886 if (from->vval.v_list == NULL)
20887 to->vval.v_list = NULL;
20888 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20889 {
20890 /* use the copy made earlier */
20891 to->vval.v_list = from->vval.v_list->lv_copylist;
20892 ++to->vval.v_list->lv_refcount;
20893 }
20894 else
20895 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20896 if (to->vval.v_list == NULL)
20897 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020898 break;
20899 case VAR_DICT:
20900 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020901 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020902 if (from->vval.v_dict == NULL)
20903 to->vval.v_dict = NULL;
20904 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20905 {
20906 /* use the copy made earlier */
20907 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20908 ++to->vval.v_dict->dv_refcount;
20909 }
20910 else
20911 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20912 if (to->vval.v_dict == NULL)
20913 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020914 break;
20915 default:
20916 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020917 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020918 }
20919 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020920 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020921}
20922
20923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 * ":echo expr1 ..." print each argument separated with a space, add a
20925 * newline at the end.
20926 * ":echon expr1 ..." print each argument plain.
20927 */
20928 void
20929ex_echo(eap)
20930 exarg_T *eap;
20931{
20932 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020933 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020934 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 char_u *p;
20936 int needclr = TRUE;
20937 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020938 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939
20940 if (eap->skip)
20941 ++emsg_skip;
20942 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20943 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020944 /* If eval1() causes an error message the text from the command may
20945 * still need to be cleared. E.g., "echo 22,44". */
20946 need_clr_eos = needclr;
20947
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020949 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 {
20951 /*
20952 * Report the invalid expression unless the expression evaluation
20953 * has been cancelled due to an aborting error, an interrupt, or an
20954 * exception.
20955 */
20956 if (!aborting())
20957 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020958 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 break;
20960 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020961 need_clr_eos = FALSE;
20962
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 if (!eap->skip)
20964 {
20965 if (atstart)
20966 {
20967 atstart = FALSE;
20968 /* Call msg_start() after eval1(), evaluating the expression
20969 * may cause a message to appear. */
20970 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020971 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020972 /* Mark the saved text as finishing the line, so that what
20973 * follows is displayed on a new line when scrolling back
20974 * at the more prompt. */
20975 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 }
20979 else if (eap->cmdidx == CMD_echo)
20980 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020981 current_copyID += COPYID_INC;
20982 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020983 if (p != NULL)
20984 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020986 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020988 if (*p != TAB && needclr)
20989 {
20990 /* remove any text still there from the command */
20991 msg_clr_eos();
20992 needclr = FALSE;
20993 }
20994 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 }
20996 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020997 {
20998#ifdef FEAT_MBYTE
20999 if (has_mbyte)
21000 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021001 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021002
21003 (void)msg_outtrans_len_attr(p, i, echo_attr);
21004 p += i - 1;
21005 }
21006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021008 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021011 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021013 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014 arg = skipwhite(arg);
21015 }
21016 eap->nextcmd = check_nextcmd(arg);
21017
21018 if (eap->skip)
21019 --emsg_skip;
21020 else
21021 {
21022 /* remove text that may still be there from the command */
21023 if (needclr)
21024 msg_clr_eos();
21025 if (eap->cmdidx == CMD_echo)
21026 msg_end();
21027 }
21028}
21029
21030/*
21031 * ":echohl {name}".
21032 */
21033 void
21034ex_echohl(eap)
21035 exarg_T *eap;
21036{
21037 int id;
21038
21039 id = syn_name2id(eap->arg);
21040 if (id == 0)
21041 echo_attr = 0;
21042 else
21043 echo_attr = syn_id2attr(id);
21044}
21045
21046/*
21047 * ":execute expr1 ..." execute the result of an expression.
21048 * ":echomsg expr1 ..." Print a message
21049 * ":echoerr expr1 ..." Print an error
21050 * Each gets spaces around each argument and a newline at the end for
21051 * echo commands
21052 */
21053 void
21054ex_execute(eap)
21055 exarg_T *eap;
21056{
21057 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021058 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 int ret = OK;
21060 char_u *p;
21061 garray_T ga;
21062 int len;
21063 int save_did_emsg;
21064
21065 ga_init2(&ga, 1, 80);
21066
21067 if (eap->skip)
21068 ++emsg_skip;
21069 while (*arg != NUL && *arg != '|' && *arg != '\n')
21070 {
21071 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021072 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073 {
21074 /*
21075 * Report the invalid expression unless the expression evaluation
21076 * has been cancelled due to an aborting error, an interrupt, or an
21077 * exception.
21078 */
21079 if (!aborting())
21080 EMSG2(_(e_invexpr2), p);
21081 ret = FAIL;
21082 break;
21083 }
21084
21085 if (!eap->skip)
21086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 len = (int)STRLEN(p);
21089 if (ga_grow(&ga, len + 2) == FAIL)
21090 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021091 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 ret = FAIL;
21093 break;
21094 }
21095 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 ga.ga_len += len;
21099 }
21100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 arg = skipwhite(arg);
21103 }
21104
21105 if (ret != FAIL && ga.ga_data != NULL)
21106 {
21107 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021108 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021110 out_flush();
21111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 else if (eap->cmdidx == CMD_echoerr)
21113 {
21114 /* We don't want to abort following commands, restore did_emsg. */
21115 save_did_emsg = did_emsg;
21116 EMSG((char_u *)ga.ga_data);
21117 if (!force_abort)
21118 did_emsg = save_did_emsg;
21119 }
21120 else if (eap->cmdidx == CMD_execute)
21121 do_cmdline((char_u *)ga.ga_data,
21122 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21123 }
21124
21125 ga_clear(&ga);
21126
21127 if (eap->skip)
21128 --emsg_skip;
21129
21130 eap->nextcmd = check_nextcmd(arg);
21131}
21132
21133/*
21134 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21135 * "arg" points to the "&" or '+' when called, to "option" when returning.
21136 * Returns NULL when no option name found. Otherwise pointer to the char
21137 * after the option name.
21138 */
21139 static char_u *
21140find_option_end(arg, opt_flags)
21141 char_u **arg;
21142 int *opt_flags;
21143{
21144 char_u *p = *arg;
21145
21146 ++p;
21147 if (*p == 'g' && p[1] == ':')
21148 {
21149 *opt_flags = OPT_GLOBAL;
21150 p += 2;
21151 }
21152 else if (*p == 'l' && p[1] == ':')
21153 {
21154 *opt_flags = OPT_LOCAL;
21155 p += 2;
21156 }
21157 else
21158 *opt_flags = 0;
21159
21160 if (!ASCII_ISALPHA(*p))
21161 return NULL;
21162 *arg = p;
21163
21164 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21165 p += 4; /* termcap option */
21166 else
21167 while (ASCII_ISALPHA(*p))
21168 ++p;
21169 return p;
21170}
21171
21172/*
21173 * ":function"
21174 */
21175 void
21176ex_function(eap)
21177 exarg_T *eap;
21178{
21179 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021180 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 int j;
21182 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021184 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 char_u *name = NULL;
21186 char_u *p;
21187 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021188 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 garray_T newargs;
21190 garray_T newlines;
21191 int varargs = FALSE;
21192 int mustend = FALSE;
21193 int flags = 0;
21194 ufunc_T *fp;
21195 int indent;
21196 int nesting;
21197 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 dictitem_T *v;
21199 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021200 static int func_nr = 0; /* number for nameless function */
21201 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021202 hashtab_T *ht;
21203 int todo;
21204 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021205 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206
21207 /*
21208 * ":function" without argument: list functions.
21209 */
21210 if (ends_excmd(*eap->arg))
21211 {
21212 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021213 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021214 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021215 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021216 {
21217 if (!HASHITEM_EMPTY(hi))
21218 {
21219 --todo;
21220 fp = HI2UF(hi);
21221 if (!isdigit(*fp->uf_name))
21222 list_func_head(fp, FALSE);
21223 }
21224 }
21225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 eap->nextcmd = check_nextcmd(eap->arg);
21227 return;
21228 }
21229
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021230 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021231 * ":function /pat": list functions matching pattern.
21232 */
21233 if (*eap->arg == '/')
21234 {
21235 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21236 if (!eap->skip)
21237 {
21238 regmatch_T regmatch;
21239
21240 c = *p;
21241 *p = NUL;
21242 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21243 *p = c;
21244 if (regmatch.regprog != NULL)
21245 {
21246 regmatch.rm_ic = p_ic;
21247
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021248 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021249 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21250 {
21251 if (!HASHITEM_EMPTY(hi))
21252 {
21253 --todo;
21254 fp = HI2UF(hi);
21255 if (!isdigit(*fp->uf_name)
21256 && vim_regexec(&regmatch, fp->uf_name, 0))
21257 list_func_head(fp, FALSE);
21258 }
21259 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021260 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021261 }
21262 }
21263 if (*p == '/')
21264 ++p;
21265 eap->nextcmd = check_nextcmd(p);
21266 return;
21267 }
21268
21269 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021270 * Get the function name. There are these situations:
21271 * func normal function name
21272 * "name" == func, "fudi.fd_dict" == NULL
21273 * dict.func new dictionary entry
21274 * "name" == NULL, "fudi.fd_dict" set,
21275 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21276 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021277 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021278 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21279 * dict.func existing dict entry that's not a Funcref
21280 * "name" == NULL, "fudi.fd_dict" set,
21281 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021284 name = trans_function_name(&p, eap->skip, 0, &fudi);
21285 paren = (vim_strchr(p, '(') != NULL);
21286 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 {
21288 /*
21289 * Return on an invalid expression in braces, unless the expression
21290 * evaluation has been cancelled due to an aborting error, an
21291 * interrupt, or an exception.
21292 */
21293 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021295 if (!eap->skip && fudi.fd_newkey != NULL)
21296 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021297 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 else
21301 eap->skip = TRUE;
21302 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021303
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 /* An error in a function call during evaluation of an expression in magic
21305 * braces should not cause the function not to be defined. */
21306 saved_did_emsg = did_emsg;
21307 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308
21309 /*
21310 * ":function func" with only function name: list function.
21311 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021312 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 {
21314 if (!ends_excmd(*skipwhite(p)))
21315 {
21316 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021317 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 }
21319 eap->nextcmd = check_nextcmd(p);
21320 if (eap->nextcmd != NULL)
21321 *p = NUL;
21322 if (!eap->skip && !got_int)
21323 {
21324 fp = find_func(name);
21325 if (fp != NULL)
21326 {
21327 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021328 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021330 if (FUNCLINE(fp, j) == NULL)
21331 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 msg_putchar('\n');
21333 msg_outnum((long)(j + 1));
21334 if (j < 9)
21335 msg_putchar(' ');
21336 if (j < 99)
21337 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 out_flush(); /* show a line at a time */
21340 ui_breakcheck();
21341 }
21342 if (!got_int)
21343 {
21344 msg_putchar('\n');
21345 msg_puts((char_u *)" endfunction");
21346 }
21347 }
21348 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021349 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021351 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 }
21353
21354 /*
21355 * ":function name(arg1, arg2)" Define function.
21356 */
21357 p = skipwhite(p);
21358 if (*p != '(')
21359 {
21360 if (!eap->skip)
21361 {
21362 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021363 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 }
21365 /* attempt to continue by skipping some text */
21366 if (vim_strchr(p, '(') != NULL)
21367 p = vim_strchr(p, '(');
21368 }
21369 p = skipwhite(p + 1);
21370
21371 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21372 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21373
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021374 if (!eap->skip)
21375 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021376 /* Check the name of the function. Unless it's a dictionary function
21377 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021378 if (name != NULL)
21379 arg = name;
21380 else
21381 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021382 if (arg != NULL && (fudi.fd_di == NULL
21383 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021384 {
21385 if (*arg == K_SPECIAL)
21386 j = 3;
21387 else
21388 j = 0;
21389 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21390 : eval_isnamec(arg[j])))
21391 ++j;
21392 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021393 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021394 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021395 /* Disallow using the g: dict. */
21396 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21397 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021398 }
21399
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 /*
21401 * Isolate the arguments: "arg1, arg2, ...)"
21402 */
21403 while (*p != ')')
21404 {
21405 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21406 {
21407 varargs = TRUE;
21408 p += 3;
21409 mustend = TRUE;
21410 }
21411 else
21412 {
21413 arg = p;
21414 while (ASCII_ISALNUM(*p) || *p == '_')
21415 ++p;
21416 if (arg == p || isdigit(*arg)
21417 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21418 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21419 {
21420 if (!eap->skip)
21421 EMSG2(_("E125: Illegal argument: %s"), arg);
21422 break;
21423 }
21424 if (ga_grow(&newargs, 1) == FAIL)
21425 goto erret;
21426 c = *p;
21427 *p = NUL;
21428 arg = vim_strsave(arg);
21429 if (arg == NULL)
21430 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021431
21432 /* Check for duplicate argument name. */
21433 for (i = 0; i < newargs.ga_len; ++i)
21434 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21435 {
21436 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21437 goto erret;
21438 }
21439
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21441 *p = c;
21442 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 if (*p == ',')
21444 ++p;
21445 else
21446 mustend = TRUE;
21447 }
21448 p = skipwhite(p);
21449 if (mustend && *p != ')')
21450 {
21451 if (!eap->skip)
21452 EMSG2(_(e_invarg2), eap->arg);
21453 break;
21454 }
21455 }
21456 ++p; /* skip the ')' */
21457
Bram Moolenaare9a41262005-01-15 22:18:47 +000021458 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 for (;;)
21460 {
21461 p = skipwhite(p);
21462 if (STRNCMP(p, "range", 5) == 0)
21463 {
21464 flags |= FC_RANGE;
21465 p += 5;
21466 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021467 else if (STRNCMP(p, "dict", 4) == 0)
21468 {
21469 flags |= FC_DICT;
21470 p += 4;
21471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 else if (STRNCMP(p, "abort", 5) == 0)
21473 {
21474 flags |= FC_ABORT;
21475 p += 5;
21476 }
21477 else
21478 break;
21479 }
21480
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021481 /* When there is a line break use what follows for the function body.
21482 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21483 if (*p == '\n')
21484 line_arg = p + 1;
21485 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 EMSG(_(e_trailing));
21487
21488 /*
21489 * Read the body of the function, until ":endfunction" is found.
21490 */
21491 if (KeyTyped)
21492 {
21493 /* Check if the function already exists, don't let the user type the
21494 * whole function before telling him it doesn't work! For a script we
21495 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021496 if (!eap->skip && !eap->forceit)
21497 {
21498 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21499 EMSG(_(e_funcdict));
21500 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021501 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021504 if (!eap->skip && did_emsg)
21505 goto erret;
21506
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 msg_putchar('\n'); /* don't overwrite the function name */
21508 cmdline_row = msg_row;
21509 }
21510
21511 indent = 2;
21512 nesting = 0;
21513 for (;;)
21514 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021515 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021516 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021517 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021518 saved_wait_return = FALSE;
21519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021521 sourcing_lnum_off = sourcing_lnum;
21522
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021523 if (line_arg != NULL)
21524 {
21525 /* Use eap->arg, split up in parts by line breaks. */
21526 theline = line_arg;
21527 p = vim_strchr(theline, '\n');
21528 if (p == NULL)
21529 line_arg += STRLEN(line_arg);
21530 else
21531 {
21532 *p = NUL;
21533 line_arg = p + 1;
21534 }
21535 }
21536 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 theline = getcmdline(':', 0L, indent);
21538 else
21539 theline = eap->getline(':', eap->cookie, indent);
21540 if (KeyTyped)
21541 lines_left = Rows - 1;
21542 if (theline == NULL)
21543 {
21544 EMSG(_("E126: Missing :endfunction"));
21545 goto erret;
21546 }
21547
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021548 /* Detect line continuation: sourcing_lnum increased more than one. */
21549 if (sourcing_lnum > sourcing_lnum_off + 1)
21550 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21551 else
21552 sourcing_lnum_off = 0;
21553
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 if (skip_until != NULL)
21555 {
21556 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21557 * don't check for ":endfunc". */
21558 if (STRCMP(theline, skip_until) == 0)
21559 {
21560 vim_free(skip_until);
21561 skip_until = NULL;
21562 }
21563 }
21564 else
21565 {
21566 /* skip ':' and blanks*/
21567 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21568 ;
21569
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021570 /* Check for "endfunction". */
21571 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021573 if (line_arg == NULL)
21574 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 break;
21576 }
21577
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021578 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 * at "end". */
21580 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21581 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021582 else if (STRNCMP(p, "if", 2) == 0
21583 || STRNCMP(p, "wh", 2) == 0
21584 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 || STRNCMP(p, "try", 3) == 0)
21586 indent += 2;
21587
21588 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021589 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021591 if (*p == '!')
21592 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 p += eval_fname_script(p);
21594 if (ASCII_ISALPHA(*p))
21595 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 if (*skipwhite(p) == '(')
21598 {
21599 ++nesting;
21600 indent += 2;
21601 }
21602 }
21603 }
21604
21605 /* Check for ":append" or ":insert". */
21606 p = skip_range(p, NULL);
21607 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21608 || (p[0] == 'i'
21609 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21610 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21611 skip_until = vim_strsave((char_u *)".");
21612
21613 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21614 arg = skipwhite(skiptowhite(p));
21615 if (arg[0] == '<' && arg[1] =='<'
21616 && ((p[0] == 'p' && p[1] == 'y'
21617 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21618 || (p[0] == 'p' && p[1] == 'e'
21619 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21620 || (p[0] == 't' && p[1] == 'c'
21621 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021622 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21623 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21625 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021626 || (p[0] == 'm' && p[1] == 'z'
21627 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 ))
21629 {
21630 /* ":python <<" continues until a dot, like ":append" */
21631 p = skipwhite(arg + 2);
21632 if (*p == NUL)
21633 skip_until = vim_strsave((char_u *)".");
21634 else
21635 skip_until = vim_strsave(p);
21636 }
21637 }
21638
21639 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021640 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021641 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021642 if (line_arg == NULL)
21643 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021645 }
21646
21647 /* Copy the line to newly allocated memory. get_one_sourceline()
21648 * allocates 250 bytes per line, this saves 80% on average. The cost
21649 * is an extra alloc/free. */
21650 p = vim_strsave(theline);
21651 if (p != NULL)
21652 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021653 if (line_arg == NULL)
21654 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021655 theline = p;
21656 }
21657
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021658 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21659
21660 /* Add NULL lines for continuation lines, so that the line count is
21661 * equal to the index in the growarray. */
21662 while (sourcing_lnum_off-- > 0)
21663 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021664
21665 /* Check for end of eap->arg. */
21666 if (line_arg != NULL && *line_arg == NUL)
21667 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 }
21669
21670 /* Don't define the function when skipping commands or when an error was
21671 * detected. */
21672 if (eap->skip || did_emsg)
21673 goto erret;
21674
21675 /*
21676 * If there are no errors, add the function
21677 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021678 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021679 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021680 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021681 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021682 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021683 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021684 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 goto erret;
21686 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021687
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021688 fp = find_func(name);
21689 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021691 if (!eap->forceit)
21692 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021693 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 goto erret;
21695 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021696 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021697 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021698 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021699 name);
21700 goto erret;
21701 }
21702 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021703 ga_clear_strings(&(fp->uf_args));
21704 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 vim_free(name);
21706 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 }
21709 else
21710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711 char numbuf[20];
21712
21713 fp = NULL;
21714 if (fudi.fd_newkey == NULL && !eap->forceit)
21715 {
21716 EMSG(_(e_funcdict));
21717 goto erret;
21718 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021719 if (fudi.fd_di == NULL)
21720 {
21721 /* Can't add a function to a locked dictionary */
21722 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21723 goto erret;
21724 }
21725 /* Can't change an existing function if it is locked */
21726 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21727 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021728
21729 /* Give the function a sequential number. Can only be used with a
21730 * Funcref! */
21731 vim_free(name);
21732 sprintf(numbuf, "%d", ++func_nr);
21733 name = vim_strsave((char_u *)numbuf);
21734 if (name == NULL)
21735 goto erret;
21736 }
21737
21738 if (fp == NULL)
21739 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021740 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 {
21742 int slen, plen;
21743 char_u *scriptname;
21744
21745 /* Check that the autoload name matches the script name. */
21746 j = FAIL;
21747 if (sourcing_name != NULL)
21748 {
21749 scriptname = autoload_name(name);
21750 if (scriptname != NULL)
21751 {
21752 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021753 plen = (int)STRLEN(p);
21754 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021755 if (slen > plen && fnamecmp(p,
21756 sourcing_name + slen - plen) == 0)
21757 j = OK;
21758 vim_free(scriptname);
21759 }
21760 }
21761 if (j == FAIL)
21762 {
21763 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21764 goto erret;
21765 }
21766 }
21767
21768 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 if (fp == NULL)
21770 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021771
21772 if (fudi.fd_dict != NULL)
21773 {
21774 if (fudi.fd_di == NULL)
21775 {
21776 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021777 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021778 if (fudi.fd_di == NULL)
21779 {
21780 vim_free(fp);
21781 goto erret;
21782 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021783 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21784 {
21785 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021786 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021787 goto erret;
21788 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021789 }
21790 else
21791 /* overwrite existing dict entry */
21792 clear_tv(&fudi.fd_di->di_tv);
21793 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021794 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021796 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021797
21798 /* behave like "dict" was used */
21799 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 }
21801
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021803 STRCPY(fp->uf_name, name);
21804 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021806 fp->uf_args = newargs;
21807 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021808#ifdef FEAT_PROFILE
21809 fp->uf_tml_count = NULL;
21810 fp->uf_tml_total = NULL;
21811 fp->uf_tml_self = NULL;
21812 fp->uf_profiling = FALSE;
21813 if (prof_def_func())
21814 func_do_profile(fp);
21815#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021816 fp->uf_varargs = varargs;
21817 fp->uf_flags = flags;
21818 fp->uf_calls = 0;
21819 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021820 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821
21822erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 ga_clear_strings(&newargs);
21824 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825ret_free:
21826 vim_free(skip_until);
21827 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021830 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831}
21832
21833/*
21834 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021835 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021838 * TFN_INT: internal function name OK
21839 * TFN_QUIET: be quiet
21840 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 * Advances "pp" to just after the function name (if no error).
21842 */
21843 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021844trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 char_u **pp;
21846 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021850 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 char_u *start;
21852 char_u *end;
21853 int lead;
21854 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021857
21858 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021861
21862 /* Check for hard coded <SNR>: already translated function ID (from a user
21863 * command). */
21864 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21865 && (*pp)[2] == (int)KE_SNR)
21866 {
21867 *pp += 3;
21868 len = get_id_len(pp) + 3;
21869 return vim_strnsave(start, len);
21870 }
21871
21872 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21873 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021877
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021878 /* Note that TFN_ flags use the same values as GLV_ flags. */
21879 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021880 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021881 if (end == start)
21882 {
21883 if (!skip)
21884 EMSG(_("E129: Function name required"));
21885 goto theend;
21886 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021887 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021888 {
21889 /*
21890 * Report an invalid expression in braces, unless the expression
21891 * evaluation has been cancelled due to an aborting error, an
21892 * interrupt, or an exception.
21893 */
21894 if (!aborting())
21895 {
21896 if (end != NULL)
21897 EMSG2(_(e_invarg2), start);
21898 }
21899 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021900 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021901 goto theend;
21902 }
21903
21904 if (lv.ll_tv != NULL)
21905 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021906 if (fdp != NULL)
21907 {
21908 fdp->fd_dict = lv.ll_dict;
21909 fdp->fd_newkey = lv.ll_newkey;
21910 lv.ll_newkey = NULL;
21911 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021913 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21914 {
21915 name = vim_strsave(lv.ll_tv->vval.v_string);
21916 *pp = end;
21917 }
21918 else
21919 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021920 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21921 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021922 EMSG(_(e_funcref));
21923 else
21924 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021925 name = NULL;
21926 }
21927 goto theend;
21928 }
21929
21930 if (lv.ll_name == NULL)
21931 {
21932 /* Error found, but continue after the function name. */
21933 *pp = end;
21934 goto theend;
21935 }
21936
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021937 /* Check if the name is a Funcref. If so, use the value. */
21938 if (lv.ll_exp_name != NULL)
21939 {
21940 len = (int)STRLEN(lv.ll_exp_name);
21941 name = deref_func_name(lv.ll_exp_name, &len);
21942 if (name == lv.ll_exp_name)
21943 name = NULL;
21944 }
21945 else
21946 {
21947 len = (int)(end - *pp);
21948 name = deref_func_name(*pp, &len);
21949 if (name == *pp)
21950 name = NULL;
21951 }
21952 if (name != NULL)
21953 {
21954 name = vim_strsave(name);
21955 *pp = end;
21956 goto theend;
21957 }
21958
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021959 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021960 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021961 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021962 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21963 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21964 {
21965 /* When there was "s:" already or the name expanded to get a
21966 * leading "s:" then remove it. */
21967 lv.ll_name += 2;
21968 len -= 2;
21969 lead = 2;
21970 }
21971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021972 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021973 {
21974 if (lead == 2) /* skip over "s:" */
21975 lv.ll_name += 2;
21976 len = (int)(end - lv.ll_name);
21977 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021978
21979 /*
21980 * Copy the function name to allocated memory.
21981 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21982 * Accept <SNR>123_name() outside a script.
21983 */
21984 if (skip)
21985 lead = 0; /* do nothing */
21986 else if (lead > 0)
21987 {
21988 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021989 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21990 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021991 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021992 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021993 if (current_SID <= 0)
21994 {
21995 EMSG(_(e_usingsid));
21996 goto theend;
21997 }
21998 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21999 lead += (int)STRLEN(sid_buf);
22000 }
22001 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022002 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022003 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022004 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022005 goto theend;
22006 }
22007 name = alloc((unsigned)(len + lead + 1));
22008 if (name != NULL)
22009 {
22010 if (lead > 0)
22011 {
22012 name[0] = K_SPECIAL;
22013 name[1] = KS_EXTRA;
22014 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022015 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022016 STRCPY(name + 3, sid_buf);
22017 }
22018 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22019 name[len + lead] = NUL;
22020 }
22021 *pp = end;
22022
22023theend:
22024 clear_lval(&lv);
22025 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026}
22027
22028/*
22029 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22030 * Return 2 if "p" starts with "s:".
22031 * Return 0 otherwise.
22032 */
22033 static int
22034eval_fname_script(p)
22035 char_u *p;
22036{
22037 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22038 || STRNICMP(p + 1, "SNR>", 4) == 0))
22039 return 5;
22040 if (p[0] == 's' && p[1] == ':')
22041 return 2;
22042 return 0;
22043}
22044
22045/*
22046 * Return TRUE if "p" starts with "<SID>" or "s:".
22047 * Only works if eval_fname_script() returned non-zero for "p"!
22048 */
22049 static int
22050eval_fname_sid(p)
22051 char_u *p;
22052{
22053 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22054}
22055
22056/*
22057 * List the head of the function: "name(arg1, arg2)".
22058 */
22059 static void
22060list_func_head(fp, indent)
22061 ufunc_T *fp;
22062 int indent;
22063{
22064 int j;
22065
22066 msg_start();
22067 if (indent)
22068 MSG_PUTS(" ");
22069 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022070 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 {
22072 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022073 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 }
22075 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022076 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022078 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 {
22080 if (j)
22081 MSG_PUTS(", ");
22082 msg_puts(FUNCARG(fp, j));
22083 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022084 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 {
22086 if (j)
22087 MSG_PUTS(", ");
22088 MSG_PUTS("...");
22089 }
22090 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022091 if (fp->uf_flags & FC_ABORT)
22092 MSG_PUTS(" abort");
22093 if (fp->uf_flags & FC_RANGE)
22094 MSG_PUTS(" range");
22095 if (fp->uf_flags & FC_DICT)
22096 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022097 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022098 if (p_verbose > 0)
22099 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100}
22101
22102/*
22103 * Find a function by name, return pointer to it in ufuncs.
22104 * Return NULL for unknown function.
22105 */
22106 static ufunc_T *
22107find_func(name)
22108 char_u *name;
22109{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022110 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022112 hi = hash_find(&func_hashtab, name);
22113 if (!HASHITEM_EMPTY(hi))
22114 return HI2UF(hi);
22115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116}
22117
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022118#if defined(EXITFREE) || defined(PROTO)
22119 void
22120free_all_functions()
22121{
22122 hashitem_T *hi;
22123
22124 /* Need to start all over every time, because func_free() may change the
22125 * hash table. */
22126 while (func_hashtab.ht_used > 0)
22127 for (hi = func_hashtab.ht_array; ; ++hi)
22128 if (!HASHITEM_EMPTY(hi))
22129 {
22130 func_free(HI2UF(hi));
22131 break;
22132 }
22133}
22134#endif
22135
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022136 int
22137translated_function_exists(name)
22138 char_u *name;
22139{
22140 if (builtin_function(name))
22141 return find_internal_func(name) >= 0;
22142 return find_func(name) != NULL;
22143}
22144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145/*
22146 * Return TRUE if a function "name" exists.
22147 */
22148 static int
22149function_exists(name)
22150 char_u *name;
22151{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022152 char_u *nm = name;
22153 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022154 int n = FALSE;
22155
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022156 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22157 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022158 nm = skipwhite(nm);
22159
22160 /* Only accept "funcname", "funcname ", "funcname (..." and
22161 * "funcname(...", not "funcname!...". */
22162 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022163 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022164 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022165 return n;
22166}
22167
Bram Moolenaara1544c02013-05-30 12:35:52 +020022168 char_u *
22169get_expanded_name(name, check)
22170 char_u *name;
22171 int check;
22172{
22173 char_u *nm = name;
22174 char_u *p;
22175
22176 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22177
22178 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022179 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022180 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022181
Bram Moolenaara1544c02013-05-30 12:35:52 +020022182 vim_free(p);
22183 return NULL;
22184}
22185
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022186/*
22187 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022188 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022189 */
22190 static int
22191builtin_function(name)
22192 char_u *name;
22193{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022194 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22195 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196}
22197
Bram Moolenaar05159a02005-02-26 23:04:13 +000022198#if defined(FEAT_PROFILE) || defined(PROTO)
22199/*
22200 * Start profiling function "fp".
22201 */
22202 static void
22203func_do_profile(fp)
22204 ufunc_T *fp;
22205{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022206 int len = fp->uf_lines.ga_len;
22207
22208 if (len == 0)
22209 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022210 fp->uf_tm_count = 0;
22211 profile_zero(&fp->uf_tm_self);
22212 profile_zero(&fp->uf_tm_total);
22213 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022214 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022215 if (fp->uf_tml_total == NULL)
22216 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022217 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022218 if (fp->uf_tml_self == NULL)
22219 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022220 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022221 fp->uf_tml_idx = -1;
22222 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22223 || fp->uf_tml_self == NULL)
22224 return; /* out of memory */
22225
22226 fp->uf_profiling = TRUE;
22227}
22228
22229/*
22230 * Dump the profiling results for all functions in file "fd".
22231 */
22232 void
22233func_dump_profile(fd)
22234 FILE *fd;
22235{
22236 hashitem_T *hi;
22237 int todo;
22238 ufunc_T *fp;
22239 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022240 ufunc_T **sorttab;
22241 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022243 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022244 if (todo == 0)
22245 return; /* nothing to dump */
22246
Bram Moolenaar73830342005-02-28 22:48:19 +000022247 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22248
Bram Moolenaar05159a02005-02-26 23:04:13 +000022249 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22250 {
22251 if (!HASHITEM_EMPTY(hi))
22252 {
22253 --todo;
22254 fp = HI2UF(hi);
22255 if (fp->uf_profiling)
22256 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022257 if (sorttab != NULL)
22258 sorttab[st_len++] = fp;
22259
Bram Moolenaar05159a02005-02-26 23:04:13 +000022260 if (fp->uf_name[0] == K_SPECIAL)
22261 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22262 else
22263 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22264 if (fp->uf_tm_count == 1)
22265 fprintf(fd, "Called 1 time\n");
22266 else
22267 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22268 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22269 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22270 fprintf(fd, "\n");
22271 fprintf(fd, "count total (s) self (s)\n");
22272
22273 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22274 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022275 if (FUNCLINE(fp, i) == NULL)
22276 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022277 prof_func_line(fd, fp->uf_tml_count[i],
22278 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022279 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22280 }
22281 fprintf(fd, "\n");
22282 }
22283 }
22284 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022285
22286 if (sorttab != NULL && st_len > 0)
22287 {
22288 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22289 prof_total_cmp);
22290 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22291 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22292 prof_self_cmp);
22293 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22294 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022295
22296 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022297}
Bram Moolenaar73830342005-02-28 22:48:19 +000022298
22299 static void
22300prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22301 FILE *fd;
22302 ufunc_T **sorttab;
22303 int st_len;
22304 char *title;
22305 int prefer_self; /* when equal print only self time */
22306{
22307 int i;
22308 ufunc_T *fp;
22309
22310 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22311 fprintf(fd, "count total (s) self (s) function\n");
22312 for (i = 0; i < 20 && i < st_len; ++i)
22313 {
22314 fp = sorttab[i];
22315 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22316 prefer_self);
22317 if (fp->uf_name[0] == K_SPECIAL)
22318 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22319 else
22320 fprintf(fd, " %s()\n", fp->uf_name);
22321 }
22322 fprintf(fd, "\n");
22323}
22324
22325/*
22326 * Print the count and times for one function or function line.
22327 */
22328 static void
22329prof_func_line(fd, count, total, self, prefer_self)
22330 FILE *fd;
22331 int count;
22332 proftime_T *total;
22333 proftime_T *self;
22334 int prefer_self; /* when equal print only self time */
22335{
22336 if (count > 0)
22337 {
22338 fprintf(fd, "%5d ", count);
22339 if (prefer_self && profile_equal(total, self))
22340 fprintf(fd, " ");
22341 else
22342 fprintf(fd, "%s ", profile_msg(total));
22343 if (!prefer_self && profile_equal(total, self))
22344 fprintf(fd, " ");
22345 else
22346 fprintf(fd, "%s ", profile_msg(self));
22347 }
22348 else
22349 fprintf(fd, " ");
22350}
22351
22352/*
22353 * Compare function for total time sorting.
22354 */
22355 static int
22356#ifdef __BORLANDC__
22357_RTLENTRYF
22358#endif
22359prof_total_cmp(s1, s2)
22360 const void *s1;
22361 const void *s2;
22362{
22363 ufunc_T *p1, *p2;
22364
22365 p1 = *(ufunc_T **)s1;
22366 p2 = *(ufunc_T **)s2;
22367 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22368}
22369
22370/*
22371 * Compare function for self time sorting.
22372 */
22373 static int
22374#ifdef __BORLANDC__
22375_RTLENTRYF
22376#endif
22377prof_self_cmp(s1, s2)
22378 const void *s1;
22379 const void *s2;
22380{
22381 ufunc_T *p1, *p2;
22382
22383 p1 = *(ufunc_T **)s1;
22384 p2 = *(ufunc_T **)s2;
22385 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22386}
22387
Bram Moolenaar05159a02005-02-26 23:04:13 +000022388#endif
22389
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022390/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022391 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022392 * Return TRUE if a package was loaded.
22393 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022394 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022395script_autoload(name, reload)
22396 char_u *name;
22397 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022398{
22399 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022400 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022401 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022402 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022403
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022404 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022405 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022406 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022407 return FALSE;
22408
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022409 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022410
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022411 /* Find the name in the list of previously loaded package names. Skip
22412 * "autoload/", it's always the same. */
22413 for (i = 0; i < ga_loaded.ga_len; ++i)
22414 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22415 break;
22416 if (!reload && i < ga_loaded.ga_len)
22417 ret = FALSE; /* was loaded already */
22418 else
22419 {
22420 /* Remember the name if it wasn't loaded already. */
22421 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22422 {
22423 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22424 tofree = NULL;
22425 }
22426
22427 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022428 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022429 ret = TRUE;
22430 }
22431
22432 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022433 return ret;
22434}
22435
22436/*
22437 * Return the autoload script name for a function or variable name.
22438 * Returns NULL when out of memory.
22439 */
22440 static char_u *
22441autoload_name(name)
22442 char_u *name;
22443{
22444 char_u *p;
22445 char_u *scriptname;
22446
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022447 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022448 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22449 if (scriptname == NULL)
22450 return FALSE;
22451 STRCPY(scriptname, "autoload/");
22452 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022453 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022454 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022455 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022456 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022457 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022458}
22459
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22461
22462/*
22463 * Function given to ExpandGeneric() to obtain the list of user defined
22464 * function names.
22465 */
22466 char_u *
22467get_user_func_name(xp, idx)
22468 expand_T *xp;
22469 int idx;
22470{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022471 static long_u done;
22472 static hashitem_T *hi;
22473 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474
22475 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022477 done = 0;
22478 hi = func_hashtab.ht_array;
22479 }
22480 if (done < func_hashtab.ht_used)
22481 {
22482 if (done++ > 0)
22483 ++hi;
22484 while (HASHITEM_EMPTY(hi))
22485 ++hi;
22486 fp = HI2UF(hi);
22487
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022488 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022489 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022490
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022491 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22492 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493
22494 cat_func_name(IObuff, fp);
22495 if (xp->xp_context != EXPAND_USER_FUNC)
22496 {
22497 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022498 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 STRCAT(IObuff, ")");
22500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 return IObuff;
22502 }
22503 return NULL;
22504}
22505
22506#endif /* FEAT_CMDL_COMPL */
22507
22508/*
22509 * Copy the function name of "fp" to buffer "buf".
22510 * "buf" must be able to hold the function name plus three bytes.
22511 * Takes care of script-local function names.
22512 */
22513 static void
22514cat_func_name(buf, fp)
22515 char_u *buf;
22516 ufunc_T *fp;
22517{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022518 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 {
22520 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022521 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 }
22523 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022524 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525}
22526
22527/*
22528 * ":delfunction {name}"
22529 */
22530 void
22531ex_delfunction(eap)
22532 exarg_T *eap;
22533{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022534 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 char_u *p;
22536 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538
22539 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 name = trans_function_name(&p, eap->skip, 0, &fudi);
22541 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022543 {
22544 if (fudi.fd_dict != NULL && !eap->skip)
22545 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 if (!ends_excmd(*skipwhite(p)))
22549 {
22550 vim_free(name);
22551 EMSG(_(e_trailing));
22552 return;
22553 }
22554 eap->nextcmd = check_nextcmd(p);
22555 if (eap->nextcmd != NULL)
22556 *p = NUL;
22557
22558 if (!eap->skip)
22559 fp = find_func(name);
22560 vim_free(name);
22561
22562 if (!eap->skip)
22563 {
22564 if (fp == NULL)
22565 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022566 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 return;
22568 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022569 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 {
22571 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22572 return;
22573 }
22574
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022577 /* Delete the dict item that refers to the function, it will
22578 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022579 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022581 else
22582 func_free(fp);
22583 }
22584}
22585
22586/*
22587 * Free a function and remove it from the list of functions.
22588 */
22589 static void
22590func_free(fp)
22591 ufunc_T *fp;
22592{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022593 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022594
22595 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022596 ga_clear_strings(&(fp->uf_args));
22597 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022598#ifdef FEAT_PROFILE
22599 vim_free(fp->uf_tml_count);
22600 vim_free(fp->uf_tml_total);
22601 vim_free(fp->uf_tml_self);
22602#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022603
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022604 /* remove the function from the function hashtable */
22605 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22606 if (HASHITEM_EMPTY(hi))
22607 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022608 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022609 hash_remove(&func_hashtab, hi);
22610
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 vim_free(fp);
22612}
22613
22614/*
22615 * Unreference a Function: decrement the reference count and free it when it
22616 * becomes zero. Only for numbered functions.
22617 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022618 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022619func_unref(name)
22620 char_u *name;
22621{
22622 ufunc_T *fp;
22623
22624 if (name != NULL && isdigit(*name))
22625 {
22626 fp = find_func(name);
22627 if (fp == NULL)
22628 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022629 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 {
22631 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022632 * when "uf_calls" becomes zero. */
22633 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022634 func_free(fp);
22635 }
22636 }
22637}
22638
22639/*
22640 * Count a reference to a Function.
22641 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022642 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022643func_ref(name)
22644 char_u *name;
22645{
22646 ufunc_T *fp;
22647
22648 if (name != NULL && isdigit(*name))
22649 {
22650 fp = find_func(name);
22651 if (fp == NULL)
22652 EMSG2(_(e_intern2), "func_ref()");
22653 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022654 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 }
22656}
22657
22658/*
22659 * Call a user function.
22660 */
22661 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022662call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 ufunc_T *fp; /* pointer to function */
22664 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 typval_T *argvars; /* arguments */
22666 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 linenr_T firstline; /* first line of range */
22668 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670{
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 char_u *save_sourcing_name;
22672 linenr_T save_sourcing_lnum;
22673 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022674 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 int save_did_emsg;
22676 static int depth = 0;
22677 dictitem_T *v;
22678 int fixvar_idx = 0; /* index in fixvar[] */
22679 int i;
22680 int ai;
22681 char_u numbuf[NUMBUFLEN];
22682 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022683#ifdef FEAT_PROFILE
22684 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022685 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687
22688 /* If depth of calling is getting too high, don't execute the function */
22689 if (depth >= p_mfd)
22690 {
22691 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 rettv->v_type = VAR_NUMBER;
22693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 return;
22695 }
22696 ++depth;
22697
22698 line_breakcheck(); /* check for CTRL-C hit */
22699
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022700 fc = (funccall_T *)alloc(sizeof(funccall_T));
22701 fc->caller = current_funccal;
22702 current_funccal = fc;
22703 fc->func = fp;
22704 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022705 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022706 fc->linenr = 0;
22707 fc->returned = FALSE;
22708 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022710 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22711 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022714 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22716 * each argument variable and saves a lot of time.
22717 */
22718 /*
22719 * Init l: variables.
22720 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022721 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022722 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022723 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022724 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22725 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022726 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022727 name = v->di_key;
22728 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022730 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022732 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 v->di_tv.vval.v_dict = selfdict;
22734 ++selfdict->dv_refcount;
22735 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022736
Bram Moolenaar33570922005-01-25 22:26:29 +000022737 /*
22738 * Init a: variables.
22739 * Set a:0 to "argcount".
22740 * Set a:000 to a list with room for the "..." arguments.
22741 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022742 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022743 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022744 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022745 /* Use "name" to avoid a warning from some compiler that checks the
22746 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022747 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022748 name = v->di_key;
22749 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022751 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022753 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022754 v->di_tv.vval.v_list = &fc->l_varlist;
22755 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22756 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22757 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022758
22759 /*
22760 * Set a:firstline to "firstline" and a:lastline to "lastline".
22761 * Set a:name to named arguments.
22762 * Set a:N to the "..." arguments.
22763 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022764 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022765 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022766 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 (varnumber_T)lastline);
22768 for (i = 0; i < argcount; ++i)
22769 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022770 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 if (ai < 0)
22772 /* named argument a:name */
22773 name = FUNCARG(fp, i);
22774 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022775 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 /* "..." argument a:1, a:2, etc. */
22777 sprintf((char *)numbuf, "%d", ai + 1);
22778 name = numbuf;
22779 }
22780 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22781 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022782 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22784 }
22785 else
22786 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022787 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22788 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 if (v == NULL)
22790 break;
22791 v->di_flags = DI_FLAGS_RO;
22792 }
22793 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022794 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022795
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022796 /* Note: the values are copied directly to avoid alloc/free.
22797 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022799 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022800
22801 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22802 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022803 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22804 fc->l_listitems[ai].li_tv = argvars[i];
22805 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022806 }
22807 }
22808
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 /* Don't redraw while executing the function. */
22810 ++RedrawingDisabled;
22811 save_sourcing_name = sourcing_name;
22812 save_sourcing_lnum = sourcing_lnum;
22813 sourcing_lnum = 1;
22814 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022815 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 if (sourcing_name != NULL)
22817 {
22818 if (save_sourcing_name != NULL
22819 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22820 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22821 else
22822 STRCPY(sourcing_name, "function ");
22823 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22824
22825 if (p_verbose >= 12)
22826 {
22827 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022828 verbose_enter_scroll();
22829
Bram Moolenaar555b2802005-05-19 21:08:39 +000022830 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 if (p_verbose >= 14)
22832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022834 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022835 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022836 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837
22838 msg_puts((char_u *)"(");
22839 for (i = 0; i < argcount; ++i)
22840 {
22841 if (i > 0)
22842 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022843 if (argvars[i].v_type == VAR_NUMBER)
22844 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 else
22846 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022847 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22848 if (s != NULL)
22849 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022850 if (vim_strsize(s) > MSG_BUF_CLEN)
22851 {
22852 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22853 s = buf;
22854 }
22855 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022856 vim_free(tofree);
22857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 }
22859 }
22860 msg_puts((char_u *)")");
22861 }
22862 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022863
22864 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 --no_wait_return;
22866 }
22867 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022868#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022869 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022870 {
22871 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22872 func_do_profile(fp);
22873 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022874 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022875 {
22876 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022877 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022878 profile_zero(&fp->uf_tm_children);
22879 }
22880 script_prof_save(&wait_start);
22881 }
22882#endif
22883
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022885 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 save_did_emsg = did_emsg;
22887 did_emsg = FALSE;
22888
22889 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022890 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22892
22893 --RedrawingDisabled;
22894
22895 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022896 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898 clear_tv(rettv);
22899 rettv->v_type = VAR_NUMBER;
22900 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 }
22902
Bram Moolenaar05159a02005-02-26 23:04:13 +000022903#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022904 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022905 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022906 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022907 profile_end(&call_start);
22908 profile_sub_wait(&wait_start, &call_start);
22909 profile_add(&fp->uf_tm_total, &call_start);
22910 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022911 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022912 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022913 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22914 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915 }
22916 }
22917#endif
22918
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 /* when being verbose, mention the return value */
22920 if (p_verbose >= 12)
22921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022923 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022926 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022927 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022928 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022929 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022932 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022933 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022934 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022935 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022936
Bram Moolenaar555b2802005-05-19 21:08:39 +000022937 /* The value may be very long. Skip the middle part, so that we
22938 * have some idea how it starts and ends. smsg() would always
22939 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022940 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022941 if (s != NULL)
22942 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022943 if (vim_strsize(s) > MSG_BUF_CLEN)
22944 {
22945 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22946 s = buf;
22947 }
22948 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022949 vim_free(tofree);
22950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 }
22952 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022953
22954 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 --no_wait_return;
22956 }
22957
22958 vim_free(sourcing_name);
22959 sourcing_name = save_sourcing_name;
22960 sourcing_lnum = save_sourcing_lnum;
22961 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022962#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022963 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022964 script_prof_restore(&wait_start);
22965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966
22967 if (p_verbose >= 12 && sourcing_name != NULL)
22968 {
22969 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022970 verbose_enter_scroll();
22971
Bram Moolenaar555b2802005-05-19 21:08:39 +000022972 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022974
22975 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 --no_wait_return;
22977 }
22978
22979 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022980 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022982
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022983 /* If the a:000 list and the l: and a: dicts are not referenced we can
22984 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022985 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22986 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22987 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22988 {
22989 free_funccal(fc, FALSE);
22990 }
22991 else
22992 {
22993 hashitem_T *hi;
22994 listitem_T *li;
22995 int todo;
22996
22997 /* "fc" is still in use. This can happen when returning "a:000" or
22998 * assigning "l:" to a global variable.
22999 * Link "fc" in the list for garbage collection later. */
23000 fc->caller = previous_funccal;
23001 previous_funccal = fc;
23002
23003 /* Make a copy of the a: variables, since we didn't do that above. */
23004 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23005 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23006 {
23007 if (!HASHITEM_EMPTY(hi))
23008 {
23009 --todo;
23010 v = HI2DI(hi);
23011 copy_tv(&v->di_tv, &v->di_tv);
23012 }
23013 }
23014
23015 /* Make a copy of the a:000 items, since we didn't do that above. */
23016 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23017 copy_tv(&li->li_tv, &li->li_tv);
23018 }
23019}
23020
23021/*
23022 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023023 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023024 */
23025 static int
23026can_free_funccal(fc, copyID)
23027 funccall_T *fc;
23028 int copyID;
23029{
23030 return (fc->l_varlist.lv_copyID != copyID
23031 && fc->l_vars.dv_copyID != copyID
23032 && fc->l_avars.dv_copyID != copyID);
23033}
23034
23035/*
23036 * Free "fc" and what it contains.
23037 */
23038 static void
23039free_funccal(fc, free_val)
23040 funccall_T *fc;
23041 int free_val; /* a: vars were allocated */
23042{
23043 listitem_T *li;
23044
23045 /* The a: variables typevals may not have been allocated, only free the
23046 * allocated variables. */
23047 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23048
23049 /* free all l: variables */
23050 vars_clear(&fc->l_vars.dv_hashtab);
23051
23052 /* Free the a:000 variables if they were allocated. */
23053 if (free_val)
23054 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23055 clear_tv(&li->li_tv);
23056
23057 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058}
23059
23060/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023061 * Add a number variable "name" to dict "dp" with value "nr".
23062 */
23063 static void
23064add_nr_var(dp, v, name, nr)
23065 dict_T *dp;
23066 dictitem_T *v;
23067 char *name;
23068 varnumber_T nr;
23069{
23070 STRCPY(v->di_key, name);
23071 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23072 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23073 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023074 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023075 v->di_tv.vval.v_number = nr;
23076}
23077
23078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 * ":return [expr]"
23080 */
23081 void
23082ex_return(eap)
23083 exarg_T *eap;
23084{
23085 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 int returning = FALSE;
23088
23089 if (current_funccal == NULL)
23090 {
23091 EMSG(_("E133: :return not inside a function"));
23092 return;
23093 }
23094
23095 if (eap->skip)
23096 ++emsg_skip;
23097
23098 eap->nextcmd = NULL;
23099 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023100 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 {
23102 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023103 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023105 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 }
23107 /* It's safer to return also on error. */
23108 else if (!eap->skip)
23109 {
23110 /*
23111 * Return unless the expression evaluation has been cancelled due to an
23112 * aborting error, an interrupt, or an exception.
23113 */
23114 if (!aborting())
23115 returning = do_return(eap, FALSE, TRUE, NULL);
23116 }
23117
23118 /* When skipping or the return gets pending, advance to the next command
23119 * in this line (!returning). Otherwise, ignore the rest of the line.
23120 * Following lines will be ignored by get_func_line(). */
23121 if (returning)
23122 eap->nextcmd = NULL;
23123 else if (eap->nextcmd == NULL) /* no argument */
23124 eap->nextcmd = check_nextcmd(arg);
23125
23126 if (eap->skip)
23127 --emsg_skip;
23128}
23129
23130/*
23131 * Return from a function. Possibly makes the return pending. Also called
23132 * for a pending return at the ":endtry" or after returning from an extra
23133 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023135 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 * FALSE when the return gets pending.
23137 */
23138 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023139do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 exarg_T *eap;
23141 int reanimate;
23142 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023143 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144{
23145 int idx;
23146 struct condstack *cstack = eap->cstack;
23147
23148 if (reanimate)
23149 /* Undo the return. */
23150 current_funccal->returned = FALSE;
23151
23152 /*
23153 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23154 * not in its finally clause (which then is to be executed next) is found.
23155 * In this case, make the ":return" pending for execution at the ":endtry".
23156 * Otherwise, return normally.
23157 */
23158 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23159 if (idx >= 0)
23160 {
23161 cstack->cs_pending[idx] = CSTP_RETURN;
23162
23163 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023164 /* A pending return again gets pending. "rettv" points to an
23165 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023167 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 else
23169 {
23170 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023171 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023173 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023175 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 {
23177 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023178 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 else
23181 EMSG(_(e_outofmem));
23182 }
23183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185
23186 if (reanimate)
23187 {
23188 /* The pending return value could be overwritten by a ":return"
23189 * without argument in a finally clause; reset the default
23190 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023191 current_funccal->rettv->v_type = VAR_NUMBER;
23192 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 }
23194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023195 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 }
23197 else
23198 {
23199 current_funccal->returned = TRUE;
23200
23201 /* If the return is carried out now, store the return value. For
23202 * a return immediately after reanimation, the value is already
23203 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023204 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023206 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023209 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 }
23211 }
23212
23213 return idx < 0;
23214}
23215
23216/*
23217 * Free the variable with a pending return value.
23218 */
23219 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023220discard_pending_return(rettv)
23221 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222{
Bram Moolenaar33570922005-01-25 22:26:29 +000023223 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224}
23225
23226/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023227 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 * is an allocated string. Used by report_pending() for verbose messages.
23229 */
23230 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023231get_return_cmd(rettv)
23232 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023234 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023235 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023236 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023238 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023239 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023240 if (s == NULL)
23241 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023242
23243 STRCPY(IObuff, ":return ");
23244 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23245 if (STRLEN(s) + 8 >= IOSIZE)
23246 STRCPY(IObuff + IOSIZE - 4, "...");
23247 vim_free(tofree);
23248 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249}
23250
23251/*
23252 * Get next function line.
23253 * Called by do_cmdline() to get the next line.
23254 * Returns allocated string, or NULL for end of function.
23255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 char_u *
23257get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023258 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023260 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261{
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023263 ufunc_T *fp = fcp->func;
23264 char_u *retval;
23265 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266
23267 /* If breakpoints have been added/deleted need to check for it. */
23268 if (fcp->dbg_tick != debug_tick)
23269 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023270 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 sourcing_lnum);
23272 fcp->dbg_tick = debug_tick;
23273 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023274#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023275 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276 func_line_end(cookie);
23277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023280 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23281 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 retval = NULL;
23283 else
23284 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023285 /* Skip NULL lines (continuation lines). */
23286 while (fcp->linenr < gap->ga_len
23287 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23288 ++fcp->linenr;
23289 if (fcp->linenr >= gap->ga_len)
23290 retval = NULL;
23291 else
23292 {
23293 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23294 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023295#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023296 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023297 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023298#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 }
23301
23302 /* Did we encounter a breakpoint? */
23303 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23304 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023305 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 sourcing_lnum);
23309 fcp->dbg_tick = debug_tick;
23310 }
23311
23312 return retval;
23313}
23314
Bram Moolenaar05159a02005-02-26 23:04:13 +000023315#if defined(FEAT_PROFILE) || defined(PROTO)
23316/*
23317 * Called when starting to read a function line.
23318 * "sourcing_lnum" must be correct!
23319 * When skipping lines it may not actually be executed, but we won't find out
23320 * until later and we need to store the time now.
23321 */
23322 void
23323func_line_start(cookie)
23324 void *cookie;
23325{
23326 funccall_T *fcp = (funccall_T *)cookie;
23327 ufunc_T *fp = fcp->func;
23328
23329 if (fp->uf_profiling && sourcing_lnum >= 1
23330 && sourcing_lnum <= fp->uf_lines.ga_len)
23331 {
23332 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023333 /* Skip continuation lines. */
23334 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23335 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023336 fp->uf_tml_execed = FALSE;
23337 profile_start(&fp->uf_tml_start);
23338 profile_zero(&fp->uf_tml_children);
23339 profile_get_wait(&fp->uf_tml_wait);
23340 }
23341}
23342
23343/*
23344 * Called when actually executing a function line.
23345 */
23346 void
23347func_line_exec(cookie)
23348 void *cookie;
23349{
23350 funccall_T *fcp = (funccall_T *)cookie;
23351 ufunc_T *fp = fcp->func;
23352
23353 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23354 fp->uf_tml_execed = TRUE;
23355}
23356
23357/*
23358 * Called when done with a function line.
23359 */
23360 void
23361func_line_end(cookie)
23362 void *cookie;
23363{
23364 funccall_T *fcp = (funccall_T *)cookie;
23365 ufunc_T *fp = fcp->func;
23366
23367 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23368 {
23369 if (fp->uf_tml_execed)
23370 {
23371 ++fp->uf_tml_count[fp->uf_tml_idx];
23372 profile_end(&fp->uf_tml_start);
23373 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023374 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023375 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23376 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023377 }
23378 fp->uf_tml_idx = -1;
23379 }
23380}
23381#endif
23382
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383/*
23384 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023385 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386 */
23387 int
23388func_has_ended(cookie)
23389 void *cookie;
23390{
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392
23393 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23394 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023395 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 || fcp->returned);
23397}
23398
23399/*
23400 * return TRUE if cookie indicates a function which "abort"s on errors.
23401 */
23402 int
23403func_has_abort(cookie)
23404 void *cookie;
23405{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023406 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407}
23408
23409#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23410typedef enum
23411{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023412 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23413 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23414 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415} var_flavour_T;
23416
23417static var_flavour_T var_flavour __ARGS((char_u *varname));
23418
23419 static var_flavour_T
23420var_flavour(varname)
23421 char_u *varname;
23422{
23423 char_u *p = varname;
23424
23425 if (ASCII_ISUPPER(*p))
23426 {
23427 while (*(++p))
23428 if (ASCII_ISLOWER(*p))
23429 return VAR_FLAVOUR_SESSION;
23430 return VAR_FLAVOUR_VIMINFO;
23431 }
23432 else
23433 return VAR_FLAVOUR_DEFAULT;
23434}
23435#endif
23436
23437#if defined(FEAT_VIMINFO) || defined(PROTO)
23438/*
23439 * Restore global vars that start with a capital from the viminfo file
23440 */
23441 int
23442read_viminfo_varlist(virp, writing)
23443 vir_T *virp;
23444 int writing;
23445{
23446 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023447 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023448 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449
23450 if (!writing && (find_viminfo_parameter('!') != NULL))
23451 {
23452 tab = vim_strchr(virp->vir_line + 1, '\t');
23453 if (tab != NULL)
23454 {
23455 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023456 switch (*tab)
23457 {
23458 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023459#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023460 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023461#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023462 case 'D': type = VAR_DICT; break;
23463 case 'L': type = VAR_LIST; break;
23464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465
23466 tab = vim_strchr(tab, '\t');
23467 if (tab != NULL)
23468 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023469 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023470 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023471 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023473#ifdef FEAT_FLOAT
23474 else if (type == VAR_FLOAT)
23475 (void)string2float(tab + 1, &tv.vval.v_float);
23476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023478 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023479 if (type == VAR_DICT || type == VAR_LIST)
23480 {
23481 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23482
23483 if (etv == NULL)
23484 /* Failed to parse back the dict or list, use it as a
23485 * string. */
23486 tv.v_type = VAR_STRING;
23487 else
23488 {
23489 vim_free(tv.vval.v_string);
23490 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023491 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023492 }
23493 }
23494
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023495 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023496
23497 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023498 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023499 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23500 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 }
23502 }
23503 }
23504
23505 return viminfo_readline(virp);
23506}
23507
23508/*
23509 * Write global vars that start with a capital to the viminfo file
23510 */
23511 void
23512write_viminfo_varlist(fp)
23513 FILE *fp;
23514{
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 hashitem_T *hi;
23516 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023517 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023518 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023519 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023520 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023521 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522
23523 if (find_viminfo_parameter('!') == NULL)
23524 return;
23525
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023526 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023527
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023528 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023529 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023531 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023533 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023534 this_var = HI2DI(hi);
23535 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023538 {
23539 case VAR_STRING: s = "STR"; break;
23540 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023541#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023542 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023543#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023544 case VAR_DICT: s = "DIC"; break;
23545 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023546 default: continue;
23547 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023548 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023549 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023550 if (p != NULL)
23551 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023552 vim_free(tofree);
23553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 }
23555 }
23556}
23557#endif
23558
23559#if defined(FEAT_SESSION) || defined(PROTO)
23560 int
23561store_session_globals(fd)
23562 FILE *fd;
23563{
Bram Moolenaar33570922005-01-25 22:26:29 +000023564 hashitem_T *hi;
23565 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023566 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 char_u *p, *t;
23568
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023569 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023570 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023572 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023574 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023575 this_var = HI2DI(hi);
23576 if ((this_var->di_tv.v_type == VAR_NUMBER
23577 || this_var->di_tv.v_type == VAR_STRING)
23578 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023579 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023580 /* Escape special characters with a backslash. Turn a LF and
23581 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023582 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023583 (char_u *)"\\\"\n\r");
23584 if (p == NULL) /* out of memory */
23585 break;
23586 for (t = p; *t != NUL; ++t)
23587 if (*t == '\n')
23588 *t = 'n';
23589 else if (*t == '\r')
23590 *t = 'r';
23591 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023592 this_var->di_key,
23593 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23594 : ' ',
23595 p,
23596 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23597 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023598 || put_eol(fd) == FAIL)
23599 {
23600 vim_free(p);
23601 return FAIL;
23602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 vim_free(p);
23604 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023605#ifdef FEAT_FLOAT
23606 else if (this_var->di_tv.v_type == VAR_FLOAT
23607 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23608 {
23609 float_T f = this_var->di_tv.vval.v_float;
23610 int sign = ' ';
23611
23612 if (f < 0)
23613 {
23614 f = -f;
23615 sign = '-';
23616 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023617 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023618 this_var->di_key, sign, f) < 0)
23619 || put_eol(fd) == FAIL)
23620 return FAIL;
23621 }
23622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623 }
23624 }
23625 return OK;
23626}
23627#endif
23628
Bram Moolenaar661b1822005-07-28 22:36:45 +000023629/*
23630 * Display script name where an item was last set.
23631 * Should only be invoked when 'verbose' is non-zero.
23632 */
23633 void
23634last_set_msg(scriptID)
23635 scid_T scriptID;
23636{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023637 char_u *p;
23638
Bram Moolenaar661b1822005-07-28 22:36:45 +000023639 if (scriptID != 0)
23640 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023641 p = home_replace_save(NULL, get_scriptname(scriptID));
23642 if (p != NULL)
23643 {
23644 verbose_enter();
23645 MSG_PUTS(_("\n\tLast set from "));
23646 MSG_PUTS(p);
23647 vim_free(p);
23648 verbose_leave();
23649 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023650 }
23651}
23652
Bram Moolenaard812df62008-11-09 12:46:09 +000023653/*
23654 * List v:oldfiles in a nice way.
23655 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023656 void
23657ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023658 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023659{
23660 list_T *l = vimvars[VV_OLDFILES].vv_list;
23661 listitem_T *li;
23662 int nr = 0;
23663
23664 if (l == NULL)
23665 msg((char_u *)_("No old files"));
23666 else
23667 {
23668 msg_start();
23669 msg_scroll = TRUE;
23670 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23671 {
23672 msg_outnum((long)++nr);
23673 MSG_PUTS(": ");
23674 msg_outtrans(get_tv_string(&li->li_tv));
23675 msg_putchar('\n');
23676 out_flush(); /* output one line at a time */
23677 ui_breakcheck();
23678 }
23679 /* Assume "got_int" was set to truncate the listing. */
23680 got_int = FALSE;
23681
23682#ifdef FEAT_BROWSE_CMD
23683 if (cmdmod.browse)
23684 {
23685 quit_more = FALSE;
23686 nr = prompt_for_number(FALSE);
23687 msg_starthere();
23688 if (nr > 0)
23689 {
23690 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23691 (long)nr);
23692
23693 if (p != NULL)
23694 {
23695 p = expand_env_save(p);
23696 eap->arg = p;
23697 eap->cmdidx = CMD_edit;
23698 cmdmod.browse = FALSE;
23699 do_exedit(eap, NULL);
23700 vim_free(p);
23701 }
23702 }
23703 }
23704#endif
23705 }
23706}
23707
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708#endif /* FEAT_EVAL */
23709
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023711#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712
23713#ifdef WIN3264
23714/*
23715 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23716 */
23717static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23718static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23719static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23720
23721/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023722 * Get the short path (8.3) for the filename in "fnamep".
23723 * Only works for a valid file name.
23724 * When the path gets longer "fnamep" is changed and the allocated buffer
23725 * is put in "bufp".
23726 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23727 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 */
23729 static int
23730get_short_pathname(fnamep, bufp, fnamelen)
23731 char_u **fnamep;
23732 char_u **bufp;
23733 int *fnamelen;
23734{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023735 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 char_u *newbuf;
23737
23738 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 l = GetShortPathName(*fnamep, *fnamep, len);
23740 if (l > len - 1)
23741 {
23742 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023743 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 newbuf = vim_strnsave(*fnamep, l);
23745 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747
23748 vim_free(*bufp);
23749 *fnamep = *bufp = newbuf;
23750
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023751 /* Really should always succeed, as the buffer is big enough. */
23752 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 }
23754
23755 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757}
23758
23759/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023760 * Get the short path (8.3) for the filename in "fname". The converted
23761 * path is returned in "bufp".
23762 *
23763 * Some of the directories specified in "fname" may not exist. This function
23764 * will shorten the existing directories at the beginning of the path and then
23765 * append the remaining non-existing path.
23766 *
23767 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023768 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769 * bufp - Pointer to an allocated buffer for the filename.
23770 * fnamelen - Length of the filename pointed to by fname
23771 *
23772 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 */
23774 static int
23775shortpath_for_invalid_fname(fname, bufp, fnamelen)
23776 char_u **fname;
23777 char_u **bufp;
23778 int *fnamelen;
23779{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023780 char_u *short_fname, *save_fname, *pbuf_unused;
23781 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023783 int old_len, len;
23784 int new_len, sfx_len;
23785 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786
23787 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023788 old_len = *fnamelen;
23789 save_fname = vim_strnsave(*fname, old_len);
23790 pbuf_unused = NULL;
23791 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023793 endp = save_fname + old_len - 1; /* Find the end of the copy */
23794 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023796 /*
23797 * Try shortening the supplied path till it succeeds by removing one
23798 * directory at a time from the tail of the path.
23799 */
23800 len = 0;
23801 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023803 /* go back one path-separator */
23804 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23805 --endp;
23806 if (endp <= save_fname)
23807 break; /* processed the complete path */
23808
23809 /*
23810 * Replace the path separator with a NUL and try to shorten the
23811 * resulting path.
23812 */
23813 ch = *endp;
23814 *endp = 0;
23815 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023816 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023817 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23818 {
23819 retval = FAIL;
23820 goto theend;
23821 }
23822 *endp = ch; /* preserve the string */
23823
23824 if (len > 0)
23825 break; /* successfully shortened the path */
23826
23827 /* failed to shorten the path. Skip the path separator */
23828 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 }
23830
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023831 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023833 /*
23834 * Succeeded in shortening the path. Now concatenate the shortened
23835 * path with the remaining path at the tail.
23836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023838 /* Compute the length of the new path. */
23839 sfx_len = (int)(save_endp - endp) + 1;
23840 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023842 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023844 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023846 /* There is not enough space in the currently allocated string,
23847 * copy it to a buffer big enough. */
23848 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023850 {
23851 retval = FAIL;
23852 goto theend;
23853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 }
23855 else
23856 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023857 /* Transfer short_fname to the main buffer (it's big enough),
23858 * unless get_short_pathname() did its work in-place. */
23859 *fname = *bufp = save_fname;
23860 if (short_fname != save_fname)
23861 vim_strncpy(save_fname, short_fname, len);
23862 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023864
23865 /* concat the not-shortened part of the path */
23866 vim_strncpy(*fname + len, endp, sfx_len);
23867 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023869
23870theend:
23871 vim_free(pbuf_unused);
23872 vim_free(save_fname);
23873
23874 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875}
23876
23877/*
23878 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023879 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 */
23881 static int
23882shortpath_for_partial(fnamep, bufp, fnamelen)
23883 char_u **fnamep;
23884 char_u **bufp;
23885 int *fnamelen;
23886{
23887 int sepcount, len, tflen;
23888 char_u *p;
23889 char_u *pbuf, *tfname;
23890 int hasTilde;
23891
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023892 /* Count up the path separators from the RHS.. so we know which part
23893 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023895 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 if (vim_ispathsep(*p))
23897 ++sepcount;
23898
23899 /* Need full path first (use expand_env() to remove a "~/") */
23900 hasTilde = (**fnamep == '~');
23901 if (hasTilde)
23902 pbuf = tfname = expand_env_save(*fnamep);
23903 else
23904 pbuf = tfname = FullName_save(*fnamep, FALSE);
23905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023906 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023908 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910
23911 if (len == 0)
23912 {
23913 /* Don't have a valid filename, so shorten the rest of the
23914 * path if we can. This CAN give us invalid 8.3 filenames, but
23915 * there's not a lot of point in guessing what it might be.
23916 */
23917 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023918 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 }
23921
23922 /* Count the paths backward to find the beginning of the desired string. */
23923 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023924 {
23925#ifdef FEAT_MBYTE
23926 if (has_mbyte)
23927 p -= mb_head_off(tfname, p);
23928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 if (vim_ispathsep(*p))
23930 {
23931 if (sepcount == 0 || (hasTilde && sepcount == 1))
23932 break;
23933 else
23934 sepcount --;
23935 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 if (hasTilde)
23938 {
23939 --p;
23940 if (p >= tfname)
23941 *p = '~';
23942 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 }
23945 else
23946 ++p;
23947
23948 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23949 vim_free(*bufp);
23950 *fnamelen = (int)STRLEN(p);
23951 *bufp = pbuf;
23952 *fnamep = p;
23953
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023954 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955}
23956#endif /* WIN3264 */
23957
23958/*
23959 * Adjust a filename, according to a string of modifiers.
23960 * *fnamep must be NUL terminated when called. When returning, the length is
23961 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023962 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 * When there is an error, *fnamep is set to NULL.
23964 */
23965 int
23966modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23967 char_u *src; /* string with modifiers */
23968 int *usedlen; /* characters after src that are used */
23969 char_u **fnamep; /* file name so far */
23970 char_u **bufp; /* buffer for allocated file name or NULL */
23971 int *fnamelen; /* length of fnamep */
23972{
23973 int valid = 0;
23974 char_u *tail;
23975 char_u *s, *p, *pbuf;
23976 char_u dirname[MAXPATHL];
23977 int c;
23978 int has_fullname = 0;
23979#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023980 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 int has_shortname = 0;
23982#endif
23983
23984repeat:
23985 /* ":p" - full path/file_name */
23986 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23987 {
23988 has_fullname = 1;
23989
23990 valid |= VALID_PATH;
23991 *usedlen += 2;
23992
23993 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23994 if ((*fnamep)[0] == '~'
23995#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23996 && ((*fnamep)[1] == '/'
23997# ifdef BACKSLASH_IN_FILENAME
23998 || (*fnamep)[1] == '\\'
23999# endif
24000 || (*fnamep)[1] == NUL)
24001
24002#endif
24003 )
24004 {
24005 *fnamep = expand_env_save(*fnamep);
24006 vim_free(*bufp); /* free any allocated file name */
24007 *bufp = *fnamep;
24008 if (*fnamep == NULL)
24009 return -1;
24010 }
24011
24012 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024013 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 {
24015 if (vim_ispathsep(*p)
24016 && p[1] == '.'
24017 && (p[2] == NUL
24018 || vim_ispathsep(p[2])
24019 || (p[2] == '.'
24020 && (p[3] == NUL || vim_ispathsep(p[3])))))
24021 break;
24022 }
24023
24024 /* FullName_save() is slow, don't use it when not needed. */
24025 if (*p != NUL || !vim_isAbsName(*fnamep))
24026 {
24027 *fnamep = FullName_save(*fnamep, *p != NUL);
24028 vim_free(*bufp); /* free any allocated file name */
24029 *bufp = *fnamep;
24030 if (*fnamep == NULL)
24031 return -1;
24032 }
24033
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024034#ifdef WIN3264
24035# if _WIN32_WINNT >= 0x0500
24036 if (vim_strchr(*fnamep, '~') != NULL)
24037 {
24038 /* Expand 8.3 filename to full path. Needed to make sure the same
24039 * file does not have two different names.
24040 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24041 p = alloc(_MAX_PATH + 1);
24042 if (p != NULL)
24043 {
24044 if (GetLongPathName(*fnamep, p, MAXPATHL))
24045 {
24046 vim_free(*bufp);
24047 *bufp = *fnamep = p;
24048 }
24049 else
24050 vim_free(p);
24051 }
24052 }
24053# endif
24054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 /* Append a path separator to a directory. */
24056 if (mch_isdir(*fnamep))
24057 {
24058 /* Make room for one or two extra characters. */
24059 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24060 vim_free(*bufp); /* free any allocated file name */
24061 *bufp = *fnamep;
24062 if (*fnamep == NULL)
24063 return -1;
24064 add_pathsep(*fnamep);
24065 }
24066 }
24067
24068 /* ":." - path relative to the current directory */
24069 /* ":~" - path relative to the home directory */
24070 /* ":8" - shortname path - postponed till after */
24071 while (src[*usedlen] == ':'
24072 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24073 {
24074 *usedlen += 2;
24075 if (c == '8')
24076 {
24077#ifdef WIN3264
24078 has_shortname = 1; /* Postpone this. */
24079#endif
24080 continue;
24081 }
24082 pbuf = NULL;
24083 /* Need full path first (use expand_env() to remove a "~/") */
24084 if (!has_fullname)
24085 {
24086 if (c == '.' && **fnamep == '~')
24087 p = pbuf = expand_env_save(*fnamep);
24088 else
24089 p = pbuf = FullName_save(*fnamep, FALSE);
24090 }
24091 else
24092 p = *fnamep;
24093
24094 has_fullname = 0;
24095
24096 if (p != NULL)
24097 {
24098 if (c == '.')
24099 {
24100 mch_dirname(dirname, MAXPATHL);
24101 s = shorten_fname(p, dirname);
24102 if (s != NULL)
24103 {
24104 *fnamep = s;
24105 if (pbuf != NULL)
24106 {
24107 vim_free(*bufp); /* free any allocated file name */
24108 *bufp = pbuf;
24109 pbuf = NULL;
24110 }
24111 }
24112 }
24113 else
24114 {
24115 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24116 /* Only replace it when it starts with '~' */
24117 if (*dirname == '~')
24118 {
24119 s = vim_strsave(dirname);
24120 if (s != NULL)
24121 {
24122 *fnamep = s;
24123 vim_free(*bufp);
24124 *bufp = s;
24125 }
24126 }
24127 }
24128 vim_free(pbuf);
24129 }
24130 }
24131
24132 tail = gettail(*fnamep);
24133 *fnamelen = (int)STRLEN(*fnamep);
24134
24135 /* ":h" - head, remove "/file_name", can be repeated */
24136 /* Don't remove the first "/" or "c:\" */
24137 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24138 {
24139 valid |= VALID_HEAD;
24140 *usedlen += 2;
24141 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024142 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024143 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 *fnamelen = (int)(tail - *fnamep);
24145#ifdef VMS
24146 if (*fnamelen > 0)
24147 *fnamelen += 1; /* the path separator is part of the path */
24148#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024149 if (*fnamelen == 0)
24150 {
24151 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24152 p = vim_strsave((char_u *)".");
24153 if (p == NULL)
24154 return -1;
24155 vim_free(*bufp);
24156 *bufp = *fnamep = tail = p;
24157 *fnamelen = 1;
24158 }
24159 else
24160 {
24161 while (tail > s && !after_pathsep(s, tail))
24162 mb_ptr_back(*fnamep, tail);
24163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165
24166 /* ":8" - shortname */
24167 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24168 {
24169 *usedlen += 2;
24170#ifdef WIN3264
24171 has_shortname = 1;
24172#endif
24173 }
24174
24175#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024176 /*
24177 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 */
24179 if (has_shortname)
24180 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024181 /* Copy the string if it is shortened by :h and when it wasn't copied
24182 * yet, because we are going to change it in place. Avoids changing
24183 * the buffer name for "%:8". */
24184 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 {
24186 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024187 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 return -1;
24189 vim_free(*bufp);
24190 *bufp = *fnamep = p;
24191 }
24192
24193 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024194 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 if (!has_fullname && !vim_isAbsName(*fnamep))
24196 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024197 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 return -1;
24199 }
24200 else
24201 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024202 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203
Bram Moolenaardc935552011-08-17 15:23:23 +020024204 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024206 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 return -1;
24208
24209 if (l == 0)
24210 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024211 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024213 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 return -1;
24215 }
24216 *fnamelen = l;
24217 }
24218 }
24219#endif /* WIN3264 */
24220
24221 /* ":t" - tail, just the basename */
24222 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24223 {
24224 *usedlen += 2;
24225 *fnamelen -= (int)(tail - *fnamep);
24226 *fnamep = tail;
24227 }
24228
24229 /* ":e" - extension, can be repeated */
24230 /* ":r" - root, without extension, can be repeated */
24231 while (src[*usedlen] == ':'
24232 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24233 {
24234 /* find a '.' in the tail:
24235 * - for second :e: before the current fname
24236 * - otherwise: The last '.'
24237 */
24238 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24239 s = *fnamep - 2;
24240 else
24241 s = *fnamep + *fnamelen - 1;
24242 for ( ; s > tail; --s)
24243 if (s[0] == '.')
24244 break;
24245 if (src[*usedlen + 1] == 'e') /* :e */
24246 {
24247 if (s > tail)
24248 {
24249 *fnamelen += (int)(*fnamep - (s + 1));
24250 *fnamep = s + 1;
24251#ifdef VMS
24252 /* cut version from the extension */
24253 s = *fnamep + *fnamelen - 1;
24254 for ( ; s > *fnamep; --s)
24255 if (s[0] == ';')
24256 break;
24257 if (s > *fnamep)
24258 *fnamelen = s - *fnamep;
24259#endif
24260 }
24261 else if (*fnamep <= tail)
24262 *fnamelen = 0;
24263 }
24264 else /* :r */
24265 {
24266 if (s > tail) /* remove one extension */
24267 *fnamelen = (int)(s - *fnamep);
24268 }
24269 *usedlen += 2;
24270 }
24271
24272 /* ":s?pat?foo?" - substitute */
24273 /* ":gs?pat?foo?" - global substitute */
24274 if (src[*usedlen] == ':'
24275 && (src[*usedlen + 1] == 's'
24276 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24277 {
24278 char_u *str;
24279 char_u *pat;
24280 char_u *sub;
24281 int sep;
24282 char_u *flags;
24283 int didit = FALSE;
24284
24285 flags = (char_u *)"";
24286 s = src + *usedlen + 2;
24287 if (src[*usedlen + 1] == 'g')
24288 {
24289 flags = (char_u *)"g";
24290 ++s;
24291 }
24292
24293 sep = *s++;
24294 if (sep)
24295 {
24296 /* find end of pattern */
24297 p = vim_strchr(s, sep);
24298 if (p != NULL)
24299 {
24300 pat = vim_strnsave(s, (int)(p - s));
24301 if (pat != NULL)
24302 {
24303 s = p + 1;
24304 /* find end of substitution */
24305 p = vim_strchr(s, sep);
24306 if (p != NULL)
24307 {
24308 sub = vim_strnsave(s, (int)(p - s));
24309 str = vim_strnsave(*fnamep, *fnamelen);
24310 if (sub != NULL && str != NULL)
24311 {
24312 *usedlen = (int)(p + 1 - src);
24313 s = do_string_sub(str, pat, sub, flags);
24314 if (s != NULL)
24315 {
24316 *fnamep = s;
24317 *fnamelen = (int)STRLEN(s);
24318 vim_free(*bufp);
24319 *bufp = s;
24320 didit = TRUE;
24321 }
24322 }
24323 vim_free(sub);
24324 vim_free(str);
24325 }
24326 vim_free(pat);
24327 }
24328 }
24329 /* after using ":s", repeat all the modifiers */
24330 if (didit)
24331 goto repeat;
24332 }
24333 }
24334
24335 return valid;
24336}
24337
24338/*
24339 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24340 * "flags" can be "g" to do a global substitute.
24341 * Returns an allocated string, NULL for error.
24342 */
24343 char_u *
24344do_string_sub(str, pat, sub, flags)
24345 char_u *str;
24346 char_u *pat;
24347 char_u *sub;
24348 char_u *flags;
24349{
24350 int sublen;
24351 regmatch_T regmatch;
24352 int i;
24353 int do_all;
24354 char_u *tail;
24355 garray_T ga;
24356 char_u *ret;
24357 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024358 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359
24360 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24361 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024362 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363
24364 ga_init2(&ga, 1, 200);
24365
24366 do_all = (flags[0] == 'g');
24367
24368 regmatch.rm_ic = p_ic;
24369 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24370 if (regmatch.regprog != NULL)
24371 {
24372 tail = str;
24373 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24374 {
24375 /*
24376 * Get some space for a temporary buffer to do the substitution
24377 * into. It will contain:
24378 * - The text up to where the match is.
24379 * - The substituted text.
24380 * - The text after the match.
24381 */
24382 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24383 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24384 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24385 {
24386 ga_clear(&ga);
24387 break;
24388 }
24389
24390 /* copy the text up to where the match is */
24391 i = (int)(regmatch.startp[0] - tail);
24392 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24393 /* add the substituted text */
24394 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24395 + ga.ga_len + i, TRUE, TRUE, FALSE);
24396 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024397 zero_width = (tail == regmatch.endp[0]
24398 || regmatch.startp[0] == regmatch.endp[0]);
24399 tail = regmatch.endp[0];
24400 if (*tail == NUL)
24401 break;
24402 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024404 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24406 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 if (!do_all)
24409 break;
24410 }
24411
24412 if (ga.ga_data != NULL)
24413 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24414
Bram Moolenaar473de612013-06-08 18:19:48 +020024415 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 }
24417
24418 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24419 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024420 if (p_cpo == empty_option)
24421 p_cpo = save_cpo;
24422 else
24423 /* Darn, evaluating {sub} expression changed the value. */
24424 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425
24426 return ret;
24427}
24428
24429#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */