blob: 8a62fa8fa2963e6b0f99fec00b178c5fe10994ca [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
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200128/* When using exists() don't auto-load a script. */
129static int no_autoload = FALSE;
130
Bram Moolenaar532c7802005-01-27 14:44:31 +0000131/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 * When recursively copying lists and dicts we need to remember which ones we
133 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 */
136static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137#define COPYID_INC 2
138#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139
140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Structure to hold info for a user function.
162 */
163typedef struct ufunc ufunc_T;
164
165struct ufunc
166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000167 int uf_varargs; /* variable nr of arguments */
168 int uf_flags;
169 int uf_calls; /* nr of active calls */
170 garray_T uf_args; /* arguments */
171 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000172#ifdef FEAT_PROFILE
173 int uf_profiling; /* TRUE when func is being profiled */
174 /* profiling the function as a whole */
175 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T uf_tm_total; /* time spent in function + children */
177 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tm_children; /* time spent in children this call */
179 /* profiling the function per line */
180 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T *uf_tml_total; /* time spent in a line + children */
182 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tml_start; /* start time for current line */
184 proftime_T uf_tml_children; /* time spent in children for this line */
185 proftime_T uf_tml_wait; /* start wait time for current line */
186 int uf_tml_idx; /* index of line being timed; -1 if none */
187 int uf_tml_execed; /* line being timed was executed */
188#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 int uf_refcount; /* for numbered function: reference count */
192 char_u uf_name[1]; /* name of function (actually longer); can
193 start with <SNR>123_ (<SNR> is K_SPECIAL
194 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195};
196
197/* function flags */
198#define FC_ABORT 1 /* abort function on error */
199#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000200#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000203 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000205static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000207/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000208static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210/* list heads for garbage collection */
211static dict_T *first_dict = NULL; /* list of all dicts */
212static list_T *first_list = NULL; /* list of all lists */
213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000214/* From user function to hashitem and back. */
215static ufunc_T dumuf;
216#define UF2HIKEY(fp) ((fp)->uf_name)
217#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
218#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
219
220#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
221#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar33570922005-01-25 22:26:29 +0000223#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
224#define VAR_SHORT_LEN 20 /* short variable name length */
225#define FIXVAR_CNT 12 /* number of fixed variables */
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000228typedef struct funccall_S funccall_T;
229
230struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 ufunc_T *func; /* function being called */
233 int linenr; /* next line to be executed */
234 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235 struct /* fixed variables for arguments */
236 {
237 dictitem_T var; /* variable (without room for name) */
238 char_u room[VAR_SHORT_LEN]; /* room for the name */
239 } fixvar[FIXVAR_CNT];
240 dict_T l_vars; /* l: local function variables */
241 dictitem_T l_vars_var; /* variable for l: scope */
242 dict_T l_avars; /* a: argument variables */
243 dictitem_T l_avars_var; /* variable for a: scope */
244 list_T l_varlist; /* list for a:000 */
245 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
246 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 linenr_T breakpoint; /* next line with breakpoint or zero */
248 int dbg_tick; /* debug_tick when breakpoint was set */
249 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000250#ifdef FEAT_PROFILE
251 proftime_T prof_child; /* time spent in a child */
252#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000253 funccall_T *caller; /* calling function or NULL */
254};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257 * Info used by a ":for" loop.
258 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000259typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260{
261 int fi_semicolon; /* TRUE if ending in '; var]' */
262 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 listwatch_T fi_lw; /* keep an eye on the item used. */
264 list_T *fi_list; /* list being used */
265} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268 * Struct used by trans_function_name()
269 */
270typedef struct
271{
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000273 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dictitem_T *fd_di; /* Dictionary item used */
275} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000276
Bram Moolenaara7043832005-01-21 11:56:39 +0000277
278/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 * Array to hold the value of v: variables.
280 * The value is in a dictitem, so that it can also be used in the v: scope.
281 * The reason to use this table anyway is for very quick access to the
282 * variables with the VV_ defines.
283 */
284#include "version.h"
285
286/* values for vv_flags: */
287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000291#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
293static struct vimvar
294{
295 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296 dictitem_T vv_di; /* value and name for key */
297 char vv_filler[16]; /* space for LONGEST name below!!! */
298 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
299} vimvars[VV_LEN] =
300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 * Initializing a union does not work, leave tv.vval empty to get zero's.
304 */
305 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("count1", VAR_NUMBER), VV_RO},
307 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
308 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
309 {VV_NAME("warningmsg", VAR_STRING), 0},
310 {VV_NAME("statusmsg", VAR_STRING), 0},
311 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
313 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("termresponse", VAR_STRING), VV_RO},
316 {VV_NAME("fname", VAR_STRING), VV_RO},
317 {VV_NAME("lang", VAR_STRING), VV_RO},
318 {VV_NAME("lc_time", VAR_STRING), VV_RO},
319 {VV_NAME("ctype", VAR_STRING), VV_RO},
320 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
322 {VV_NAME("fname_in", VAR_STRING), VV_RO},
323 {VV_NAME("fname_out", VAR_STRING), VV_RO},
324 {VV_NAME("fname_new", VAR_STRING), VV_RO},
325 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
326 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
327 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
330 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("progname", VAR_STRING), VV_RO},
332 {VV_NAME("servername", VAR_STRING), VV_RO},
333 {VV_NAME("dying", VAR_NUMBER), VV_RO},
334 {VV_NAME("exception", VAR_STRING), VV_RO},
335 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
336 {VV_NAME("register", VAR_STRING), VV_RO},
337 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
338 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000339 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
340 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000341 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000342 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
343 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000344 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000349 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000350 {VV_NAME("swapname", VAR_STRING), VV_RO},
351 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000352 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200353 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000354 {VV_NAME("mouse_win", VAR_NUMBER), 0},
355 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
356 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000357 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100359 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000360 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200361 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000362};
363
364/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000365#define vv_type vv_di.di_tv.v_type
366#define vv_nr vv_di.di_tv.vval.v_number
367#define vv_float vv_di.di_tv.vval.v_float
368#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000369#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000370#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000371
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200372static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000373#define vimvarht vimvardict.dv_hashtab
374
Bram Moolenaara40058a2005-07-11 22:42:07 +0000375static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
376static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
378static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
379static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000380static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
381static void list_glob_vars __ARGS((int *first));
382static void list_buf_vars __ARGS((int *first));
383static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000384#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000385static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_vim_vars __ARGS((int *first));
388static void list_script_vars __ARGS((int *first));
389static void list_func_vars __ARGS((int *first));
390static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000391static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
392static int check_changedtick __ARGS((char_u *arg));
393static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
394static void clear_lval __ARGS((lval_T *lp));
395static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
396static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
398static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
399static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
400static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
401static void item_lock __ARGS((typval_T *tv, int deep, int lock));
402static int tv_islocked __ARGS((typval_T *tv));
403
Bram Moolenaar33570922005-01-25 22:26:29 +0000404static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
405static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000410static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
411static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000412
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000413static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000414static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000418static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000419static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100420static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
421static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
422static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000423static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000424static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000425static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
427static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000429static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100430static 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 +0000431static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000432static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200433static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
435static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000443#ifdef FEAT_FLOAT
444static int string2float __ARGS((char_u *text, float_T *value));
445#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
447static int find_internal_func __ARGS((char_u *name));
448static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
449static 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 +0200450static 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 +0000451static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000452static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200456static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100459static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200465static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100478static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000479static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
483static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
484#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000485static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000488static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000490#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000491static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
498static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200499static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000501static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
504static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200515#ifdef FEAT_FLOAT
516static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
517#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000520static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000521static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
527static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200529static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000531static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000532static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000540static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000542static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000548static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000556static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000557static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000558static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000559static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200562static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000563static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000571static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000585static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100590static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000592static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000604#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200605static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200608#ifdef FEAT_LUA
609static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
610#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000611static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000615static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000616static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000619static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000623#ifdef vim_mkdir
624static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100627#ifdef FEAT_MZSCHEME
628static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100632static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
635static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000638static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000639static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200640#ifdef FEAT_PYTHON3
641static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
643#ifdef FEAT_PYTHON
644static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
645#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000647static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000648static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#ifdef FEAT_FLOAT
661static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
662#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200663static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100665static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000668static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000670static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000677static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000678static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000679static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000680static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200682static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000683static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100685#ifdef FEAT_CRYPT
686static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
687#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000688static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200689static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
692static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200693static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000696static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000697static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
701static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
703#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000704static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200705static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000706#ifdef HAVE_STRFTIME
707static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
709static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200715static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000722static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200723static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000725static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000726static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000728static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000729static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000731static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200732#ifdef FEAT_FLOAT
733static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
740static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200743static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200744static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100748static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000755static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000758static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100759static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000761static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000762static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int get_env_len __ARGS((char_u **arg));
764static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
767#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
768#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
769 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000770static 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 +0000771static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
774static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static typval_T *alloc_tv __ARGS((void));
776static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void init_tv __ARGS((typval_T *varp));
778static long get_tv_number __ARGS((typval_T *varp));
779static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000780static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static char_u *get_tv_string __ARGS((typval_T *varp));
782static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200785static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
787static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
788static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000789static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
790static 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 +0000791static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
792static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000793static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200794static int var_check_func_name __ARGS((char_u *name, int new_var));
795static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000796static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000797static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000798static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
799static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
800static int eval_fname_script __ARGS((char_u *p));
801static int eval_fname_sid __ARGS((char_u *p));
802static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static ufunc_T *find_func __ARGS((char_u *name));
804static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000805static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_PROFILE
807static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000808static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
809static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
810static int
811# ifdef __BORLANDC__
812 _RTLENTRYF
813# endif
814 prof_total_cmp __ARGS((const void *s1, const void *s2));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200821static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000822static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000823static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static 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 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200850 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
851 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200852 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000853 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000854 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000855
856 for (i = 0; i < VV_LEN; ++i)
857 {
858 p = &vimvars[i];
859 STRCPY(p->vv_di.di_key, p->vv_name);
860 if (p->vv_flags & VV_RO)
861 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
862 else if (p->vv_flags & VV_RO_SBX)
863 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
864 else
865 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000866
867 /* add to v: scope dict, unless the value is not always available */
868 if (p->vv_type != VAR_UNKNOWN)
869 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000871 /* add to compat scope dict */
872 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000874 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100875 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200876 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877
878#ifdef EBCDIC
879 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100880 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200881 */
882 sortFunctions();
883#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000884}
885
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000886#if defined(EXITFREE) || defined(PROTO)
887 void
888eval_clear()
889{
890 int i;
891 struct vimvar *p;
892
893 for (i = 0; i < VV_LEN; ++i)
894 {
895 p = &vimvars[i];
896 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000897 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000898 vim_free(p->vv_str);
899 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000900 }
901 else if (p->vv_di.di_tv.v_type == VAR_LIST)
902 {
903 list_unref(p->vv_list);
904 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906 }
907 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000908 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000909 hash_clear(&compat_hashtab);
910
Bram Moolenaard9fba312005-06-26 22:34:35 +0000911 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100912# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200913 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915
916 /* global variables */
917 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000918
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000919 /* autoloaded script names */
920 ga_clear_strings(&ga_loaded);
921
Bram Moolenaarcca74132013-09-25 21:00:28 +0200922 /* Script-local variables. First clear all the variables and in a second
923 * loop free the scriptvar_T, because a variable in one script might hold
924 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200925 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200926 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200929 ga_clear(&ga_scripts);
930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000931 /* unreferenced lists and dicts */
932 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000933
934 /* functions */
935 free_all_functions();
936 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000937}
938#endif
939
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 * Return the name of the executed function.
942 */
943 char_u *
944func_name(cookie)
945 void *cookie;
946{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000947 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the next breakpoint line for a funccall cookie.
952 */
953 linenr_T *
954func_breakpoint(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the address holding the debug tick for a funccall cookie.
962 */
963 int *
964func_dbg_tick(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Return the nesting level for a funccall cookie.
972 */
973 int
974func_level(cookie)
975 void *cookie;
976{
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979
980/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000981funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000983/* pointer to list of previously used funccal, still around because some
984 * item in it is still being used. */
985funccall_T *previous_funccal = NULL;
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987/*
988 * Return TRUE when a function was ended by a ":return" command.
989 */
990 int
991current_func_returned()
992{
993 return current_funccal->returned;
994}
995
996
997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * Set an internal variable to a string value. Creates the variable if it does
999 * not already exist.
1000 */
1001 void
1002set_internal_string_var(name, value)
1003 char_u *name;
1004 char_u *value;
1005{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001006 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001007 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009 val = vim_strsave(value);
1010 if (val != NULL)
1011 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018 }
1019}
1020
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001022static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static char_u *redir_endp = NULL;
1024static char_u *redir_varname = NULL;
1025
1026/*
1027 * Start recording command output to a variable
1028 * Returns OK if successfully completed the setup. FAIL otherwise.
1029 */
1030 int
1031var_redir_start(name, append)
1032 char_u *name;
1033 int append; /* append to an existing variable */
1034{
1035 int save_emsg;
1036 int err;
1037 typval_T tv;
1038
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001039 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001040 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 {
1042 EMSG(_(e_invarg));
1043 return FAIL;
1044 }
1045
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001046 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 redir_varname = vim_strsave(name);
1048 if (redir_varname == NULL)
1049 return FAIL;
1050
1051 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1052 if (redir_lval == NULL)
1053 {
1054 var_redir_stop();
1055 return FAIL;
1056 }
1057
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001058 /* The output is stored in growarray "redir_ga" until redirection ends. */
1059 ga_init2(&redir_ga, (int)sizeof(char), 500);
1060
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001062 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1063 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1065 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001066 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 if (redir_endp != NULL && *redir_endp != NUL)
1068 /* Trailing characters are present after the variable name */
1069 EMSG(_(e_trailing));
1070 else
1071 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001072 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 var_redir_stop();
1074 return FAIL;
1075 }
1076
1077 /* check if we can write to the variable: set it to or append an empty
1078 * string */
1079 save_emsg = did_emsg;
1080 did_emsg = FALSE;
1081 tv.v_type = VAR_STRING;
1082 tv.vval.v_string = (char_u *)"";
1083 if (append)
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1085 else
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001087 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001089 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 if (err)
1091 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001092 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 var_redir_stop();
1094 return FAIL;
1095 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001101 * Append "value[value_len]" to the variable set by var_redir_start().
1102 * The actual appending is postponed until redirection ends, because the value
1103 * appended may in fact be the string we write to, changing it may cause freed
1104 * memory to be used:
1105 * :redir => foo
1106 * :let foo
1107 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 */
1109 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115
1116 if (redir_lval == NULL)
1117 return;
1118
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 {
1126 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 }
1129 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131}
1132
1133/*
1134 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001135 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 */
1137 void
1138var_redir_stop()
1139{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 typval_T tv;
1141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 if (redir_lval != NULL)
1143 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 /* If there was no error: assign the text to the variable. */
1145 if (redir_endp != NULL)
1146 {
1147 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1148 tv.v_type = VAR_STRING;
1149 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001150 /* Call get_lval() again, if it's inside a Dict or List it may
1151 * have changed. */
1152 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1153 FALSE, FALSE, FALSE, FNE_CHECK_START);
1154 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1155 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1156 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* free the collected output */
1160 vim_free(redir_ga.ga_data);
1161 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 vim_free(redir_lval);
1164 redir_lval = NULL;
1165 }
1166 vim_free(redir_varname);
1167 redir_varname = NULL;
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170# if defined(FEAT_MBYTE) || defined(PROTO)
1171 int
1172eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1173 char_u *enc_from;
1174 char_u *enc_to;
1175 char_u *fname_from;
1176 char_u *fname_to;
1177{
1178 int err = FALSE;
1179
1180 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1181 set_vim_var_string(VV_CC_TO, enc_to, -1);
1182 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1183 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1184 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1185 err = TRUE;
1186 set_vim_var_string(VV_CC_FROM, NULL, -1);
1187 set_vim_var_string(VV_CC_TO, NULL, -1);
1188 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1189 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1190
1191 if (err)
1192 return FAIL;
1193 return OK;
1194}
1195# endif
1196
1197# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1198 int
1199eval_printexpr(fname, args)
1200 char_u *fname;
1201 char_u *args;
1202{
1203 int err = FALSE;
1204
1205 set_vim_var_string(VV_FNAME_IN, fname, -1);
1206 set_vim_var_string(VV_CMDARG, args, -1);
1207 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1208 err = TRUE;
1209 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1210 set_vim_var_string(VV_CMDARG, NULL, -1);
1211
1212 if (err)
1213 {
1214 mch_remove(fname);
1215 return FAIL;
1216 }
1217 return OK;
1218}
1219# endif
1220
1221# if defined(FEAT_DIFF) || defined(PROTO)
1222 void
1223eval_diff(origfile, newfile, outfile)
1224 char_u *origfile;
1225 char_u *newfile;
1226 char_u *outfile;
1227{
1228 int err = FALSE;
1229
1230 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1231 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1232 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1233 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1234 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1235 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1236 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1237}
1238
1239 void
1240eval_patch(origfile, difffile, outfile)
1241 char_u *origfile;
1242 char_u *difffile;
1243 char_u *outfile;
1244{
1245 int err;
1246
1247 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1249 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1250 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1251 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1252 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1253 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1254}
1255# endif
1256
1257/*
1258 * Top level evaluation function, returning a boolean.
1259 * Sets "error" to TRUE if there was an error.
1260 * Return TRUE or FALSE.
1261 */
1262 int
1263eval_to_bool(arg, error, nextcmd, skip)
1264 char_u *arg;
1265 int *error;
1266 char_u **nextcmd;
1267 int skip; /* only parse, don't execute */
1268{
Bram Moolenaar33570922005-01-25 22:26:29 +00001269 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 int retval = FALSE;
1271
1272 if (skip)
1273 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001274 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 else
1277 {
1278 *error = FALSE;
1279 if (!skip)
1280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001281 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 }
1285 if (skip)
1286 --emsg_skip;
1287
1288 return retval;
1289}
1290
1291/*
1292 * Top level evaluation function, returning a string. If "skip" is TRUE,
1293 * only parsing to "nextcmd" is done, without reporting errors. Return
1294 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1295 */
1296 char_u *
1297eval_to_string_skip(arg, nextcmd, skip)
1298 char_u *arg;
1299 char_u **nextcmd;
1300 int skip; /* only parse, don't execute */
1301{
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 char_u *retval;
1304
1305 if (skip)
1306 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 retval = NULL;
1309 else
1310 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 retval = vim_strsave(get_tv_string(&tv));
1312 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 if (skip)
1315 --emsg_skip;
1316
1317 return retval;
1318}
1319
1320/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001321 * Skip over an expression at "*pp".
1322 * Return FAIL for an error, OK otherwise.
1323 */
1324 int
1325skip_expr(pp)
1326 char_u **pp;
1327{
Bram Moolenaar33570922005-01-25 22:26:29 +00001328 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329
1330 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001331 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001332}
1333
1334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336 * When "convert" is TRUE convert a List into a sequence of lines and convert
1337 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * Return pointer to allocated memory, or NULL for failure.
1339 */
1340 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *arg;
1343 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
Bram Moolenaar33570922005-01-25 22:26:29 +00001346 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001353 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 retval = NULL;
1355 else
1356 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001357 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001358 {
1359 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001360 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001361 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 if (tv.vval.v_list->lv_len > 0)
1364 ga_append(&ga, NL);
1365 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 ga_append(&ga, NUL);
1367 retval = (char_u *)ga.ga_data;
1368 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001369#ifdef FEAT_FLOAT
1370 else if (convert && tv.v_type == VAR_FLOAT)
1371 {
1372 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1373 retval = vim_strsave(numbuf);
1374 }
1375#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001376 else
1377 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 * Call eval_to_string() without using current local variables and using
1386 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 */
1388 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001389eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 char_u *arg;
1391 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 char_u *retval;
1395 void *save_funccalp;
1396
1397 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 ++sandbox;
1400 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001401 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001402 if (use_sandbox)
1403 --sandbox;
1404 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 restore_funccal(save_funccalp);
1406 return retval;
1407}
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409/*
1410 * Top level evaluation function, returning a number.
1411 * Evaluates "expr" silently.
1412 * Returns -1 for an error.
1413 */
1414 int
1415eval_to_number(expr)
1416 char_u *expr;
1417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001420 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 ++emsg_off;
1423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = -1;
1426 else
1427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001428 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 --emsg_off;
1432
1433 return retval;
1434}
1435
Bram Moolenaara40058a2005-07-11 22:42:07 +00001436/*
1437 * Prepare v: variable "idx" to be used.
1438 * Save the current typeval in "save_tv".
1439 * When not used yet add the variable to the v: hashtable.
1440 */
1441 static void
1442prepare_vimvar(idx, save_tv)
1443 int idx;
1444 typval_T *save_tv;
1445{
1446 *save_tv = vimvars[idx].vv_tv;
1447 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1448 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1449}
1450
1451/*
1452 * Restore v: variable "idx" to typeval "save_tv".
1453 * When no longer defined, remove the variable from the v: hashtable.
1454 */
1455 static void
1456restore_vimvar(idx, save_tv)
1457 int idx;
1458 typval_T *save_tv;
1459{
1460 hashitem_T *hi;
1461
Bram Moolenaara40058a2005-07-11 22:42:07 +00001462 vimvars[idx].vv_tv = *save_tv;
1463 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1464 {
1465 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1466 if (HASHITEM_EMPTY(hi))
1467 EMSG2(_(e_intern2), "restore_vimvar()");
1468 else
1469 hash_remove(&vimvarht, hi);
1470 }
1471}
1472
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001473#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474/*
1475 * Evaluate an expression to a list with suggestions.
1476 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001477 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001478 */
1479 list_T *
1480eval_spell_expr(badword, expr)
1481 char_u *badword;
1482 char_u *expr;
1483{
1484 typval_T save_val;
1485 typval_T rettv;
1486 list_T *list = NULL;
1487 char_u *p = skipwhite(expr);
1488
1489 /* Set "v:val" to the bad word. */
1490 prepare_vimvar(VV_VAL, &save_val);
1491 vimvars[VV_VAL].vv_type = VAR_STRING;
1492 vimvars[VV_VAL].vv_str = badword;
1493 if (p_verbose == 0)
1494 ++emsg_off;
1495
1496 if (eval1(&p, &rettv, TRUE) == OK)
1497 {
1498 if (rettv.v_type != VAR_LIST)
1499 clear_tv(&rettv);
1500 else
1501 list = rettv.vval.v_list;
1502 }
1503
1504 if (p_verbose == 0)
1505 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001506 restore_vimvar(VV_VAL, &save_val);
1507
1508 return list;
1509}
1510
1511/*
1512 * "list" is supposed to contain two items: a word and a number. Return the
1513 * word in "pp" and the number as the return value.
1514 * Return -1 if anything isn't right.
1515 * Used to get the good word and score from the eval_spell_expr() result.
1516 */
1517 int
1518get_spellword(list, pp)
1519 list_T *list;
1520 char_u **pp;
1521{
1522 listitem_T *li;
1523
1524 li = list->lv_first;
1525 if (li == NULL)
1526 return -1;
1527 *pp = get_tv_string(&li->li_tv);
1528
1529 li = li->li_next;
1530 if (li == NULL)
1531 return -1;
1532 return get_tv_number(&li->li_tv);
1533}
1534#endif
1535
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001537 * Top level evaluation function.
1538 * Returns an allocated typval_T with the result.
1539 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 */
1541 typval_T *
1542eval_expr(arg, nextcmd)
1543 char_u *arg;
1544 char_u **nextcmd;
1545{
1546 typval_T *tv;
1547
1548 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001549 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001550 {
1551 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001553 }
1554
1555 return tv;
1556}
1557
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001561 * Uses argv[argc] for the function arguments. Only Number and String
1562 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001563 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001565 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001566call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 char_u *func;
1568 int argc;
1569 char_u **argv;
1570 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001571 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
Bram Moolenaar33570922005-01-25 22:26:29 +00001574 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 long n;
1576 int len;
1577 int i;
1578 int doesrange;
1579 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001582 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 for (i = 0; i < argc; i++)
1587 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 /* Pass a NULL or empty argument as an empty string */
1589 if (argv[i] == NULL || *argv[i] == NUL)
1590 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001591 argvars[i].v_type = VAR_STRING;
1592 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 continue;
1594 }
1595
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001596 if (str_arg_only)
1597 len = 0;
1598 else
1599 /* Recognize a number argument, the others must be strings. */
1600 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (len != 0 && len == (int)STRLEN(argv[i]))
1602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001603 argvars[i].v_type = VAR_NUMBER;
1604 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001608 argvars[i].v_type = VAR_STRING;
1609 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 }
1612
1613 if (safe)
1614 {
1615 save_funccalp = save_funccal();
1616 ++sandbox;
1617 }
1618
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1620 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001622 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (safe)
1624 {
1625 --sandbox;
1626 restore_funccal(save_funccalp);
1627 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 vim_free(argvars);
1629
1630 if (ret == FAIL)
1631 clear_tv(rettv);
1632
1633 return ret;
1634}
1635
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001636/*
1637 * Call vimL function "func" and return the result as a number.
1638 * Returns -1 when calling the function fails.
1639 * Uses argv[argc] for the function arguments.
1640 */
1641 long
1642call_func_retnr(func, argc, argv, safe)
1643 char_u *func;
1644 int argc;
1645 char_u **argv;
1646 int safe; /* use the sandbox */
1647{
1648 typval_T rettv;
1649 long retval;
1650
1651 /* All arguments are passed as strings, no conversion to number. */
1652 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1653 return -1;
1654
1655 retval = get_tv_number_chk(&rettv, NULL);
1656 clear_tv(&rettv);
1657 return retval;
1658}
1659
1660#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1661 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1662
Bram Moolenaar4f688582007-07-24 12:34:30 +00001663# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001664/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001665 * Call vimL function "func" and return the result as a string.
1666 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 * Uses argv[argc] for the function arguments.
1668 */
1669 void *
1670call_func_retstr(func, argc, argv, safe)
1671 char_u *func;
1672 int argc;
1673 char_u **argv;
1674 int safe; /* use the sandbox */
1675{
1676 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001679 /* All arguments are passed as strings, no conversion to number. */
1680 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 return NULL;
1682
1683 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 return retval;
1686}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001687# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001689/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001690 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 */
1694 void *
1695call_func_retlist(func, argc, argv, safe)
1696 char_u *func;
1697 int argc;
1698 char_u **argv;
1699 int safe; /* use the sandbox */
1700{
1701 typval_T rettv;
1702
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 return NULL;
1706
1707 if (rettv.v_type != VAR_LIST)
1708 {
1709 clear_tv(&rettv);
1710 return NULL;
1711 }
1712
1713 return rettv.vval.v_list;
1714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
1717/*
1718 * Save the current function call pointer, and set it to NULL.
1719 * Used when executing autocommands and for ":source".
1720 */
1721 void *
1722save_funccal()
1723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 current_funccal = NULL;
1727 return (void *)fc;
1728}
1729
1730 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001731restore_funccal(vfc)
1732 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734 funccall_T *fc = (funccall_T *)vfc;
1735
1736 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737}
1738
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739#if defined(FEAT_PROFILE) || defined(PROTO)
1740/*
1741 * Prepare profiling for entering a child or something else that is not
1742 * counted for the script/function itself.
1743 * Should always be called in pair with prof_child_exit().
1744 */
1745 void
1746prof_child_enter(tm)
1747 proftime_T *tm; /* place to store waittime */
1748{
1749 funccall_T *fc = current_funccal;
1750
1751 if (fc != NULL && fc->func->uf_profiling)
1752 profile_start(&fc->prof_child);
1753 script_prof_save(tm);
1754}
1755
1756/*
1757 * Take care of time spent in a child.
1758 * Should always be called after prof_child_enter().
1759 */
1760 void
1761prof_child_exit(tm)
1762 proftime_T *tm; /* where waittime was stored */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 {
1768 profile_end(&fc->prof_child);
1769 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1770 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1771 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1772 }
1773 script_prof_restore(tm);
1774}
1775#endif
1776
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#ifdef FEAT_FOLDING
1779/*
1780 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1781 * it in "*cp". Doesn't give error messages.
1782 */
1783 int
1784eval_foldexpr(arg, cp)
1785 char_u *arg;
1786 int *cp;
1787{
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 int retval;
1790 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001791 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1792 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001795 if (use_sandbox)
1796 ++sandbox;
1797 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 retval = 0;
1801 else
1802 {
1803 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 if (tv.v_type == VAR_NUMBER)
1805 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001806 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 retval = 0;
1808 else
1809 {
1810 /* If the result is a string, check if there is a non-digit before
1811 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (!VIM_ISDIGIT(*s) && *s != '-')
1814 *cp = *s++;
1815 retval = atol((char *)s);
1816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 }
1819 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001820 if (use_sandbox)
1821 --sandbox;
1822 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 return retval;
1825}
1826#endif
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001829 * ":let" list all variable values
1830 * ":let var1 var2" list variable values
1831 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001832 * ":let var += expr" assignment command.
1833 * ":let var -= expr" assignment command.
1834 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 void
1838ex_let(eap)
1839 exarg_T *eap;
1840{
1841 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001843 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845 int var_count = 0;
1846 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001848 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001849 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 argend = skip_var_list(arg, &var_count, &semicolon);
1852 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001854 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1855 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001859 /*
1860 * ":let" without "=": list variables
1861 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 if (*arg == '[')
1863 EMSG(_(e_invarg));
1864 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_glob_vars(&first);
1871 list_buf_vars(&first);
1872 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001873#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_script_vars(&first);
1877 list_func_vars(&first);
1878 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 eap->nextcmd = check_nextcmd(arg);
1881 }
1882 else
1883 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 op[0] = '=';
1885 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001886 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 {
1888 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1889 op[0] = expr[-1]; /* +=, -= or .= */
1890 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (eap->skip)
1894 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001895 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (eap->skip)
1897 {
1898 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 --emsg_skip;
1901 }
1902 else if (i != FAIL)
1903 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 }
1909}
1910
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911/*
1912 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1913 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 * When "nextchars" is not NULL it points to a string with characters that
1915 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1916 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 * Returns OK or FAIL;
1918 */
1919 static int
1920ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1921 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 int copy; /* copy values from "tv", don't move */
1924 int semicolon; /* from skip_var_list() */
1925 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927{
1928 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 listitem_T *item;
1932 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933
1934 if (*arg != '[')
1935 {
1936 /*
1937 * ":let var = expr" or ":for var in list"
1938 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940 return FAIL;
1941 return OK;
1942 }
1943
1944 /*
1945 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1946 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001947 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 {
1949 EMSG(_(e_listreq));
1950 return FAIL;
1951 }
1952
1953 i = list_len(l);
1954 if (semicolon == 0 && var_count < i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959 if (var_count - semicolon > i)
1960 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001961 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962 return FAIL;
1963 }
1964
1965 item = l->lv_first;
1966 while (*arg != ']')
1967 {
1968 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 item = item->li_next;
1971 if (arg == NULL)
1972 return FAIL;
1973
1974 arg = skipwhite(arg);
1975 if (*arg == ';')
1976 {
1977 /* Put the rest of the list (may be empty) in the var after ';'.
1978 * Create a new list for this. */
1979 l = list_alloc();
1980 if (l == NULL)
1981 return FAIL;
1982 while (item != NULL)
1983 {
1984 list_append_tv(l, &item->li_tv);
1985 item = item->li_next;
1986 }
1987
1988 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001989 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 ltv.vval.v_list = l;
1991 l->lv_refcount = 1;
1992
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1994 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 clear_tv(&ltv);
1996 if (arg == NULL)
1997 return FAIL;
1998 break;
1999 }
2000 else if (*arg != ',' && *arg != ']')
2001 {
2002 EMSG2(_(e_intern2), "ex_let_vars()");
2003 return FAIL;
2004 }
2005 }
2006
2007 return OK;
2008}
2009
2010/*
2011 * Skip over assignable variable "var" or list of variables "[var, var]".
2012 * Used for ":let varvar = expr" and ":for varvar in expr".
2013 * For "[var, var]" increment "*var_count" for each variable.
2014 * for "[var, var; var]" set "semicolon".
2015 * Return NULL for an error.
2016 */
2017 static char_u *
2018skip_var_list(arg, var_count, semicolon)
2019 char_u *arg;
2020 int *var_count;
2021 int *semicolon;
2022{
2023 char_u *p, *s;
2024
2025 if (*arg == '[')
2026 {
2027 /* "[var, var]": find the matching ']'. */
2028 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002029 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 {
2031 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2032 s = skip_var_one(p);
2033 if (s == p)
2034 {
2035 EMSG2(_(e_invarg2), p);
2036 return NULL;
2037 }
2038 ++*var_count;
2039
2040 p = skipwhite(s);
2041 if (*p == ']')
2042 break;
2043 else if (*p == ';')
2044 {
2045 if (*semicolon == 1)
2046 {
2047 EMSG(_("Double ; in list of variables"));
2048 return NULL;
2049 }
2050 *semicolon = 1;
2051 }
2052 else if (*p != ',')
2053 {
2054 EMSG2(_(e_invarg2), p);
2055 return NULL;
2056 }
2057 }
2058 return p + 1;
2059 }
2060 else
2061 return skip_var_one(arg);
2062}
2063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002064/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002065 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002066 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002067 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002068 static char_u *
2069skip_var_one(arg)
2070 char_u *arg;
2071{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002072 if (*arg == '@' && arg[1] != NUL)
2073 return arg + 2;
2074 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2075 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076}
2077
Bram Moolenaara7043832005-01-21 11:56:39 +00002078/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 * List variables for hashtab "ht" with prefix "prefix".
2080 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002082 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002087 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002088{
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 hashitem_T *hi;
2090 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002091 int todo;
2092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002094 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2095 {
2096 if (!HASHITEM_EMPTY(hi))
2097 {
2098 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002099 di = HI2DI(hi);
2100 if (empty || di->di_tv.v_type != VAR_STRING
2101 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 }
2104 }
2105}
2106
2107/*
2108 * List global variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_glob_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002115}
2116
2117/*
2118 * List buffer variables.
2119 */
2120 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002121list_buf_vars(first)
2122 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002123{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 char_u numbuf[NUMBUFLEN];
2125
Bram Moolenaar429fa852013-04-15 12:27:36 +02002126 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128
2129 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2131 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
2134/*
2135 * List window variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_win_vars(first)
2139 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002141 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145#ifdef FEAT_WINDOWS
2146/*
2147 * List tab page variables.
2148 */
2149 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150list_tab_vars(first)
2151 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002152{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002153 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002155}
2156#endif
2157
Bram Moolenaara7043832005-01-21 11:56:39 +00002158/*
2159 * List Vim variables.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_vim_vars(first)
2163 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166}
2167
2168/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169 * List script-local variables, if there is a script.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_script_vars(first)
2173 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174{
2175 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2177 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178}
2179
2180/*
2181 * List function variables, if there is a function.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_func_vars(first)
2185 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186{
2187 if (current_funccal != NULL)
2188 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190}
2191
2192/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 * List variables in "arg".
2194 */
2195 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 exarg_T *eap;
2198 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002199 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200{
2201 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 char_u *name_start;
2205 char_u *arg_subsc;
2206 char_u *tofree;
2207 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208
2209 while (!ends_excmd(*arg) && !got_int)
2210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002213 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2215 {
2216 emsg_severe = TRUE;
2217 EMSG(_(e_trailing));
2218 break;
2219 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* get_name_len() takes care of expanding curly braces */
2224 name_start = name = arg;
2225 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2226 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 /* This is mainly to keep test 49 working: when expanding
2229 * curly braces fails overrule the exception error message. */
2230 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 emsg_severe = TRUE;
2233 EMSG2(_(e_invarg2), arg);
2234 break;
2235 }
2236 error = TRUE;
2237 }
2238 else
2239 {
2240 if (tofree != NULL)
2241 name = tofree;
2242 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 else
2245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* handle d.key, l[idx], f(expr) */
2247 arg_subsc = arg;
2248 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 'g': list_glob_vars(first); break;
2257 case 'b': list_buf_vars(first); break;
2258 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 'v': list_vim_vars(first); break;
2263 case 's': list_script_vars(first); break;
2264 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 default:
2266 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002268 }
2269 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 {
2271 char_u numbuf[NUMBUFLEN];
2272 char_u *tf;
2273 int c;
2274 char_u *s;
2275
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002276 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 c = *arg;
2278 *arg = NUL;
2279 list_one_var_a((char_u *)"",
2280 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002281 tv.v_type,
2282 s == NULL ? (char_u *)"" : s,
2283 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 *arg = c;
2285 vim_free(tf);
2286 }
2287 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002288 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
2290 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291
2292 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294
2295 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 }
2297
2298 return arg;
2299}
2300
2301/*
2302 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2303 * Returns a pointer to the char just after the var name.
2304 * Returns NULL if there is an error.
2305 */
2306 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002309 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002311 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002312 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313{
2314 int c1;
2315 char_u *name;
2316 char_u *p;
2317 char_u *arg_end = NULL;
2318 int len;
2319 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321
2322 /*
2323 * ":let $VAR = expr": Set environment variable.
2324 */
2325 if (*arg == '$')
2326 {
2327 /* Find the end of the name. */
2328 ++arg;
2329 name = arg;
2330 len = get_env_len(&arg);
2331 if (len == 0)
2332 EMSG2(_(e_invarg2), name - 1);
2333 else
2334 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 if (op != NULL && (*op == '+' || *op == '-'))
2336 EMSG2(_(e_letwrong), op);
2337 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002338 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002340 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 {
2342 c1 = name[len];
2343 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344 p = get_tv_string_chk(tv);
2345 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 {
2347 int mustfree = FALSE;
2348 char_u *s = vim_getenv(name, &mustfree);
2349
2350 if (s != NULL)
2351 {
2352 p = tofree = concat_str(s, p);
2353 if (mustfree)
2354 vim_free(s);
2355 }
2356 }
2357 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002358 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 if (STRICMP(name, "HOME") == 0)
2361 init_homedir();
2362 else if (didset_vim && STRICMP(name, "VIM") == 0)
2363 didset_vim = FALSE;
2364 else if (didset_vimruntime
2365 && STRICMP(name, "VIMRUNTIME") == 0)
2366 didset_vimruntime = FALSE;
2367 arg_end = arg;
2368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 }
2372 }
2373 }
2374
2375 /*
2376 * ":let &option = expr": Set option value.
2377 * ":let &l:option = expr": Set local option value.
2378 * ":let &g:option = expr": Set global option value.
2379 */
2380 else if (*arg == '&')
2381 {
2382 /* Find the end of the name. */
2383 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 if (p == NULL || (endchars != NULL
2385 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 EMSG(_(e_letunexp));
2387 else
2388 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 long n;
2390 int opt_type;
2391 long numval;
2392 char_u *stringval = NULL;
2393 char_u *s;
2394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395 c1 = *p;
2396 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397
2398 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002399 s = get_tv_string_chk(tv); /* != NULL if number or string */
2400 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 {
2402 opt_type = get_option_value(arg, &numval,
2403 &stringval, opt_flags);
2404 if ((opt_type == 1 && *op == '.')
2405 || (opt_type == 0 && *op != '.'))
2406 EMSG2(_(e_letwrong), op);
2407 else
2408 {
2409 if (opt_type == 1) /* number */
2410 {
2411 if (*op == '+')
2412 n = numval + n;
2413 else
2414 n = numval - n;
2415 }
2416 else if (opt_type == 0 && stringval != NULL) /* string */
2417 {
2418 s = concat_str(stringval, s);
2419 vim_free(stringval);
2420 stringval = s;
2421 }
2422 }
2423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 if (s != NULL)
2425 {
2426 set_option_value(arg, n, s, opt_flags);
2427 arg_end = p;
2428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
2432 }
2433
2434 /*
2435 * ":let @r = expr": Set register contents.
2436 */
2437 else if (*arg == '@')
2438 {
2439 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 if (op != NULL && (*op == '+' || *op == '-'))
2441 EMSG2(_(e_letwrong), op);
2442 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002443 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 EMSG(_(e_letunexp));
2445 else
2446 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002447 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 char_u *s;
2449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 p = get_tv_string_chk(tv);
2451 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002453 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 if (s != NULL)
2455 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002456 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 vim_free(s);
2458 }
2459 }
2460 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 arg_end = arg + 1;
2464 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
2467 }
2468
2469 /*
2470 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002471 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002473 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002475 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002477 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2481 EMSG(_(e_letunexp));
2482 else
2483 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 arg_end = p;
2486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
2490
2491 else
2492 EMSG2(_(e_invarg2), arg);
2493
2494 return arg_end;
2495}
2496
2497/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2499 */
2500 static int
2501check_changedtick(arg)
2502 char_u *arg;
2503{
2504 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2505 {
2506 EMSG2(_(e_readonlyvar), arg);
2507 return TRUE;
2508 }
2509 return FALSE;
2510}
2511
2512/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Get an lval: variable, Dict item or List item that can be assigned a value
2514 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2515 * "name.key", "name.key[expr]" etc.
2516 * Indexing only works if "name" is an existing List or Dictionary.
2517 * "name" points to the start of the name.
2518 * If "rettv" is not NULL it points to the value to be assigned.
2519 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2520 * wrong; must end in space or cmd separator.
2521 *
2522 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002523 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 */
2526 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002527get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 typval_T *rettv;
2530 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int unlet;
2532 int skip;
2533 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002534 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 char_u *expr_start, *expr_end;
2538 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 dictitem_T *v;
2540 typval_T var1;
2541 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002543 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550
2551 if (skip)
2552 {
2553 /* When skipping just find the end of the name. */
2554 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 }
2557
2558 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002559 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (expr_start != NULL)
2561 {
2562 /* Don't expand the name when we already know there is an error. */
2563 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2564 && *p != '[' && *p != '.')
2565 {
2566 EMSG(_(e_trailing));
2567 return NULL;
2568 }
2569
2570 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2571 if (lp->ll_exp_name == NULL)
2572 {
2573 /* Report an invalid expression in braces, unless the
2574 * expression evaluation has been cancelled due to an
2575 * aborting error, an interrupt, or an exception. */
2576 if (!aborting() && !quiet)
2577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002578 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 EMSG2(_(e_invarg2), name);
2580 return NULL;
2581 }
2582 }
2583 lp->ll_name = lp->ll_exp_name;
2584 }
2585 else
2586 lp->ll_name = name;
2587
2588 /* Without [idx] or .key we are done. */
2589 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2590 return p;
2591
2592 cc = *p;
2593 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002594 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (v == NULL && !quiet)
2596 EMSG2(_(e_undefvar), lp->ll_name);
2597 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 if (v == NULL)
2599 return NULL;
2600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /*
2602 * Loop until no more [idx] or .key is following.
2603 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2608 && !(lp->ll_tv->v_type == VAR_DICT
2609 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (!quiet)
2612 EMSG(_("E689: Can only index a List or Dictionary"));
2613 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (!quiet)
2618 EMSG(_("E708: [:] must come last"));
2619 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002621
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 len = -1;
2623 if (*p == '.')
2624 {
2625 key = p + 1;
2626 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2627 ;
2628 if (len == 0)
2629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!quiet)
2631 EMSG(_(e_emptykey));
2632 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 }
2634 p = key + len;
2635 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002636 else
2637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002639 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 if (*p == ':')
2641 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002642 else
2643 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 empty1 = FALSE;
2645 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002647 if (get_tv_string_chk(&var1) == NULL)
2648 {
2649 /* not a number or string */
2650 clear_tv(&var1);
2651 return NULL;
2652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 }
2654
2655 /* Optionally get the second index [ :expr]. */
2656 if (*p == ':')
2657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002661 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 if (!empty1)
2663 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (rettv != NULL && (rettv->v_type != VAR_LIST
2667 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (!empty1)
2672 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
2675 p = skipwhite(p + 1);
2676 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 else
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2682 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 if (!empty1)
2684 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 if (get_tv_string_chk(&var2) == NULL)
2688 {
2689 /* not a number or string */
2690 if (!empty1)
2691 clear_tv(&var1);
2692 clear_tv(&var2);
2693 return NULL;
2694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
2704 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 if (!empty1)
2706 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711
2712 /* Skip to past ']'. */
2713 ++p;
2714 }
2715
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
2718 if (len == -1)
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002721 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (*key == NUL)
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
2725 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 lp->ll_list = NULL;
2731 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002732 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002733
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002734 /* When assigning to a scope dictionary check that a function and
2735 * variable name is valid (only variable name unless it is l: or
2736 * g: dictionary). Disallow overwriting a builtin function. */
2737 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002738 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002739 int prevval;
2740 int wrong;
2741
2742 if (len != -1)
2743 {
2744 prevval = key[len];
2745 key[len] = NUL;
2746 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002747 else
2748 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002749 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2750 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002752 || !valid_varname(key);
2753 if (len != -1)
2754 key[len] = prevval;
2755 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 return NULL;
2757 }
2758
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 /* Can't add "v:" variable. */
2762 if (lp->ll_dict == &vimvardict)
2763 {
2764 EMSG2(_(e_illvar), name);
2765 return NULL;
2766 }
2767
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002768 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002772 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 if (len == -1)
2774 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 }
2777 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 if (len == -1)
2782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 p = NULL;
2785 break;
2786 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 /* existing variable, need to check if it can be changed */
2788 else if (var_check_ro(lp->ll_di->di_flags, name))
2789 return NULL;
2790
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 if (len == -1)
2792 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795 else
2796 {
2797 /*
2798 * Get the number and item for the only or first index of the List.
2799 */
2800 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
2803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002804 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var1);
2806 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002807 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_list = lp->ll_tv->vval.v_list;
2809 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2810 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002812 if (lp->ll_n1 < 0)
2813 {
2814 lp->ll_n1 = 0;
2815 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2816 }
2817 }
2818 if (lp->ll_li == NULL)
2819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002822 if (!quiet)
2823 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826
2827 /*
2828 * May need to find the item or absolute index for the second
2829 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 * When no index given: "lp->ll_empty2" is TRUE.
2831 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002835 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 {
2842 if (!quiet)
2843 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2850 if (lp->ll_n1 < 0)
2851 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2852 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 {
2854 if (!quiet)
2855 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002858 }
2859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002862 }
2863
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 return p;
2865}
2866
2867/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 */
2870 static void
2871clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002872 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873{
2874 vim_free(lp->ll_exp_name);
2875 vim_free(lp->ll_newkey);
2876}
2877
2878/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002879 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 */
2883 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890{
2891 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 listitem_T *ri;
2893 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894
2895 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 cc = *endp;
2900 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901 if (op != NULL && *op != '=')
2902 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002905 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002906 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002907 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 {
2909 if (tv_op(&tv, rettv, op) == OK)
2910 set_var(lp->ll_name, &tv, FALSE);
2911 clear_tv(&tv);
2912 }
2913 }
2914 else
2915 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002919 else if (tv_check_lock(lp->ll_newkey == NULL
2920 ? lp->ll_tv->v_lock
2921 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2922 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 else if (lp->ll_range)
2924 {
2925 /*
2926 * Assign the List values to the list items.
2927 */
2928 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 if (op != NULL && *op != '=')
2931 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2932 else
2933 {
2934 clear_tv(&lp->ll_li->li_tv);
2935 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 ri = ri->li_next;
2938 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2939 break;
2940 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002943 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 ri = NULL;
2946 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 lp->ll_li = lp->ll_li->li_next;
2950 ++lp->ll_n1;
2951 }
2952 if (ri != NULL)
2953 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 else if (lp->ll_empty2
2955 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 : lp->ll_n1 != lp->ll_n2)
2957 EMSG(_("E711: List value has not enough items"));
2958 }
2959 else
2960 {
2961 /*
2962 * Assign to a List or Dictionary item.
2963 */
2964 if (lp->ll_newkey != NULL)
2965 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 if (op != NULL && *op != '=')
2967 {
2968 EMSG2(_(e_letwrong), op);
2969 return;
2970 }
2971
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002973 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 if (di == NULL)
2975 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002976 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2977 {
2978 vim_free(di);
2979 return;
2980 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 else if (op != NULL && *op != '=')
2984 {
2985 tv_op(lp->ll_tv, rettv, op);
2986 return;
2987 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 /*
2992 * Assign the value to the variable or list item.
2993 */
2994 if (copy)
2995 copy_tv(rettv, lp->ll_tv);
2996 else
2997 {
2998 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002999 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 }
3002 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003}
3004
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003005/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3007 * Returns OK or FAIL.
3008 */
3009 static int
3010tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003011 typval_T *tv1;
3012 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 char_u *op;
3014{
3015 long n;
3016 char_u numbuf[NUMBUFLEN];
3017 char_u *s;
3018
3019 /* Can't do anything with a Funcref or a Dict on the right. */
3020 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3021 {
3022 switch (tv1->v_type)
3023 {
3024 case VAR_DICT:
3025 case VAR_FUNC:
3026 break;
3027
3028 case VAR_LIST:
3029 if (*op != '+' || tv2->v_type != VAR_LIST)
3030 break;
3031 /* List += List */
3032 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3033 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3034 return OK;
3035
3036 case VAR_NUMBER:
3037 case VAR_STRING:
3038 if (tv2->v_type == VAR_LIST)
3039 break;
3040 if (*op == '+' || *op == '-')
3041 {
3042 /* nr += nr or nr -= nr*/
3043 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044#ifdef FEAT_FLOAT
3045 if (tv2->v_type == VAR_FLOAT)
3046 {
3047 float_T f = n;
3048
3049 if (*op == '+')
3050 f += tv2->vval.v_float;
3051 else
3052 f -= tv2->vval.v_float;
3053 clear_tv(tv1);
3054 tv1->v_type = VAR_FLOAT;
3055 tv1->vval.v_float = f;
3056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#endif
3059 {
3060 if (*op == '+')
3061 n += get_tv_number(tv2);
3062 else
3063 n -= get_tv_number(tv2);
3064 clear_tv(tv1);
3065 tv1->v_type = VAR_NUMBER;
3066 tv1->vval.v_number = n;
3067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 }
3069 else
3070 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071 if (tv2->v_type == VAR_FLOAT)
3072 break;
3073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 /* str .= str */
3075 s = get_tv_string(tv1);
3076 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3077 clear_tv(tv1);
3078 tv1->v_type = VAR_STRING;
3079 tv1->vval.v_string = s;
3080 }
3081 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082
3083#ifdef FEAT_FLOAT
3084 case VAR_FLOAT:
3085 {
3086 float_T f;
3087
3088 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3089 && tv2->v_type != VAR_NUMBER
3090 && tv2->v_type != VAR_STRING))
3091 break;
3092 if (tv2->v_type == VAR_FLOAT)
3093 f = tv2->vval.v_float;
3094 else
3095 f = get_tv_number(tv2);
3096 if (*op == '+')
3097 tv1->vval.v_float += f;
3098 else
3099 tv1->vval.v_float -= f;
3100 }
3101 return OK;
3102#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 }
3104 }
3105
3106 EMSG2(_(e_letwrong), op);
3107 return FAIL;
3108}
3109
3110/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111 * Add a watcher to a list.
3112 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003113 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003114list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 list_T *l;
3116 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117{
3118 lw->lw_next = l->lv_watch;
3119 l->lv_watch = lw;
3120}
3121
3122/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003123 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 * No warning when it isn't found...
3125 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003126 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 list_T *l;
3129 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130{
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003132
3133 lwp = &l->lv_watch;
3134 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3135 {
3136 if (lw == lwrem)
3137 {
3138 *lwp = lw->lw_next;
3139 break;
3140 }
3141 lwp = &lw->lw_next;
3142 }
3143}
3144
3145/*
3146 * Just before removing an item from a list: advance watchers to the next
3147 * item.
3148 */
3149 static void
3150list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003151 list_T *l;
3152 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153{
Bram Moolenaar33570922005-01-25 22:26:29 +00003154 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155
3156 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3157 if (lw->lw_item == item)
3158 lw->lw_item = item->li_next;
3159}
3160
3161/*
3162 * Evaluate the expression used in a ":for var in expr" command.
3163 * "arg" points to "var".
3164 * Set "*errp" to TRUE for an error, FALSE otherwise;
3165 * Return a pointer that holds the info. Null when there is an error.
3166 */
3167 void *
3168eval_for_line(arg, errp, nextcmdp, skip)
3169 char_u *arg;
3170 int *errp;
3171 char_u **nextcmdp;
3172 int skip;
3173{
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003175 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 typval_T tv;
3177 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178
3179 *errp = TRUE; /* default: there is an error */
3180
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 if (fi == NULL)
3183 return NULL;
3184
3185 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3186 if (expr == NULL)
3187 return fi;
3188
3189 expr = skipwhite(expr);
3190 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3191 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003192 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193 return fi;
3194 }
3195
3196 if (skip)
3197 ++emsg_skip;
3198 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3199 {
3200 *errp = FALSE;
3201 if (!skip)
3202 {
3203 l = tv.vval.v_list;
3204 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207 clear_tv(&tv);
3208 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 else
3210 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003211 /* No need to increment the refcount, it's already set for the
3212 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 fi->fi_list = l;
3214 list_add_watch(l, &fi->fi_lw);
3215 fi->fi_lw.lw_item = l->lv_first;
3216 }
3217 }
3218 }
3219 if (skip)
3220 --emsg_skip;
3221
3222 return fi;
3223}
3224
3225/*
3226 * Use the first item in a ":for" list. Advance to the next.
3227 * Assign the values to the variable (list). "arg" points to the first one.
3228 * Return TRUE when a valid item was found, FALSE when at end of list or
3229 * something wrong.
3230 */
3231 int
3232next_for_item(fi_void, arg)
3233 void *fi_void;
3234 char_u *arg;
3235{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003236 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 item = fi->fi_lw.lw_item;
3241 if (item == NULL)
3242 result = FALSE;
3243 else
3244 {
3245 fi->fi_lw.lw_item = item->li_next;
3246 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3247 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3248 }
3249 return result;
3250}
3251
3252/*
3253 * Free the structure used to store info used by ":for".
3254 */
3255 void
3256free_for_info(fi_void)
3257 void *fi_void;
3258{
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003261 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 list_unref(fi->fi_list);
3265 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 vim_free(fi);
3267}
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3270
3271 void
3272set_context_for_expression(xp, arg, cmdidx)
3273 expand_T *xp;
3274 char_u *arg;
3275 cmdidx_T cmdidx;
3276{
3277 int got_eq = FALSE;
3278 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 if (cmdidx == CMD_let)
3282 {
3283 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003284 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 {
3286 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003287 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 {
3289 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003290 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 if (vim_iswhite(*p))
3292 break;
3293 }
3294 return;
3295 }
3296 }
3297 else
3298 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3299 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 while ((xp->xp_pattern = vim_strpbrk(arg,
3301 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3302 {
3303 c = *xp->xp_pattern;
3304 if (c == '&')
3305 {
3306 c = xp->xp_pattern[1];
3307 if (c == '&')
3308 {
3309 ++xp->xp_pattern;
3310 xp->xp_context = cmdidx != CMD_let || got_eq
3311 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3312 }
3313 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003316 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3317 xp->xp_pattern += 2;
3318
3319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
3321 else if (c == '$')
3322 {
3323 /* environment variable */
3324 xp->xp_context = EXPAND_ENV_VARS;
3325 }
3326 else if (c == '=')
3327 {
3328 got_eq = TRUE;
3329 xp->xp_context = EXPAND_EXPRESSION;
3330 }
3331 else if (c == '<'
3332 && xp->xp_context == EXPAND_FUNCTIONS
3333 && vim_strchr(xp->xp_pattern, '(') == NULL)
3334 {
3335 /* Function name can start with "<SNR>" */
3336 break;
3337 }
3338 else if (cmdidx != CMD_let || got_eq)
3339 {
3340 if (c == '"') /* string */
3341 {
3342 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3343 if (c == '\\' && xp->xp_pattern[1] != NUL)
3344 ++xp->xp_pattern;
3345 xp->xp_context = EXPAND_NOTHING;
3346 }
3347 else if (c == '\'') /* literal string */
3348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3351 /* skip */ ;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '|')
3355 {
3356 if (xp->xp_pattern[1] == '|')
3357 {
3358 ++xp->xp_pattern;
3359 xp->xp_context = EXPAND_EXPRESSION;
3360 }
3361 else
3362 xp->xp_context = EXPAND_COMMANDS;
3363 }
3364 else
3365 xp->xp_context = EXPAND_EXPRESSION;
3366 }
3367 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 /* Doesn't look like something valid, expand as an expression
3369 * anyway. */
3370 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 arg = xp->xp_pattern;
3372 if (*arg != NUL)
3373 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3374 /* skip */ ;
3375 }
3376 xp->xp_pattern = arg;
3377}
3378
3379#endif /* FEAT_CMDL_COMPL */
3380
3381/*
3382 * ":1,25call func(arg1, arg2)" function call.
3383 */
3384 void
3385ex_call(eap)
3386 exarg_T *eap;
3387{
3388 char_u *arg = eap->arg;
3389 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003391 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 linenr_T lnum;
3395 int doesrange;
3396 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003397 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003399 if (eap->skip)
3400 {
3401 /* trans_function_name() doesn't work well when skipping, use eval0()
3402 * instead to skip to any following command, e.g. for:
3403 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003404 ++emsg_skip;
3405 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3406 clear_tv(&rettv);
3407 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003408 return;
3409 }
3410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003411 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003412 if (fudi.fd_newkey != NULL)
3413 {
3414 /* Still need to give an error message for missing key. */
3415 EMSG2(_(e_dictkey), fudi.fd_newkey);
3416 vim_free(fudi.fd_newkey);
3417 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003418 if (tofree == NULL)
3419 return;
3420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003421 /* Increase refcount on dictionary, it could get deleted when evaluating
3422 * the arguments. */
3423 if (fudi.fd_dict != NULL)
3424 ++fudi.fd_dict->dv_refcount;
3425
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003426 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003427 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003428 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar532c7802005-01-27 14:44:31 +00003430 /* Skip white space to allow ":call func ()". Not good, but required for
3431 * backward compatibility. */
3432 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 if (*startarg != '(')
3436 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003437 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 goto end;
3439 }
3440
3441 /*
3442 * When skipping, evaluate the function once, to find the end of the
3443 * arguments.
3444 * When the function takes a range, this is discovered after the first
3445 * call, and the loop is broken.
3446 */
3447 if (eap->skip)
3448 {
3449 ++emsg_skip;
3450 lnum = eap->line2; /* do it once, also with an invalid range */
3451 }
3452 else
3453 lnum = eap->line1;
3454 for ( ; lnum <= eap->line2; ++lnum)
3455 {
3456 if (!eap->skip && eap->addr_count > 0)
3457 {
3458 curwin->w_cursor.lnum = lnum;
3459 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003460#ifdef FEAT_VIRTUALEDIT
3461 curwin->w_cursor.coladd = 0;
3462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003466 eap->line1, eap->line2, &doesrange,
3467 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 failed = TRUE;
3470 break;
3471 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003472
3473 /* Handle a function returning a Funcref, Dictionary or List. */
3474 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3475 {
3476 failed = TRUE;
3477 break;
3478 }
3479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (doesrange || eap->skip)
3482 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003485 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003486 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003487 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (aborting())
3489 break;
3490 }
3491 if (eap->skip)
3492 --emsg_skip;
3493
3494 if (!failed)
3495 {
3496 /* Check for trailing illegal characters and a following command. */
3497 if (!ends_excmd(*arg))
3498 {
3499 emsg_severe = TRUE;
3500 EMSG(_(e_trailing));
3501 }
3502 else
3503 eap->nextcmd = check_nextcmd(arg);
3504 }
3505
3506end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003507 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003508 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509}
3510
3511/*
3512 * ":unlet[!] var1 ... " command.
3513 */
3514 void
3515ex_unlet(eap)
3516 exarg_T *eap;
3517{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003518 ex_unletlock(eap, eap->arg, 0);
3519}
3520
3521/*
3522 * ":lockvar" and ":unlockvar" commands
3523 */
3524 void
3525ex_lockvar(eap)
3526 exarg_T *eap;
3527{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003529 int deep = 2;
3530
3531 if (eap->forceit)
3532 deep = -1;
3533 else if (vim_isdigit(*arg))
3534 {
3535 deep = getdigits(&arg);
3536 arg = skipwhite(arg);
3537 }
3538
3539 ex_unletlock(eap, arg, deep);
3540}
3541
3542/*
3543 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3544 */
3545 static void
3546ex_unletlock(eap, argstart, deep)
3547 exarg_T *eap;
3548 char_u *argstart;
3549 int deep;
3550{
3551 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003554 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556 do
3557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003558 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003559 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3560 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (lv.ll_name == NULL)
3562 error = TRUE; /* error but continue parsing */
3563 if (name_end == NULL || (!vim_iswhite(*name_end)
3564 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 if (name_end != NULL)
3567 {
3568 emsg_severe = TRUE;
3569 EMSG(_(e_trailing));
3570 }
3571 if (!(eap->skip || error))
3572 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 break;
3574 }
3575
3576 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 {
3578 if (eap->cmdidx == CMD_unlet)
3579 {
3580 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3581 error = TRUE;
3582 }
3583 else
3584 {
3585 if (do_lock_var(&lv, name_end, deep,
3586 eap->cmdidx == CMD_lockvar) == FAIL)
3587 error = TRUE;
3588 }
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 if (!eap->skip)
3592 clear_lval(&lv);
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 arg = skipwhite(name_end);
3595 } while (!ends_excmd(*arg));
3596
3597 eap->nextcmd = check_nextcmd(arg);
3598}
3599
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003602 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003603 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 int forceit;
3605{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 int ret = OK;
3607 int cc;
3608
3609 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 cc = *name_end;
3612 *name_end = NUL;
3613
3614 /* Normal name or expanded name. */
3615 if (check_changedtick(lp->ll_name))
3616 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3622 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 else if (lp->ll_range)
3624 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003625 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626
3627 /* Delete a range of List items. */
3628 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3629 {
3630 li = lp->ll_li->li_next;
3631 listitem_remove(lp->ll_list, lp->ll_li);
3632 lp->ll_li = li;
3633 ++lp->ll_n1;
3634 }
3635 }
3636 else
3637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 /* unlet a List item. */
3640 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003643 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 }
3645
3646 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
3650 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 */
3653 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003656 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 hashtab_T *ht;
3659 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003660 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003661 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar33570922005-01-25 22:26:29 +00003663 ht = find_var_ht(name, &varname);
3664 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003666 hi = hash_find(ht, varname);
3667 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003668 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003669 di = HI2DI(hi);
3670 if (var_check_fixed(di->di_flags, name)
3671 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 return FAIL;
3673 delete_var(ht, hi);
3674 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 if (forceit)
3678 return OK;
3679 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return FAIL;
3681}
3682
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683/*
3684 * Lock or unlock variable indicated by "lp".
3685 * "deep" is the levels to go (-1 for unlimited);
3686 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3687 */
3688 static int
3689do_lock_var(lp, name_end, deep, lock)
3690 lval_T *lp;
3691 char_u *name_end;
3692 int deep;
3693 int lock;
3694{
3695 int ret = OK;
3696 int cc;
3697 dictitem_T *di;
3698
3699 if (deep == 0) /* nothing to do */
3700 return OK;
3701
3702 if (lp->ll_tv == NULL)
3703 {
3704 cc = *name_end;
3705 *name_end = NUL;
3706
3707 /* Normal name or expanded name. */
3708 if (check_changedtick(lp->ll_name))
3709 ret = FAIL;
3710 else
3711 {
3712 di = find_var(lp->ll_name, NULL);
3713 if (di == NULL)
3714 ret = FAIL;
3715 else
3716 {
3717 if (lock)
3718 di->di_flags |= DI_FLAGS_LOCK;
3719 else
3720 di->di_flags &= ~DI_FLAGS_LOCK;
3721 item_lock(&di->di_tv, deep, lock);
3722 }
3723 }
3724 *name_end = cc;
3725 }
3726 else if (lp->ll_range)
3727 {
3728 listitem_T *li = lp->ll_li;
3729
3730 /* (un)lock a range of List items. */
3731 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3732 {
3733 item_lock(&li->li_tv, deep, lock);
3734 li = li->li_next;
3735 ++lp->ll_n1;
3736 }
3737 }
3738 else if (lp->ll_list != NULL)
3739 /* (un)lock a List item. */
3740 item_lock(&lp->ll_li->li_tv, deep, lock);
3741 else
3742 /* un(lock) a Dictionary item. */
3743 item_lock(&lp->ll_di->di_tv, deep, lock);
3744
3745 return ret;
3746}
3747
3748/*
3749 * Lock or unlock an item. "deep" is nr of levels to go.
3750 */
3751 static void
3752item_lock(tv, deep, lock)
3753 typval_T *tv;
3754 int deep;
3755 int lock;
3756{
3757 static int recurse = 0;
3758 list_T *l;
3759 listitem_T *li;
3760 dict_T *d;
3761 hashitem_T *hi;
3762 int todo;
3763
3764 if (recurse >= DICT_MAXNEST)
3765 {
3766 EMSG(_("E743: variable nested too deep for (un)lock"));
3767 return;
3768 }
3769 if (deep == 0)
3770 return;
3771 ++recurse;
3772
3773 /* lock/unlock the item itself */
3774 if (lock)
3775 tv->v_lock |= VAR_LOCKED;
3776 else
3777 tv->v_lock &= ~VAR_LOCKED;
3778
3779 switch (tv->v_type)
3780 {
3781 case VAR_LIST:
3782 if ((l = tv->vval.v_list) != NULL)
3783 {
3784 if (lock)
3785 l->lv_lock |= VAR_LOCKED;
3786 else
3787 l->lv_lock &= ~VAR_LOCKED;
3788 if (deep < 0 || deep > 1)
3789 /* recursive: lock/unlock the items the List contains */
3790 for (li = l->lv_first; li != NULL; li = li->li_next)
3791 item_lock(&li->li_tv, deep - 1, lock);
3792 }
3793 break;
3794 case VAR_DICT:
3795 if ((d = tv->vval.v_dict) != NULL)
3796 {
3797 if (lock)
3798 d->dv_lock |= VAR_LOCKED;
3799 else
3800 d->dv_lock &= ~VAR_LOCKED;
3801 if (deep < 0 || deep > 1)
3802 {
3803 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003804 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3806 {
3807 if (!HASHITEM_EMPTY(hi))
3808 {
3809 --todo;
3810 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3811 }
3812 }
3813 }
3814 }
3815 }
3816 --recurse;
3817}
3818
Bram Moolenaara40058a2005-07-11 22:42:07 +00003819/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003820 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3821 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003822 */
3823 static int
3824tv_islocked(tv)
3825 typval_T *tv;
3826{
3827 return (tv->v_lock & VAR_LOCKED)
3828 || (tv->v_type == VAR_LIST
3829 && tv->vval.v_list != NULL
3830 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3831 || (tv->v_type == VAR_DICT
3832 && tv->vval.v_dict != NULL
3833 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3834}
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3837/*
3838 * Delete all "menutrans_" variables.
3839 */
3840 void
3841del_menutrans_vars()
3842{
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003847 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003849 {
3850 if (!HASHITEM_EMPTY(hi))
3851 {
3852 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3854 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003855 }
3856 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003857 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858}
3859#endif
3860
3861#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3862
3863/*
3864 * Local string buffer for the next two functions to store a variable name
3865 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3866 * get_user_var_name().
3867 */
3868
3869static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3870
3871static char_u *varnamebuf = NULL;
3872static int varnamebuflen = 0;
3873
3874/*
3875 * Function to concatenate a prefix and a variable name.
3876 */
3877 static char_u *
3878cat_prefix_varname(prefix, name)
3879 int prefix;
3880 char_u *name;
3881{
3882 int len;
3883
3884 len = (int)STRLEN(name) + 3;
3885 if (len > varnamebuflen)
3886 {
3887 vim_free(varnamebuf);
3888 len += 10; /* some additional space */
3889 varnamebuf = alloc(len);
3890 if (varnamebuf == NULL)
3891 {
3892 varnamebuflen = 0;
3893 return NULL;
3894 }
3895 varnamebuflen = len;
3896 }
3897 *varnamebuf = prefix;
3898 varnamebuf[1] = ':';
3899 STRCPY(varnamebuf + 2, name);
3900 return varnamebuf;
3901}
3902
3903/*
3904 * Function given to ExpandGeneric() to obtain the list of user defined
3905 * (global/buffer/window/built-in) variable names.
3906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 char_u *
3908get_user_var_name(xp, idx)
3909 expand_T *xp;
3910 int idx;
3911{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003912 static long_u gdone;
3913 static long_u bdone;
3914 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003915#ifdef FEAT_WINDOWS
3916 static long_u tdone;
3917#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003918 static int vidx;
3919 static hashitem_T *hi;
3920 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003924 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003925#ifdef FEAT_WINDOWS
3926 tdone = 0;
3927#endif
3928 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003929
3930 /* Global variables */
3931 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003935 else
3936 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 while (HASHITEM_EMPTY(hi))
3938 ++hi;
3939 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3940 return cat_prefix_varname('g', hi->hi_key);
3941 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003943
3944 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003945 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003950 else
3951 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003952 while (HASHITEM_EMPTY(hi))
3953 ++hi;
3954 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 return (char_u *)"b:changedtick";
3960 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003961
3962 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003963 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003964 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003966 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003968 else
3969 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003970 while (HASHITEM_EMPTY(hi))
3971 ++hi;
3972 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003975#ifdef FEAT_WINDOWS
3976 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003977 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003978 if (tdone < ht->ht_used)
3979 {
3980 if (tdone++ == 0)
3981 hi = ht->ht_array;
3982 else
3983 ++hi;
3984 while (HASHITEM_EMPTY(hi))
3985 ++hi;
3986 return cat_prefix_varname('t', hi->hi_key);
3987 }
3988#endif
3989
Bram Moolenaar33570922005-01-25 22:26:29 +00003990 /* v: variables */
3991 if (vidx < VV_LEN)
3992 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994 vim_free(varnamebuf);
3995 varnamebuf = NULL;
3996 varnamebuflen = 0;
3997 return NULL;
3998}
3999
4000#endif /* FEAT_CMDL_COMPL */
4001
4002/*
4003 * types for expressions.
4004 */
4005typedef enum
4006{
4007 TYPE_UNKNOWN = 0
4008 , TYPE_EQUAL /* == */
4009 , TYPE_NEQUAL /* != */
4010 , TYPE_GREATER /* > */
4011 , TYPE_GEQUAL /* >= */
4012 , TYPE_SMALLER /* < */
4013 , TYPE_SEQUAL /* <= */
4014 , TYPE_MATCH /* =~ */
4015 , TYPE_NOMATCH /* !~ */
4016} exptype_T;
4017
4018/*
4019 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4022 */
4023
4024/*
4025 * Handle zero level expression.
4026 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004028 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 * Return OK or FAIL.
4030 */
4031 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 char_u **nextcmd;
4036 int evaluate;
4037{
4038 int ret;
4039 char_u *p;
4040
4041 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (ret == FAIL || !ends_excmd(*p))
4044 {
4045 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 /*
4048 * Report the invalid expression unless the expression evaluation has
4049 * been cancelled due to an aborting error, an interrupt, or an
4050 * exception.
4051 */
4052 if (!aborting())
4053 EMSG2(_(e_invexpr2), arg);
4054 ret = FAIL;
4055 }
4056 if (nextcmd != NULL)
4057 *nextcmd = check_nextcmd(p);
4058
4059 return ret;
4060}
4061
4062/*
4063 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004064 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 *
4066 * "arg" must point to the first non-white of the expression.
4067 * "arg" is advanced to the next non-white after the recognized expression.
4068 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004069 * Note: "rettv.v_lock" is not set.
4070 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * Return OK or FAIL.
4072 */
4073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int evaluate;
4078{
4079 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081
4082 /*
4083 * Get the first variable.
4084 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 return FAIL;
4087
4088 if ((*arg)[0] == '?')
4089 {
4090 result = FALSE;
4091 if (evaluate)
4092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 int error = FALSE;
4094
4095 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 if (error)
4099 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101
4102 /*
4103 * Get the second variable.
4104 */
4105 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004106 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return FAIL;
4108
4109 /*
4110 * Check for the ":".
4111 */
4112 if ((*arg)[0] != ':')
4113 {
4114 EMSG(_("E109: Missing ':' after '?'"));
4115 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 return FAIL;
4118 }
4119
4120 /*
4121 * Get the third variable.
4122 */
4123 *arg = skipwhite(*arg + 1);
4124 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4125 {
4126 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 return FAIL;
4129 }
4130 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133
4134 return OK;
4135}
4136
4137/*
4138 * Handle first level expression:
4139 * expr2 || expr2 || expr2 logical OR
4140 *
4141 * "arg" must point to the first non-white of the expression.
4142 * "arg" is advanced to the next non-white after the recognized expression.
4143 *
4144 * Return OK or FAIL.
4145 */
4146 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int evaluate;
4151{
Bram Moolenaar33570922005-01-25 22:26:29 +00004152 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 long result;
4154 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 /*
4158 * Get the first variable.
4159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return FAIL;
4162
4163 /*
4164 * Repeat until there is no following "||".
4165 */
4166 first = TRUE;
4167 result = FALSE;
4168 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4169 {
4170 if (evaluate && first)
4171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 if (error)
4176 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 first = FALSE;
4178 }
4179
4180 /*
4181 * Get the second variable.
4182 */
4183 *arg = skipwhite(*arg + 2);
4184 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4185 return FAIL;
4186
4187 /*
4188 * Compute the result.
4189 */
4190 if (evaluate && !result)
4191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (error)
4196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 if (evaluate)
4199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 rettv->v_type = VAR_NUMBER;
4201 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 }
4203 }
4204
4205 return OK;
4206}
4207
4208/*
4209 * Handle second level expression:
4210 * expr3 && expr3 && expr3 logical AND
4211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
4215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int evaluate;
4222{
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 long result;
4225 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 /*
4235 * Repeat until there is no following "&&".
4236 */
4237 first = TRUE;
4238 result = TRUE;
4239 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4240 {
4241 if (evaluate && first)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 first = FALSE;
4249 }
4250
4251 /*
4252 * Get the second variable.
4253 */
4254 *arg = skipwhite(*arg + 2);
4255 if (eval4(arg, &var2, evaluate && result) == FAIL)
4256 return FAIL;
4257
4258 /*
4259 * Compute the result.
4260 */
4261 if (evaluate && result)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 if (evaluate)
4270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 rettv->v_type = VAR_NUMBER;
4272 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 }
4275
4276 return OK;
4277}
4278
4279/*
4280 * Handle third level expression:
4281 * var1 == var2
4282 * var1 =~ var2
4283 * var1 != var2
4284 * var1 !~ var2
4285 * var1 > var2
4286 * var1 >= var2
4287 * var1 < var2
4288 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004289 * var1 is var2
4290 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 *
4292 * "arg" must point to the first non-white of the expression.
4293 * "arg" is advanced to the next non-white after the recognized expression.
4294 *
4295 * Return OK or FAIL.
4296 */
4297 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 int evaluate;
4302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u *p;
4305 int i;
4306 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004307 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int len = 2;
4309 long n1, n2;
4310 char_u *s1, *s2;
4311 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4312 regmatch_T regmatch;
4313 int ic;
4314 char_u *save_cpo;
4315
4316 /*
4317 * Get the first variable.
4318 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 return FAIL;
4321
4322 p = *arg;
4323 switch (p[0])
4324 {
4325 case '=': if (p[1] == '=')
4326 type = TYPE_EQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_MATCH;
4329 break;
4330 case '!': if (p[1] == '=')
4331 type = TYPE_NEQUAL;
4332 else if (p[1] == '~')
4333 type = TYPE_NOMATCH;
4334 break;
4335 case '>': if (p[1] != '=')
4336 {
4337 type = TYPE_GREATER;
4338 len = 1;
4339 }
4340 else
4341 type = TYPE_GEQUAL;
4342 break;
4343 case '<': if (p[1] != '=')
4344 {
4345 type = TYPE_SMALLER;
4346 len = 1;
4347 }
4348 else
4349 type = TYPE_SEQUAL;
4350 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004351 case 'i': if (p[1] == 's')
4352 {
4353 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4354 len = 5;
4355 if (!vim_isIDc(p[len]))
4356 {
4357 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4358 type_is = TRUE;
4359 }
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363
4364 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004365 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 */
4367 if (type != TYPE_UNKNOWN)
4368 {
4369 /* extra question mark appended: ignore case */
4370 if (p[len] == '?')
4371 {
4372 ic = TRUE;
4373 ++len;
4374 }
4375 /* extra '#' appended: match case */
4376 else if (p[len] == '#')
4377 {
4378 ic = FALSE;
4379 ++len;
4380 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 else
4383 ic = p_ic;
4384
4385 /*
4386 * Get the second variable.
4387 */
4388 *arg = skipwhite(p + len);
4389 if (eval5(arg, &var2, evaluate) == FAIL)
4390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393 }
4394
4395 if (evaluate)
4396 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 if (type_is && rettv->v_type != var2.v_type)
4398 {
4399 /* For "is" a different type always means FALSE, for "notis"
4400 * it means TRUE. */
4401 n1 = (type == TYPE_NEQUAL);
4402 }
4403 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4404 {
4405 if (type_is)
4406 {
4407 n1 = (rettv->v_type == var2.v_type
4408 && rettv->vval.v_list == var2.vval.v_list);
4409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 else if (rettv->v_type != var2.v_type
4413 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4414 {
4415 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004416 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004418 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004419 clear_tv(rettv);
4420 clear_tv(&var2);
4421 return FAIL;
4422 }
4423 else
4424 {
4425 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004426 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4427 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 if (type == TYPE_NEQUAL)
4429 n1 = !n1;
4430 }
4431 }
4432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004433 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4434 {
4435 if (type_is)
4436 {
4437 n1 = (rettv->v_type == var2.v_type
4438 && rettv->vval.v_dict == var2.vval.v_dict);
4439 if (type == TYPE_NEQUAL)
4440 n1 = !n1;
4441 }
4442 else if (rettv->v_type != var2.v_type
4443 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4444 {
4445 if (rettv->v_type != var2.v_type)
4446 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4447 else
4448 EMSG(_("E736: Invalid operation for Dictionary"));
4449 clear_tv(rettv);
4450 clear_tv(&var2);
4451 return FAIL;
4452 }
4453 else
4454 {
4455 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004456 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4457 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004458 if (type == TYPE_NEQUAL)
4459 n1 = !n1;
4460 }
4461 }
4462
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004463 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4464 {
4465 if (rettv->v_type != var2.v_type
4466 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4467 {
4468 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004469 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004471 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004472 clear_tv(rettv);
4473 clear_tv(&var2);
4474 return FAIL;
4475 }
4476 else
4477 {
4478 /* Compare two Funcrefs for being equal or unequal. */
4479 if (rettv->vval.v_string == NULL
4480 || var2.vval.v_string == NULL)
4481 n1 = FALSE;
4482 else
4483 n1 = STRCMP(rettv->vval.v_string,
4484 var2.vval.v_string) == 0;
4485 if (type == TYPE_NEQUAL)
4486 n1 = !n1;
4487 }
4488 }
4489
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004490#ifdef FEAT_FLOAT
4491 /*
4492 * If one of the two variables is a float, compare as a float.
4493 * When using "=~" or "!~", always compare as string.
4494 */
4495 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4496 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4497 {
4498 float_T f1, f2;
4499
4500 if (rettv->v_type == VAR_FLOAT)
4501 f1 = rettv->vval.v_float;
4502 else
4503 f1 = get_tv_number(rettv);
4504 if (var2.v_type == VAR_FLOAT)
4505 f2 = var2.vval.v_float;
4506 else
4507 f2 = get_tv_number(&var2);
4508 n1 = FALSE;
4509 switch (type)
4510 {
4511 case TYPE_EQUAL: n1 = (f1 == f2); break;
4512 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4513 case TYPE_GREATER: n1 = (f1 > f2); break;
4514 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4515 case TYPE_SMALLER: n1 = (f1 < f2); break;
4516 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4517 case TYPE_UNKNOWN:
4518 case TYPE_MATCH:
4519 case TYPE_NOMATCH: break; /* avoid gcc warning */
4520 }
4521 }
4522#endif
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /*
4525 * If one of the two variables is a number, compare as a number.
4526 * When using "=~" or "!~", always compare as string.
4527 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 n1 = get_tv_number(rettv);
4532 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 switch (type)
4534 {
4535 case TYPE_EQUAL: n1 = (n1 == n2); break;
4536 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4537 case TYPE_GREATER: n1 = (n1 > n2); break;
4538 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4539 case TYPE_SMALLER: n1 = (n1 < n2); break;
4540 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4541 case TYPE_UNKNOWN:
4542 case TYPE_MATCH:
4543 case TYPE_NOMATCH: break; /* avoid gcc warning */
4544 }
4545 }
4546 else
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 s1 = get_tv_string_buf(rettv, buf1);
4549 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4551 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4552 else
4553 i = 0;
4554 n1 = FALSE;
4555 switch (type)
4556 {
4557 case TYPE_EQUAL: n1 = (i == 0); break;
4558 case TYPE_NEQUAL: n1 = (i != 0); break;
4559 case TYPE_GREATER: n1 = (i > 0); break;
4560 case TYPE_GEQUAL: n1 = (i >= 0); break;
4561 case TYPE_SMALLER: n1 = (i < 0); break;
4562 case TYPE_SEQUAL: n1 = (i <= 0); break;
4563
4564 case TYPE_MATCH:
4565 case TYPE_NOMATCH:
4566 /* avoid 'l' flag in 'cpoptions' */
4567 save_cpo = p_cpo;
4568 p_cpo = (char_u *)"";
4569 regmatch.regprog = vim_regcomp(s2,
4570 RE_MAGIC + RE_STRING);
4571 regmatch.rm_ic = ic;
4572 if (regmatch.regprog != NULL)
4573 {
4574 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004575 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (type == TYPE_NOMATCH)
4577 n1 = !n1;
4578 }
4579 p_cpo = save_cpo;
4580 break;
4581
4582 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4583 }
4584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004585 clear_tv(rettv);
4586 clear_tv(&var2);
4587 rettv->v_type = VAR_NUMBER;
4588 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 }
4591
4592 return OK;
4593}
4594
4595/*
4596 * Handle fourth level expression:
4597 * + number addition
4598 * - number subtraction
4599 * . string concatenation
4600 *
4601 * "arg" must point to the first non-white of the expression.
4602 * "arg" is advanced to the next non-white after the recognized expression.
4603 *
4604 * Return OK or FAIL.
4605 */
4606 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 int evaluate;
4611{
Bram Moolenaar33570922005-01-25 22:26:29 +00004612 typval_T var2;
4613 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 int op;
4615 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004616#ifdef FEAT_FLOAT
4617 float_T f1 = 0, f2 = 0;
4618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 char_u *s1, *s2;
4620 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4621 char_u *p;
4622
4623 /*
4624 * Get the first variable.
4625 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004626 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return FAIL;
4628
4629 /*
4630 * Repeat computing, until no '+', '-' or '.' is following.
4631 */
4632 for (;;)
4633 {
4634 op = **arg;
4635 if (op != '+' && op != '-' && op != '.')
4636 break;
4637
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004638 if ((op != '+' || rettv->v_type != VAR_LIST)
4639#ifdef FEAT_FLOAT
4640 && (op == '.' || rettv->v_type != VAR_FLOAT)
4641#endif
4642 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004643 {
4644 /* For "list + ...", an illegal use of the first operand as
4645 * a number cannot be determined before evaluating the 2nd
4646 * operand: if this is also a list, all is ok.
4647 * For "something . ...", "something - ..." or "non-list + ...",
4648 * we know that the first operand needs to be a string or number
4649 * without evaluating the 2nd operand. So check before to avoid
4650 * side effects after an error. */
4651 if (evaluate && get_tv_string_chk(rettv) == NULL)
4652 {
4653 clear_tv(rettv);
4654 return FAIL;
4655 }
4656 }
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /*
4659 * Get the second variable.
4660 */
4661 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004662 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004664 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 return FAIL;
4666 }
4667
4668 if (evaluate)
4669 {
4670 /*
4671 * Compute the result.
4672 */
4673 if (op == '.')
4674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4676 s2 = get_tv_string_buf_chk(&var2, buf2);
4677 if (s2 == NULL) /* type error ? */
4678 {
4679 clear_tv(rettv);
4680 clear_tv(&var2);
4681 return FAIL;
4682 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004683 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(rettv);
4685 rettv->v_type = VAR_STRING;
4686 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004688 else if (op == '+' && rettv->v_type == VAR_LIST
4689 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004690 {
4691 /* concatenate Lists */
4692 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4693 &var3) == FAIL)
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
4699 clear_tv(rettv);
4700 *rettv = var3;
4701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else
4703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 int error = FALSE;
4705
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706#ifdef FEAT_FLOAT
4707 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 f1 = rettv->vval.v_float;
4710 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712 else
4713#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004714 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 n1 = get_tv_number_chk(rettv, &error);
4716 if (error)
4717 {
4718 /* This can only happen for "list + non-list". For
4719 * "non-list + ..." or "something - ...", we returned
4720 * before evaluating the 2nd operand. */
4721 clear_tv(rettv);
4722 return FAIL;
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 f1 = n1;
4727#endif
4728 }
4729#ifdef FEAT_FLOAT
4730 if (var2.v_type == VAR_FLOAT)
4731 {
4732 f2 = var2.vval.v_float;
4733 n2 = 0;
4734 }
4735 else
4736#endif
4737 {
4738 n2 = get_tv_number_chk(&var2, &error);
4739 if (error)
4740 {
4741 clear_tv(rettv);
4742 clear_tv(&var2);
4743 return FAIL;
4744 }
4745#ifdef FEAT_FLOAT
4746 if (rettv->v_type == VAR_FLOAT)
4747 f2 = n2;
4748#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751
4752#ifdef FEAT_FLOAT
4753 /* If there is a float on either side the result is a float. */
4754 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4755 {
4756 if (op == '+')
4757 f1 = f1 + f2;
4758 else
4759 f1 = f1 - f2;
4760 rettv->v_type = VAR_FLOAT;
4761 rettv->vval.v_float = f1;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764#endif
4765 {
4766 if (op == '+')
4767 n1 = n1 + n2;
4768 else
4769 n1 = n1 - n2;
4770 rettv->v_type = VAR_NUMBER;
4771 rettv->vval.v_number = n1;
4772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
4776 }
4777 return OK;
4778}
4779
4780/*
4781 * Handle fifth level expression:
4782 * * number multiplication
4783 * / number division
4784 * % number modulo
4785 *
4786 * "arg" must point to the first non-white of the expression.
4787 * "arg" is advanced to the next non-white after the recognized expression.
4788 *
4789 * Return OK or FAIL.
4790 */
4791 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004796 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar33570922005-01-25 22:26:29 +00004798 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 int op;
4800 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801#ifdef FEAT_FLOAT
4802 int use_float = FALSE;
4803 float_T f1 = 0, f2;
4804#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
4808 * Get the first variable.
4809 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004810 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return FAIL;
4812
4813 /*
4814 * Repeat computing, until no '*', '/' or '%' is following.
4815 */
4816 for (;;)
4817 {
4818 op = **arg;
4819 if (op != '*' && op != '/' && op != '%')
4820 break;
4821
4822 if (evaluate)
4823 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824#ifdef FEAT_FLOAT
4825 if (rettv->v_type == VAR_FLOAT)
4826 {
4827 f1 = rettv->vval.v_float;
4828 use_float = TRUE;
4829 n1 = 0;
4830 }
4831 else
4832#endif
4833 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 if (error)
4836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 else
4839 n1 = 0;
4840
4841 /*
4842 * Get the second variable.
4843 */
4844 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004845 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 return FAIL;
4847
4848 if (evaluate)
4849 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#ifdef FEAT_FLOAT
4851 if (var2.v_type == VAR_FLOAT)
4852 {
4853 if (!use_float)
4854 {
4855 f1 = n1;
4856 use_float = TRUE;
4857 }
4858 f2 = var2.vval.v_float;
4859 n2 = 0;
4860 }
4861 else
4862#endif
4863 {
4864 n2 = get_tv_number_chk(&var2, &error);
4865 clear_tv(&var2);
4866 if (error)
4867 return FAIL;
4868#ifdef FEAT_FLOAT
4869 if (use_float)
4870 f2 = n2;
4871#endif
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 /*
4875 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881 if (op == '*')
4882 f1 = f1 * f2;
4883 else if (op == '/')
4884 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885# ifdef VMS
4886 /* VMS crashes on divide by zero, work around it */
4887 if (f2 == 0.0)
4888 {
4889 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004892 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004894 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004895 }
4896 else
4897 f1 = f1 / f2;
4898# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 /* We rely on the floating point library to handle divide
4900 * by zero to result in "inf" and not a crash. */
4901 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004906 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907 return FAIL;
4908 }
4909 rettv->v_type = VAR_FLOAT;
4910 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 if (op == '*')
4916 n1 = n1 * n2;
4917 else if (op == '/')
4918 {
4919 if (n2 == 0) /* give an error message? */
4920 {
4921 if (n1 == 0)
4922 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4923 else if (n1 < 0)
4924 n1 = -0x7fffffffL;
4925 else
4926 n1 = 0x7fffffffL;
4927 }
4928 else
4929 n1 = n1 / n2;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 {
4933 if (n2 == 0) /* give an error message? */
4934 n1 = 0;
4935 else
4936 n1 = n1 % n2;
4937 }
4938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 }
4943
4944 return OK;
4945}
4946
4947/*
4948 * Handle sixth level expression:
4949 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004950 * "string" string constant
4951 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * &option-name option value
4953 * @r register contents
4954 * identifier variable value
4955 * function() function call
4956 * $VAR environment variable
4957 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004958 * [expr, expr] List
4959 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 *
4961 * Also handle:
4962 * ! in front logical NOT
4963 * - in front unary minus
4964 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965 * trailing [] subscript in String or List
4966 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * "arg" must point to the first non-white of the expression.
4969 * "arg" is advanced to the next non-white after the recognized expression.
4970 *
4971 * Return OK or FAIL.
4972 */
4973 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004974eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004978 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 long n;
4981 int len;
4982 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 char_u *start_leader, *end_leader;
4984 int ret = OK;
4985 char_u *alias;
4986
4987 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004988 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 /*
4994 * Skip '!' and '-' characters. They are handled later.
4995 */
4996 start_leader = *arg;
4997 while (**arg == '!' || **arg == '-' || **arg == '+')
4998 *arg = skipwhite(*arg + 1);
4999 end_leader = *arg;
5000
5001 switch (**arg)
5002 {
5003 /*
5004 * Number constant.
5005 */
5006 case '0':
5007 case '1':
5008 case '2':
5009 case '3':
5010 case '4':
5011 case '5':
5012 case '6':
5013 case '7':
5014 case '8':
5015 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016 {
5017#ifdef FEAT_FLOAT
5018 char_u *p = skipdigits(*arg + 1);
5019 int get_float = FALSE;
5020
5021 /* We accept a float when the format matches
5022 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005023 * strict to avoid backwards compatibility problems.
5024 * Don't look for a float after the "." operator, so that
5025 * ":let vers = 1.2.3" doesn't fail. */
5026 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 get_float = TRUE;
5029 p = skipdigits(p + 2);
5030 if (*p == 'e' || *p == 'E')
5031 {
5032 ++p;
5033 if (*p == '-' || *p == '+')
5034 ++p;
5035 if (!vim_isdigit(*p))
5036 get_float = FALSE;
5037 else
5038 p = skipdigits(p + 1);
5039 }
5040 if (ASCII_ISALPHA(*p) || *p == '.')
5041 get_float = FALSE;
5042 }
5043 if (get_float)
5044 {
5045 float_T f;
5046
5047 *arg += string2float(*arg, &f);
5048 if (evaluate)
5049 {
5050 rettv->v_type = VAR_FLOAT;
5051 rettv->vval.v_float = f;
5052 }
5053 }
5054 else
5055#endif
5056 {
5057 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5058 *arg += len;
5059 if (evaluate)
5060 {
5061 rettv->v_type = VAR_NUMBER;
5062 rettv->vval.v_number = n;
5063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * String constant: "string".
5070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005078 break;
5079
5080 /*
5081 * List: [expr, expr]
5082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005083 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085
5086 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005087 * Dictionary: {key: val, key: val}
5088 */
5089 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5090 break;
5091
5092 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097
5098 /*
5099 * Environment variable: $VAR.
5100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103
5104 /*
5105 * Register contents: @r.
5106 */
5107 case '@': ++*arg;
5108 if (evaluate)
5109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005110 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005111 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113 if (**arg != NUL)
5114 ++*arg;
5115 break;
5116
5117 /*
5118 * nested expression: (expression).
5119 */
5120 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (**arg == ')')
5123 ++*arg;
5124 else if (ret == OK)
5125 {
5126 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 ret = FAIL;
5129 }
5130 break;
5131
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
5134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005135
5136 if (ret == NOTDONE)
5137 {
5138 /*
5139 * Must be a variable or function name.
5140 * Can also be a curly-braces kind of name: {expr}.
5141 */
5142 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 if (alias != NULL)
5145 s = alias;
5146
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005147 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 ret = FAIL;
5149 else
5150 {
5151 if (**arg == '(') /* recursive! */
5152 {
5153 /* If "s" is the name of a variable of type VAR_FUNC
5154 * use its contents. */
5155 s = deref_func_name(s, &len);
5156
5157 /* Invoke the function. */
5158 ret = get_func_tv(s, len, rettv, arg,
5159 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005160 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005161
5162 /* If evaluate is FALSE rettv->v_type was not set in
5163 * get_func_tv, but it's needed in handle_subscript() to parse
5164 * what follows. So set it here. */
5165 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5166 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005167 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168 rettv->v_type = VAR_FUNC;
5169 }
5170
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 /* Stop the expression evaluation when immediately
5172 * aborting on error, or when an interrupt occurred or
5173 * an exception was thrown but not caught. */
5174 if (aborting())
5175 {
5176 if (ret == OK)
5177 clear_tv(rettv);
5178 ret = FAIL;
5179 }
5180 }
5181 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005182 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005183 else
5184 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005186 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 }
5188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 *arg = skipwhite(*arg);
5190
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005191 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5192 * expr(expr). */
5193 if (ret == OK)
5194 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 /*
5197 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5198 */
5199 if (ret == OK && evaluate && end_leader > start_leader)
5200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 int val = 0;
5203#ifdef FEAT_FLOAT
5204 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005206 if (rettv->v_type == VAR_FLOAT)
5207 f = rettv->vval.v_float;
5208 else
5209#endif
5210 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 clear_tv(rettv);
5214 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 else
5217 {
5218 while (end_leader > start_leader)
5219 {
5220 --end_leader;
5221 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005222 {
5223#ifdef FEAT_FLOAT
5224 if (rettv->v_type == VAR_FLOAT)
5225 f = !f;
5226 else
5227#endif
5228 val = !val;
5229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231 {
5232#ifdef FEAT_FLOAT
5233 if (rettv->v_type == VAR_FLOAT)
5234 f = -f;
5235 else
5236#endif
5237 val = -val;
5238 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 {
5243 clear_tv(rettv);
5244 rettv->vval.v_float = f;
5245 }
5246 else
5247#endif
5248 {
5249 clear_tv(rettv);
5250 rettv->v_type = VAR_NUMBER;
5251 rettv->vval.v_number = val;
5252 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255
5256 return ret;
5257}
5258
5259/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005260 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5261 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5263 */
5264 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270{
5271 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005272 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 long len = -1;
5275 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005279 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 if (verbose)
5282 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 return FAIL;
5284 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005285#ifdef FEAT_FLOAT
5286 else if (rettv->v_type == VAR_FLOAT)
5287 {
5288 if (verbose)
5289 EMSG(_(e_float_as_string));
5290 return FAIL;
5291 }
5292#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 /*
5297 * dict.name
5298 */
5299 key = *arg + 1;
5300 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5301 ;
5302 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 }
5306 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005307 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 /*
5309 * something[idx]
5310 *
5311 * Get the (first) variable from inside the [].
5312 */
5313 *arg = skipwhite(*arg + 1);
5314 if (**arg == ':')
5315 empty1 = TRUE;
5316 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5317 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5319 {
5320 /* not a number or string */
5321 clear_tv(&var1);
5322 return FAIL;
5323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324
5325 /*
5326 * Get the second variable from inside the [:].
5327 */
5328 if (**arg == ':')
5329 {
5330 range = TRUE;
5331 *arg = skipwhite(*arg + 1);
5332 if (**arg == ']')
5333 empty2 = TRUE;
5334 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 if (!empty1)
5337 clear_tv(&var1);
5338 return FAIL;
5339 }
5340 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5341 {
5342 /* not a number or string */
5343 if (!empty1)
5344 clear_tv(&var1);
5345 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 return FAIL;
5347 }
5348 }
5349
5350 /* Check for the ']'. */
5351 if (**arg != ']')
5352 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005353 if (verbose)
5354 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 clear_tv(&var1);
5356 if (range)
5357 clear_tv(&var2);
5358 return FAIL;
5359 }
5360 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 }
5362
5363 if (evaluate)
5364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 n1 = 0;
5366 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005368 n1 = get_tv_number(&var1);
5369 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 }
5371 if (range)
5372 {
5373 if (empty2)
5374 n2 = -1;
5375 else
5376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 n2 = get_tv_number(&var2);
5378 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 }
5380 }
5381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 {
5384 case VAR_NUMBER:
5385 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 len = (long)STRLEN(s);
5388 if (range)
5389 {
5390 /* The resulting variable is a substring. If the indexes
5391 * are out of range the result is empty. */
5392 if (n1 < 0)
5393 {
5394 n1 = len + n1;
5395 if (n1 < 0)
5396 n1 = 0;
5397 }
5398 if (n2 < 0)
5399 n2 = len + n2;
5400 else if (n2 >= len)
5401 n2 = len;
5402 if (n1 >= len || n2 < 0 || n1 > n2)
5403 s = NULL;
5404 else
5405 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5406 }
5407 else
5408 {
5409 /* The resulting variable is a string of a single
5410 * character. If the index is too big or negative the
5411 * result is empty. */
5412 if (n1 >= len || n1 < 0)
5413 s = NULL;
5414 else
5415 s = vim_strnsave(s + n1, 1);
5416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005417 clear_tv(rettv);
5418 rettv->v_type = VAR_STRING;
5419 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 break;
5421
5422 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 if (n1 < 0)
5425 n1 = len + n1;
5426 if (!empty1 && (n1 < 0 || n1 >= len))
5427 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005428 /* For a range we allow invalid values and return an empty
5429 * list. A list index out of range is an error. */
5430 if (!range)
5431 {
5432 if (verbose)
5433 EMSGN(_(e_listidx), n1);
5434 return FAIL;
5435 }
5436 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 }
5438 if (range)
5439 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 list_T *l;
5441 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
5443 if (n2 < 0)
5444 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005445 else if (n2 >= len)
5446 n2 = len - 1;
5447 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005448 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 l = list_alloc();
5450 if (l == NULL)
5451 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 n1 <= n2; ++n1)
5454 {
5455 if (list_append_tv(l, &item->li_tv) == FAIL)
5456 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005457 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 return FAIL;
5459 }
5460 item = item->li_next;
5461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 clear_tv(rettv);
5463 rettv->v_type = VAR_LIST;
5464 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005465 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 }
5467 else
5468 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005469 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 clear_tv(rettv);
5471 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474
5475 case VAR_DICT:
5476 if (range)
5477 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005478 if (verbose)
5479 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 if (len == -1)
5481 clear_tv(&var1);
5482 return FAIL;
5483 }
5484 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005485 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486
5487 if (len == -1)
5488 {
5489 key = get_tv_string(&var1);
5490 if (*key == NUL)
5491 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005492 if (verbose)
5493 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 clear_tv(&var1);
5495 return FAIL;
5496 }
5497 }
5498
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005499 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005500
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005501 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005502 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503 if (len == -1)
5504 clear_tv(&var1);
5505 if (item == NULL)
5506 return FAIL;
5507
5508 copy_tv(&item->di_tv, &var1);
5509 clear_tv(rettv);
5510 *rettv = var1;
5511 }
5512 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514 }
5515
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 return OK;
5517}
5518
5519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 * Get an option value.
5521 * "arg" points to the '&' or '+' before the option name.
5522 * "arg" is advanced to character after the option name.
5523 * Return OK or FAIL.
5524 */
5525 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005528 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 int evaluate;
5530{
5531 char_u *option_end;
5532 long numval;
5533 char_u *stringval;
5534 int opt_type;
5535 int c;
5536 int working = (**arg == '+'); /* has("+option") */
5537 int ret = OK;
5538 int opt_flags;
5539
5540 /*
5541 * Isolate the option name and find its value.
5542 */
5543 option_end = find_option_end(arg, &opt_flags);
5544 if (option_end == NULL)
5545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 EMSG2(_("E112: Option name missing: %s"), *arg);
5548 return FAIL;
5549 }
5550
5551 if (!evaluate)
5552 {
5553 *arg = option_end;
5554 return OK;
5555 }
5556
5557 c = *option_end;
5558 *option_end = NUL;
5559 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562 if (opt_type == -3) /* invalid name */
5563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 EMSG2(_("E113: Unknown option: %s"), *arg);
5566 ret = FAIL;
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 if (opt_type == -2) /* hidden string option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_STRING;
5573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else if (opt_type == -1) /* hidden number option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_NUMBER;
5578 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 else if (opt_type == 1) /* number option */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->v_type = VAR_NUMBER;
5583 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 else /* string option */
5586 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 rettv->v_type = VAR_STRING;
5588 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590 }
5591 else if (working && (opt_type == -2 || opt_type == -1))
5592 ret = FAIL;
5593
5594 *option_end = c; /* put back for error messages */
5595 *arg = option_end;
5596
5597 return ret;
5598}
5599
5600/*
5601 * Allocate a variable for a string constant.
5602 * Return OK or FAIL.
5603 */
5604 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 int evaluate;
5609{
5610 char_u *p;
5611 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 int extra = 0;
5613
5614 /*
5615 * Find the end of the string, skipping backslashed characters.
5616 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005617 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 if (*p == '\\' && p[1] != NUL)
5620 {
5621 ++p;
5622 /* A "\<x>" form occupies at least 4 characters, and produces up
5623 * to 6 characters: reserve space for 2 extra */
5624 if (*p == '<')
5625 extra += 2;
5626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628
5629 if (*p != '"')
5630 {
5631 EMSG2(_("E114: Missing quote: %s"), *arg);
5632 return FAIL;
5633 }
5634
5635 /* If only parsing, set *arg and return here */
5636 if (!evaluate)
5637 {
5638 *arg = p + 1;
5639 return OK;
5640 }
5641
5642 /*
5643 * Copy the string into allocated memory, handling backslashed
5644 * characters.
5645 */
5646 name = alloc((unsigned)(p - *arg + extra));
5647 if (name == NULL)
5648 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649 rettv->v_type = VAR_STRING;
5650 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
5654 if (*p == '\\')
5655 {
5656 switch (*++p)
5657 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658 case 'b': *name++ = BS; ++p; break;
5659 case 'e': *name++ = ESC; ++p; break;
5660 case 'f': *name++ = FF; ++p; break;
5661 case 'n': *name++ = NL; ++p; break;
5662 case 'r': *name++ = CAR; ++p; break;
5663 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 case 'X': /* hex: "\x1", "\x12" */
5666 case 'x':
5667 case 'u': /* Unicode: "\u0023" */
5668 case 'U':
5669 if (vim_isxdigit(p[1]))
5670 {
5671 int n, nr;
5672 int c = toupper(*p);
5673
5674 if (c == 'X')
5675 n = 2;
5676 else
5677 n = 4;
5678 nr = 0;
5679 while (--n >= 0 && vim_isxdigit(p[1]))
5680 {
5681 ++p;
5682 nr = (nr << 4) + hex2nr(*p);
5683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#ifdef FEAT_MBYTE
5686 /* For "\u" store the number according to
5687 * 'encoding'. */
5688 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 else
5691#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 break;
5695
5696 /* octal: "\1", "\12", "\123" */
5697 case '0':
5698 case '1':
5699 case '2':
5700 case '3':
5701 case '4':
5702 case '5':
5703 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 case '7': *name = *p++ - '0';
5705 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 *name = (*name << 3) + *p++ - '0';
5708 if (*p >= '0' && *p <= '7')
5709 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713
5714 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (extra != 0)
5717 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 }
5721 /* FALLTHROUGH */
5722
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 break;
5725 }
5726 }
5727 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 *arg = p + 1;
5733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 return OK;
5735}
5736
5737/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 * Return OK or FAIL.
5740 */
5741 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005742get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005744 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 int evaluate;
5746{
5747 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 int reduce = 0;
5750
5751 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 */
5754 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005759 break;
5760 ++reduce;
5761 ++p;
5762 }
5763 }
5764
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 return FAIL;
5769 }
5770
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 if (!evaluate)
5773 {
5774 *arg = p + 1;
5775 return OK;
5776 }
5777
5778 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 */
5781 str = alloc((unsigned)((p - *arg) - reduce));
5782 if (str == NULL)
5783 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 rettv->v_type = VAR_STRING;
5785 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 break;
5793 ++p;
5794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 *arg = p + 1;
5799
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 return OK;
5801}
5802
5803/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 * Allocate a variable for a List and fill it from "*arg".
5805 * Return OK or FAIL.
5806 */
5807 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005808get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005810 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 int evaluate;
5812{
Bram Moolenaar33570922005-01-25 22:26:29 +00005813 list_T *l = NULL;
5814 typval_T tv;
5815 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816
5817 if (evaluate)
5818 {
5819 l = list_alloc();
5820 if (l == NULL)
5821 return FAIL;
5822 }
5823
5824 *arg = skipwhite(*arg + 1);
5825 while (**arg != ']' && **arg != NUL)
5826 {
5827 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5828 goto failret;
5829 if (evaluate)
5830 {
5831 item = listitem_alloc();
5832 if (item != NULL)
5833 {
5834 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005835 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 list_append(l, item);
5837 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005838 else
5839 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005840 }
5841
5842 if (**arg == ']')
5843 break;
5844 if (**arg != ',')
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 goto failret;
5848 }
5849 *arg = skipwhite(*arg + 1);
5850 }
5851
5852 if (**arg != ']')
5853 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005855failret:
5856 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005857 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 return FAIL;
5859 }
5860
5861 *arg = skipwhite(*arg + 1);
5862 if (evaluate)
5863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005864 rettv->v_type = VAR_LIST;
5865 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005866 ++l->lv_refcount;
5867 }
5868
5869 return OK;
5870}
5871
5872/*
5873 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005874 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005876 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005877list_alloc()
5878{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005879 list_T *l;
5880
5881 l = (list_T *)alloc_clear(sizeof(list_T));
5882 if (l != NULL)
5883 {
5884 /* Prepend the list to the list of lists for garbage collection. */
5885 if (first_list != NULL)
5886 first_list->lv_used_prev = l;
5887 l->lv_used_prev = NULL;
5888 l->lv_used_next = first_list;
5889 first_list = l;
5890 }
5891 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892}
5893
5894/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005895 * Allocate an empty list for a return value.
5896 * Returns OK or FAIL.
5897 */
5898 static int
5899rettv_list_alloc(rettv)
5900 typval_T *rettv;
5901{
5902 list_T *l = list_alloc();
5903
5904 if (l == NULL)
5905 return FAIL;
5906
5907 rettv->vval.v_list = l;
5908 rettv->v_type = VAR_LIST;
5909 ++l->lv_refcount;
5910 return OK;
5911}
5912
5913/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 * Unreference a list: decrement the reference count and free it when it
5915 * becomes zero.
5916 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005917 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005919 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005920{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005921 if (l != NULL && --l->lv_refcount <= 0)
5922 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923}
5924
5925/*
5926 * Free a list, including all items it points to.
5927 * Ignores the reference count.
5928 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005929 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005930list_free(l, recurse)
5931 list_T *l;
5932 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933{
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005936 /* Remove the list from the list of lists for garbage collection. */
5937 if (l->lv_used_prev == NULL)
5938 first_list = l->lv_used_next;
5939 else
5940 l->lv_used_prev->lv_used_next = l->lv_used_next;
5941 if (l->lv_used_next != NULL)
5942 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5943
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005946 /* Remove the item before deleting it. */
5947 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005948 if (recurse || (item->li_tv.v_type != VAR_LIST
5949 && item->li_tv.v_type != VAR_DICT))
5950 clear_tv(&item->li_tv);
5951 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 }
5953 vim_free(l);
5954}
5955
5956/*
5957 * Allocate a list item.
5958 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005959 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960listitem_alloc()
5961{
Bram Moolenaar33570922005-01-25 22:26:29 +00005962 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963}
5964
5965/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005966 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005968 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005970 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005972 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 vim_free(item);
5974}
5975
5976/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005977 * Remove a list item from a List and free it. Also clears the value.
5978 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005979 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005980listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 list_T *l;
5982 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005983{
5984 list_remove(l, item, item);
5985 listitem_free(item);
5986}
5987
5988/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989 * Get the number of items in a list.
5990 */
5991 static long
5992list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995 if (l == NULL)
5996 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005997 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998}
5999
6000/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001 * Return TRUE when two lists have exactly the same values.
6002 */
6003 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006004list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 list_T *l1;
6006 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006008 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009{
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006012 if (l1 == NULL || l2 == NULL)
6013 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006014 if (l1 == l2)
6015 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006016 if (list_len(l1) != list_len(l2))
6017 return FALSE;
6018
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006019 for (item1 = l1->lv_first, item2 = l2->lv_first;
6020 item1 != NULL && item2 != NULL;
6021 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006022 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006023 return FALSE;
6024 return item1 == NULL && item2 == NULL;
6025}
6026
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006027#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6028 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006029/*
6030 * Return the dictitem that an entry in a hashtable points to.
6031 */
6032 dictitem_T *
6033dict_lookup(hi)
6034 hashitem_T *hi;
6035{
6036 return HI2DI(hi);
6037}
6038#endif
6039
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041 * Return TRUE when two dictionaries have exactly the same key/values.
6042 */
6043 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 dict_T *d1;
6046 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006047 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006048 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049{
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 hashitem_T *hi;
6051 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006052 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006054 if (d1 == NULL || d2 == NULL)
6055 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006056 if (d1 == d2)
6057 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 if (dict_len(d1) != dict_len(d2))
6059 return FALSE;
6060
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006061 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006064 if (!HASHITEM_EMPTY(hi))
6065 {
6066 item2 = dict_find(d2, hi->hi_key, -1);
6067 if (item2 == NULL)
6068 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006069 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006070 return FALSE;
6071 --todo;
6072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006073 }
6074 return TRUE;
6075}
6076
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077static int tv_equal_recurse_limit;
6078
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006079/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006082 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006083 */
6084 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 typval_T *tv1;
6087 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 int ic; /* ignore case */
6089 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090{
6091 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006092 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006094 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006096 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006097 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006099 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 * recursiveness to a limit. We guess they are equal then.
6101 * A fixed limit has the problem of still taking an awful long time.
6102 * Reduce the limit every time running into it. That should work fine for
6103 * deeply linked structures that are not recursively linked and catch
6104 * recursiveness quickly. */
6105 if (!recursive)
6106 tv_equal_recurse_limit = 1000;
6107 if (recursive_cnt >= tv_equal_recurse_limit)
6108 {
6109 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006110 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 ++recursive_cnt;
6117 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6118 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006119 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006120
6121 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 ++recursive_cnt;
6123 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6124 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006125 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006126
6127 case VAR_FUNC:
6128 return (tv1->vval.v_string != NULL
6129 && tv2->vval.v_string != NULL
6130 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6131
6132 case VAR_NUMBER:
6133 return tv1->vval.v_number == tv2->vval.v_number;
6134
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006135#ifdef FEAT_FLOAT
6136 case VAR_FLOAT:
6137 return tv1->vval.v_float == tv2->vval.v_float;
6138#endif
6139
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140 case VAR_STRING:
6141 s1 = get_tv_string_buf(tv1, buf1);
6142 s2 = get_tv_string_buf(tv2, buf2);
6143 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006144 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006145
6146 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006147 return TRUE;
6148}
6149
6150/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 * Locate item with index "n" in list "l" and return it.
6152 * A negative index is counted from the end; -1 is the last item.
6153 * Returns NULL when "n" is out of range.
6154 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006155 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006157 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 long n;
6159{
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 long idx;
6162
6163 if (l == NULL)
6164 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006165
6166 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006168 n = l->lv_len + n;
6169
6170 /* Check for index out of range. */
6171 if (n < 0 || n >= l->lv_len)
6172 return NULL;
6173
6174 /* When there is a cached index may start search from there. */
6175 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006177 if (n < l->lv_idx / 2)
6178 {
6179 /* closest to the start of the list */
6180 item = l->lv_first;
6181 idx = 0;
6182 }
6183 else if (n > (l->lv_idx + l->lv_len) / 2)
6184 {
6185 /* closest to the end of the list */
6186 item = l->lv_last;
6187 idx = l->lv_len - 1;
6188 }
6189 else
6190 {
6191 /* closest to the cached index */
6192 item = l->lv_idx_item;
6193 idx = l->lv_idx;
6194 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 }
6196 else
6197 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006198 if (n < l->lv_len / 2)
6199 {
6200 /* closest to the start of the list */
6201 item = l->lv_first;
6202 idx = 0;
6203 }
6204 else
6205 {
6206 /* closest to the end of the list */
6207 item = l->lv_last;
6208 idx = l->lv_len - 1;
6209 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006211
6212 while (n > idx)
6213 {
6214 /* search forward */
6215 item = item->li_next;
6216 ++idx;
6217 }
6218 while (n < idx)
6219 {
6220 /* search backward */
6221 item = item->li_prev;
6222 --idx;
6223 }
6224
6225 /* cache the used index */
6226 l->lv_idx = idx;
6227 l->lv_idx_item = item;
6228
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 return item;
6230}
6231
6232/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006233 * Get list item "l[idx]" as a number.
6234 */
6235 static long
6236list_find_nr(l, idx, errorp)
6237 list_T *l;
6238 long idx;
6239 int *errorp; /* set to TRUE when something wrong */
6240{
6241 listitem_T *li;
6242
6243 li = list_find(l, idx);
6244 if (li == NULL)
6245 {
6246 if (errorp != NULL)
6247 *errorp = TRUE;
6248 return -1L;
6249 }
6250 return get_tv_number_chk(&li->li_tv, errorp);
6251}
6252
6253/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006254 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6255 */
6256 char_u *
6257list_find_str(l, idx)
6258 list_T *l;
6259 long idx;
6260{
6261 listitem_T *li;
6262
6263 li = list_find(l, idx - 1);
6264 if (li == NULL)
6265 {
6266 EMSGN(_(e_listidx), idx);
6267 return NULL;
6268 }
6269 return get_tv_string(&li->li_tv);
6270}
6271
6272/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006273 * Locate "item" list "l" and return its index.
6274 * Returns -1 when "item" is not in the list.
6275 */
6276 static long
6277list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 list_T *l;
6279 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280{
6281 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006283
6284 if (l == NULL)
6285 return -1;
6286 idx = 0;
6287 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6288 ++idx;
6289 if (li == NULL)
6290 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006291 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006292}
6293
6294/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 * Append item "item" to the end of list "l".
6296 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006297 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 list_T *l;
6300 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301{
6302 if (l->lv_last == NULL)
6303 {
6304 /* empty list */
6305 l->lv_first = item;
6306 l->lv_last = item;
6307 item->li_prev = NULL;
6308 }
6309 else
6310 {
6311 l->lv_last->li_next = item;
6312 item->li_prev = l->lv_last;
6313 l->lv_last = item;
6314 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006315 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316 item->li_next = NULL;
6317}
6318
6319/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006321 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006323 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006325 list_T *l;
6326 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006328 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006332 copy_tv(tv, &li->li_tv);
6333 list_append(l, li);
6334 return OK;
6335}
6336
6337/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006338 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 * Return FAIL when out of memory.
6340 */
6341 int
6342list_append_dict(list, dict)
6343 list_T *list;
6344 dict_T *dict;
6345{
6346 listitem_T *li = listitem_alloc();
6347
6348 if (li == NULL)
6349 return FAIL;
6350 li->li_tv.v_type = VAR_DICT;
6351 li->li_tv.v_lock = 0;
6352 li->li_tv.vval.v_dict = dict;
6353 list_append(list, li);
6354 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return OK;
6356}
6357
6358/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006359 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006360 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 * Returns FAIL when out of memory.
6362 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006363 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006364list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006365 list_T *l;
6366 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368{
6369 listitem_T *li = listitem_alloc();
6370
6371 if (li == NULL)
6372 return FAIL;
6373 list_append(l, li);
6374 li->li_tv.v_type = VAR_STRING;
6375 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006376 if (str == NULL)
6377 li->li_tv.vval.v_string = NULL;
6378 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006379 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006380 return FAIL;
6381 return OK;
6382}
6383
6384/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006385 * Append "n" to list "l".
6386 * Returns FAIL when out of memory.
6387 */
6388 static int
6389list_append_number(l, n)
6390 list_T *l;
6391 varnumber_T n;
6392{
6393 listitem_T *li;
6394
6395 li = listitem_alloc();
6396 if (li == NULL)
6397 return FAIL;
6398 li->li_tv.v_type = VAR_NUMBER;
6399 li->li_tv.v_lock = 0;
6400 li->li_tv.vval.v_number = n;
6401 list_append(l, li);
6402 return OK;
6403}
6404
6405/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 * If "item" is NULL append at the end.
6408 * Return FAIL when out of memory.
6409 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006410 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 list_T *l;
6413 typval_T *tv;
6414 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415{
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417
6418 if (ni == NULL)
6419 return FAIL;
6420 copy_tv(tv, &ni->li_tv);
6421 if (item == NULL)
6422 /* Append new item at end of list. */
6423 list_append(l, ni);
6424 else
6425 {
6426 /* Insert new item before existing item. */
6427 ni->li_prev = item->li_prev;
6428 ni->li_next = item;
6429 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 ++l->lv_idx;
6433 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006437 l->lv_idx_item = NULL;
6438 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006440 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 }
6442 return OK;
6443}
6444
6445/*
6446 * Extend "l1" with "l2".
6447 * If "bef" is NULL append at the end, otherwise insert before this item.
6448 * Returns FAIL when out of memory.
6449 */
6450 static int
6451list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 list_T *l1;
6453 list_T *l2;
6454 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455{
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006457 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006459 /* We also quit the loop when we have inserted the original item count of
6460 * the list, avoid a hang when we extend a list with itself. */
6461 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6463 return FAIL;
6464 return OK;
6465}
6466
6467/*
6468 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6469 * Return FAIL when out of memory.
6470 */
6471 static int
6472list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 list_T *l1;
6474 list_T *l2;
6475 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476{
Bram Moolenaar33570922005-01-25 22:26:29 +00006477 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006479 if (l1 == NULL || l2 == NULL)
6480 return FAIL;
6481
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006483 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (l == NULL)
6485 return FAIL;
6486 tv->v_type = VAR_LIST;
6487 tv->vval.v_list = l;
6488
6489 /* append all items from the second list */
6490 return list_extend(l, l2, NULL);
6491}
6492
6493/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006494 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 * Returns NULL when out of memory.
6498 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006500list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006503 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504{
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 list_T *copy;
6506 listitem_T *item;
6507 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508
6509 if (orig == NULL)
6510 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511
6512 copy = list_alloc();
6513 if (copy != NULL)
6514 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006515 if (copyID != 0)
6516 {
6517 /* Do this before adding the items, because one of the items may
6518 * refer back to this list. */
6519 orig->lv_copyID = copyID;
6520 orig->lv_copylist = copy;
6521 }
6522 for (item = orig->lv_first; item != NULL && !got_int;
6523 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 {
6525 ni = listitem_alloc();
6526 if (ni == NULL)
6527 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006528 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 {
6530 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6531 {
6532 vim_free(ni);
6533 break;
6534 }
6535 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006537 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 list_append(copy, ni);
6539 }
6540 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (item != NULL)
6542 {
6543 list_unref(copy);
6544 copy = NULL;
6545 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 }
6547
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 return copy;
6549}
6550
6551/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006553 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006555 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006556list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 list_T *l;
6558 listitem_T *item;
6559 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 /* notify watchers */
6564 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 list_fix_watch(l, ip);
6568 if (ip == item2)
6569 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571
6572 if (item2->li_next == NULL)
6573 l->lv_last = item->li_prev;
6574 else
6575 item2->li_next->li_prev = item->li_prev;
6576 if (item->li_prev == NULL)
6577 l->lv_first = item2->li_next;
6578 else
6579 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006580 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581}
6582
6583/*
6584 * Return an allocated string with the string representation of a list.
6585 * May return NULL.
6586 */
6587 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006588list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591{
6592 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593
6594 if (tv->vval.v_list == NULL)
6595 return NULL;
6596 ga_init2(&ga, (int)sizeof(char), 80);
6597 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006598 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006599 {
6600 vim_free(ga.ga_data);
6601 return NULL;
6602 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 ga_append(&ga, ']');
6604 ga_append(&ga, NUL);
6605 return (char_u *)ga.ga_data;
6606}
6607
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006608typedef struct join_S {
6609 char_u *s;
6610 char_u *tofree;
6611} join_T;
6612
6613 static int
6614list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6615 garray_T *gap; /* to store the result in */
6616 list_T *l;
6617 char_u *sep;
6618 int echo_style;
6619 int copyID;
6620 garray_T *join_gap; /* to keep each list item string */
6621{
6622 int i;
6623 join_T *p;
6624 int len;
6625 int sumlen = 0;
6626 int first = TRUE;
6627 char_u *tofree;
6628 char_u numbuf[NUMBUFLEN];
6629 listitem_T *item;
6630 char_u *s;
6631
6632 /* Stringify each item in the list. */
6633 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6634 {
6635 if (echo_style)
6636 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6637 else
6638 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6639 if (s == NULL)
6640 return FAIL;
6641
6642 len = (int)STRLEN(s);
6643 sumlen += len;
6644
6645 ga_grow(join_gap, 1);
6646 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6647 if (tofree != NULL || s != numbuf)
6648 {
6649 p->s = s;
6650 p->tofree = tofree;
6651 }
6652 else
6653 {
6654 p->s = vim_strnsave(s, len);
6655 p->tofree = p->s;
6656 }
6657
6658 line_breakcheck();
6659 }
6660
6661 /* Allocate result buffer with its total size, avoid re-allocation and
6662 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6663 if (join_gap->ga_len >= 2)
6664 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6665 if (ga_grow(gap, sumlen + 2) == FAIL)
6666 return FAIL;
6667
6668 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6669 {
6670 if (first)
6671 first = FALSE;
6672 else
6673 ga_concat(gap, sep);
6674 p = ((join_T *)join_gap->ga_data) + i;
6675
6676 if (p->s != NULL)
6677 ga_concat(gap, p->s);
6678 line_breakcheck();
6679 }
6680
6681 return OK;
6682}
6683
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006686 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006687 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006690list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006692 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006694 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006695 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 garray_T join_ga;
6698 int retval;
6699 join_T *p;
6700 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006702 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6703 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6704
6705 /* Dispose each item in join_ga. */
6706 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006708 p = (join_T *)join_ga.ga_data;
6709 for (i = 0; i < join_ga.ga_len; ++i)
6710 {
6711 vim_free(p->tofree);
6712 ++p;
6713 }
6714 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006716
6717 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718}
6719
6720/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006721 * Garbage collection for lists and dictionaries.
6722 *
6723 * We use reference counts to be able to free most items right away when they
6724 * are no longer used. But for composite items it's possible that it becomes
6725 * unused while the reference count is > 0: When there is a recursive
6726 * reference. Example:
6727 * :let l = [1, 2, 3]
6728 * :let d = {9: l}
6729 * :let l[1] = d
6730 *
6731 * Since this is quite unusual we handle this with garbage collection: every
6732 * once in a while find out which lists and dicts are not referenced from any
6733 * variable.
6734 *
6735 * Here is a good reference text about garbage collection (refers to Python
6736 * but it applies to all reference-counting mechanisms):
6737 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739
6740/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006741 * Do garbage collection for lists and dicts.
6742 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006743 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 int
6745garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006746{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006748 buf_T *buf;
6749 win_T *wp;
6750 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006751 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006752 int did_free;
6753 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006754#ifdef FEAT_WINDOWS
6755 tabpage_T *tp;
6756#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006758 /* Only do this once. */
6759 want_garbage_collect = FALSE;
6760 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006761 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006762
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 /* We advance by two because we add one for items referenced through
6764 * previous_funccal. */
6765 current_copyID += COPYID_INC;
6766 copyID = current_copyID;
6767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006768 /*
6769 * 1. Go through all accessible variables and mark all lists and dicts
6770 * with copyID.
6771 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006772
6773 /* Don't free variables in the previous_funccal list unless they are only
6774 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006775 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006776 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6777 {
6778 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6779 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6780 }
6781
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006782 /* script-local variables */
6783 for (i = 1; i <= ga_scripts.ga_len; ++i)
6784 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6785
6786 /* buffer-local variables */
6787 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006788 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789
6790 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006791 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006792 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006793#ifdef FEAT_AUTOCMD
6794 if (aucmd_win != NULL)
6795 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6796#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006797
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006798#ifdef FEAT_WINDOWS
6799 /* tabpage-local variables */
6800 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006801 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006802#endif
6803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 /* global variables */
6805 set_ref_in_ht(&globvarht, copyID);
6806
6807 /* function-local variables */
6808 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6809 {
6810 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6811 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6812 }
6813
Bram Moolenaard812df62008-11-09 12:46:09 +00006814 /* v: vars */
6815 set_ref_in_ht(&vimvarht, copyID);
6816
Bram Moolenaar1dced572012-04-05 16:54:08 +02006817#ifdef FEAT_LUA
6818 set_ref_in_lua(copyID);
6819#endif
6820
Bram Moolenaardb913952012-06-29 12:54:53 +02006821#ifdef FEAT_PYTHON
6822 set_ref_in_python(copyID);
6823#endif
6824
6825#ifdef FEAT_PYTHON3
6826 set_ref_in_python3(copyID);
6827#endif
6828
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006829 /*
6830 * 2. Free lists and dictionaries that are not referenced.
6831 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 did_free = free_unref_items(copyID);
6833
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006834 /*
6835 * 3. Check if any funccal can be freed now.
6836 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 for (pfc = &previous_funccal; *pfc != NULL; )
6838 {
6839 if (can_free_funccal(*pfc, copyID))
6840 {
6841 fc = *pfc;
6842 *pfc = fc->caller;
6843 free_funccal(fc, TRUE);
6844 did_free = TRUE;
6845 did_free_funccal = TRUE;
6846 }
6847 else
6848 pfc = &(*pfc)->caller;
6849 }
6850 if (did_free_funccal)
6851 /* When a funccal was freed some more items might be garbage
6852 * collected, so run again. */
6853 (void)garbage_collect();
6854
6855 return did_free;
6856}
6857
6858/*
6859 * Free lists and dictionaries that are no longer referenced.
6860 */
6861 static int
6862free_unref_items(copyID)
6863 int copyID;
6864{
6865 dict_T *dd;
6866 list_T *ll;
6867 int did_free = FALSE;
6868
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 */
6872 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006875 /* Free the Dictionary and ordinary items it contains, but don't
6876 * recurse into Lists and Dictionaries, they will be in the list
6877 * of dicts or list of lists. */
6878 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 did_free = TRUE;
6880
6881 /* restart, next dict may also have been freed */
6882 dd = first_dict;
6883 }
6884 else
6885 dd = dd->dv_used_next;
6886
6887 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 * Go through the list of lists and free items without the copyID.
6889 * But don't free a list that has a watcher (used in a for loop), these
6890 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 */
6892 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6894 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006896 /* Free the List and ordinary items it contains, but don't recurse
6897 * into Lists and Dictionaries, they will be in the list of dicts
6898 * or list of lists. */
6899 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 did_free = TRUE;
6901
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006902 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 ll = first_list;
6904 }
6905 else
6906 ll = ll->lv_used_next;
6907
6908 return did_free;
6909}
6910
6911/*
6912 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6913 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006914 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915set_ref_in_ht(ht, copyID)
6916 hashtab_T *ht;
6917 int copyID;
6918{
6919 int todo;
6920 hashitem_T *hi;
6921
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006922 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 for (hi = ht->ht_array; todo > 0; ++hi)
6924 if (!HASHITEM_EMPTY(hi))
6925 {
6926 --todo;
6927 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6928 }
6929}
6930
6931/*
6932 * Mark all lists and dicts referenced through list "l" with "copyID".
6933 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006934 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935set_ref_in_list(l, copyID)
6936 list_T *l;
6937 int copyID;
6938{
6939 listitem_T *li;
6940
6941 for (li = l->lv_first; li != NULL; li = li->li_next)
6942 set_ref_in_item(&li->li_tv, copyID);
6943}
6944
6945/*
6946 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6947 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006948 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949set_ref_in_item(tv, copyID)
6950 typval_T *tv;
6951 int copyID;
6952{
6953 dict_T *dd;
6954 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955
6956 switch (tv->v_type)
6957 {
6958 case VAR_DICT:
6959 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006960 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 /* Didn't see this dict yet. */
6963 dd->dv_copyID = copyID;
6964 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967
6968 case VAR_LIST:
6969 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006970 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 /* Didn't see this list yet. */
6973 ll->lv_copyID = copyID;
6974 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006979}
6980
6981/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006982 * Allocate an empty header for a dictionary.
6983 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006984 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006985dict_alloc()
6986{
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006990 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006991 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006992 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 if (first_dict != NULL)
6994 first_dict->dv_used_prev = d;
6995 d->dv_used_next = first_dict;
6996 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006997 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007001 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007003 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007004 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007005 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007006}
7007
7008/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007009 * Allocate an empty dict for a return value.
7010 * Returns OK or FAIL.
7011 */
7012 static int
7013rettv_dict_alloc(rettv)
7014 typval_T *rettv;
7015{
7016 dict_T *d = dict_alloc();
7017
7018 if (d == NULL)
7019 return FAIL;
7020
7021 rettv->vval.v_dict = d;
7022 rettv->v_type = VAR_DICT;
7023 ++d->dv_refcount;
7024 return OK;
7025}
7026
7027
7028/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029 * Unreference a Dictionary: decrement the reference count and free it when it
7030 * becomes zero.
7031 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007032 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007036 if (d != NULL && --d->dv_refcount <= 0)
7037 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038}
7039
7040/*
7041 * Free a Dictionary, including all items it contains.
7042 * Ignores the reference count.
7043 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007044 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007045dict_free(d, recurse)
7046 dict_T *d;
7047 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007049 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007051 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 /* Remove the dict from the list of dicts for garbage collection. */
7054 if (d->dv_used_prev == NULL)
7055 first_dict = d->dv_used_next;
7056 else
7057 d->dv_used_prev->dv_used_next = d->dv_used_next;
7058 if (d->dv_used_next != NULL)
7059 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7060
7061 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007062 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007063 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007065 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 if (!HASHITEM_EMPTY(hi))
7067 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007068 /* Remove the item before deleting it, just in case there is
7069 * something recursive causing trouble. */
7070 di = HI2DI(hi);
7071 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007072 if (recurse || (di->di_tv.v_type != VAR_LIST
7073 && di->di_tv.v_type != VAR_DICT))
7074 clear_tv(&di->di_tv);
7075 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007076 --todo;
7077 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 vim_free(d);
7081}
7082
7083/*
7084 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 * The "key" is copied to the new item.
7086 * Note that the value of the item "di_tv" still needs to be initialized!
7087 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007089 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090dictitem_alloc(key)
7091 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092{
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007095 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007098 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007099 di->di_flags = 0;
7100 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102}
7103
7104/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105 * Make a copy of a Dictionary item.
7106 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110{
Bram Moolenaar33570922005-01-25 22:26:29 +00007111 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007113 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7114 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 if (di != NULL)
7116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007118 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007119 copy_tv(&org->di_tv, &di->di_tv);
7120 }
7121 return di;
7122}
7123
7124/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 * Remove item "item" from Dictionary "dict" and free it.
7126 */
7127 static void
7128dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 dict_T *dict;
7130 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131{
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 if (HASHITEM_EMPTY(hi))
7136 EMSG2(_(e_intern2), "dictitem_remove()");
7137 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007139 dictitem_free(item);
7140}
7141
7142/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143 * Free a dict item. Also clears the value.
7144 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007145 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007148{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007149 clear_tv(&item->di_tv);
7150 vim_free(item);
7151}
7152
7153/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7155 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 * Returns NULL when out of memory.
7158 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007160dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007163 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164{
Bram Moolenaar33570922005-01-25 22:26:29 +00007165 dict_T *copy;
7166 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007168 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169
7170 if (orig == NULL)
7171 return NULL;
7172
7173 copy = dict_alloc();
7174 if (copy != NULL)
7175 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 if (copyID != 0)
7177 {
7178 orig->dv_copyID = copyID;
7179 orig->dv_copydict = copy;
7180 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007181 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007186 --todo;
7187
7188 di = dictitem_alloc(hi->hi_key);
7189 if (di == NULL)
7190 break;
7191 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 {
7193 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7194 copyID) == FAIL)
7195 {
7196 vim_free(di);
7197 break;
7198 }
7199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 else
7201 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7202 if (dict_add(copy, di) == FAIL)
7203 {
7204 dictitem_free(di);
7205 break;
7206 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007209
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 if (todo > 0)
7212 {
7213 dict_unref(copy);
7214 copy = NULL;
7215 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007216 }
7217
7218 return copy;
7219}
7220
7221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007223 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007225 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 dict_T *d;
7228 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229{
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007234 * Add a number or string entry to dictionary "d".
7235 * When "str" is NULL use number "nr", otherwise use "str".
7236 * Returns FAIL when out of memory and when key already exists.
7237 */
7238 int
7239dict_add_nr_str(d, key, nr, str)
7240 dict_T *d;
7241 char *key;
7242 long nr;
7243 char_u *str;
7244{
7245 dictitem_T *item;
7246
7247 item = dictitem_alloc((char_u *)key);
7248 if (item == NULL)
7249 return FAIL;
7250 item->di_tv.v_lock = 0;
7251 if (str == NULL)
7252 {
7253 item->di_tv.v_type = VAR_NUMBER;
7254 item->di_tv.vval.v_number = nr;
7255 }
7256 else
7257 {
7258 item->di_tv.v_type = VAR_STRING;
7259 item->di_tv.vval.v_string = vim_strsave(str);
7260 }
7261 if (dict_add(d, item) == FAIL)
7262 {
7263 dictitem_free(item);
7264 return FAIL;
7265 }
7266 return OK;
7267}
7268
7269/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007270 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007271 * Returns FAIL when out of memory and when key already exists.
7272 */
7273 int
7274dict_add_list(d, key, list)
7275 dict_T *d;
7276 char *key;
7277 list_T *list;
7278{
7279 dictitem_T *item;
7280
7281 item = dictitem_alloc((char_u *)key);
7282 if (item == NULL)
7283 return FAIL;
7284 item->di_tv.v_lock = 0;
7285 item->di_tv.v_type = VAR_LIST;
7286 item->di_tv.vval.v_list = list;
7287 if (dict_add(d, item) == FAIL)
7288 {
7289 dictitem_free(item);
7290 return FAIL;
7291 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007292 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007293 return OK;
7294}
7295
7296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 * Get the number of items in a Dictionary.
7298 */
7299 static long
7300dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 if (d == NULL)
7304 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007305 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306}
7307
7308/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 * Find item "key[len]" in Dictionary "d".
7310 * If "len" is negative use strlen(key).
7311 * Returns NULL when not found.
7312 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007313 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316 char_u *key;
7317 int len;
7318{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319#define AKEYLEN 200
7320 char_u buf[AKEYLEN];
7321 char_u *akey;
7322 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007324
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 if (len < 0)
7326 akey = key;
7327 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007328 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 tofree = akey = vim_strnsave(key, len);
7330 if (akey == NULL)
7331 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007332 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 else
7334 {
7335 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007336 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 akey = buf;
7338 }
7339
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 vim_free(tofree);
7342 if (HASHITEM_EMPTY(hi))
7343 return NULL;
7344 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345}
7346
7347/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 * Get a string item from a dictionary.
7349 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350 * Returns NULL if the entry doesn't exist or out of memory.
7351 */
7352 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007353get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007354 dict_T *d;
7355 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007356 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007357{
7358 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007359 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007360
7361 di = dict_find(d, key, -1);
7362 if (di == NULL)
7363 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007364 s = get_tv_string(&di->di_tv);
7365 if (save && s != NULL)
7366 s = vim_strsave(s);
7367 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007368}
7369
7370/*
7371 * Get a number item from a dictionary.
7372 * Returns 0 if the entry doesn't exist or out of memory.
7373 */
7374 long
7375get_dict_number(d, key)
7376 dict_T *d;
7377 char_u *key;
7378{
7379 dictitem_T *di;
7380
7381 di = dict_find(d, key, -1);
7382 if (di == NULL)
7383 return 0;
7384 return get_tv_number(&di->di_tv);
7385}
7386
7387/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388 * Return an allocated string with the string representation of a Dictionary.
7389 * May return NULL.
7390 */
7391 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007393 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007394 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395{
7396 garray_T ga;
7397 int first = TRUE;
7398 char_u *tofree;
7399 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007402 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 return NULL;
7407 ga_init2(&ga, (int)sizeof(char), 80);
7408 ga_append(&ga, '{');
7409
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007410 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007411 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415 --todo;
7416
7417 if (first)
7418 first = FALSE;
7419 else
7420 ga_concat(&ga, (char_u *)", ");
7421
7422 tofree = string_quote(hi->hi_key, FALSE);
7423 if (tofree != NULL)
7424 {
7425 ga_concat(&ga, tofree);
7426 vim_free(tofree);
7427 }
7428 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007429 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007430 if (s != NULL)
7431 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 if (s == NULL)
7434 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007437 if (todo > 0)
7438 {
7439 vim_free(ga.ga_data);
7440 return NULL;
7441 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442
7443 ga_append(&ga, '}');
7444 ga_append(&ga, NUL);
7445 return (char_u *)ga.ga_data;
7446}
7447
7448/*
7449 * Allocate a variable for a Dictionary and fill it from "*arg".
7450 * Return OK or FAIL. Returns NOTDONE for {expr}.
7451 */
7452 static int
7453get_dict_tv(arg, rettv, evaluate)
7454 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 int evaluate;
7457{
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 dict_T *d = NULL;
7459 typval_T tvkey;
7460 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007461 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007464 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465
7466 /*
7467 * First check if it's not a curly-braces thing: {expr}.
7468 * Must do this without evaluating, otherwise a function may be called
7469 * twice. Unfortunately this means we need to call eval1() twice for the
7470 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 if (*start != '}')
7474 {
7475 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7476 return FAIL;
7477 if (*start == '}')
7478 return NOTDONE;
7479 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480
7481 if (evaluate)
7482 {
7483 d = dict_alloc();
7484 if (d == NULL)
7485 return FAIL;
7486 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007487 tvkey.v_type = VAR_UNKNOWN;
7488 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489
7490 *arg = skipwhite(*arg + 1);
7491 while (**arg != '}' && **arg != NUL)
7492 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 goto failret;
7495 if (**arg != ':')
7496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007497 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499 goto failret;
7500 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007503 key = get_tv_string_buf_chk(&tvkey, buf);
7504 if (key == NULL || *key == NUL)
7505 {
7506 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7507 if (key != NULL)
7508 EMSG(_(e_emptykey));
7509 clear_tv(&tvkey);
7510 goto failret;
7511 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513
7514 *arg = skipwhite(*arg + 1);
7515 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7516 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007517 if (evaluate)
7518 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 goto failret;
7520 }
7521 if (evaluate)
7522 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 if (item != NULL)
7525 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007526 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 clear_tv(&tv);
7529 goto failret;
7530 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 item = dictitem_alloc(key);
7532 clear_tv(&tvkey);
7533 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007536 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 if (dict_add(d, item) == FAIL)
7538 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 }
7540 }
7541
7542 if (**arg == '}')
7543 break;
7544 if (**arg != ',')
7545 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007546 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 goto failret;
7548 }
7549 *arg = skipwhite(*arg + 1);
7550 }
7551
7552 if (**arg != '}')
7553 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007554 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555failret:
7556 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007557 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 return FAIL;
7559 }
7560
7561 *arg = skipwhite(*arg + 1);
7562 if (evaluate)
7563 {
7564 rettv->v_type = VAR_DICT;
7565 rettv->vval.v_dict = d;
7566 ++d->dv_refcount;
7567 }
7568
7569 return OK;
7570}
7571
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 * Return a string with the string representation of a variable.
7574 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007575 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007576 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007577 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007578 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 */
7580 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007581echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007583 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007584 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007586{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 static int recurse = 0;
7588 char_u *r = NULL;
7589
Bram Moolenaar33570922005-01-25 22:26:29 +00007590 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007592 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007593 *tofree = NULL;
7594 return NULL;
7595 }
7596 ++recurse;
7597
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007598 switch (tv->v_type)
7599 {
7600 case VAR_FUNC:
7601 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007602 r = tv->vval.v_string;
7603 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606 if (tv->vval.v_list == NULL)
7607 {
7608 *tofree = NULL;
7609 r = NULL;
7610 }
7611 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7612 {
7613 *tofree = NULL;
7614 r = (char_u *)"[...]";
7615 }
7616 else
7617 {
7618 tv->vval.v_list->lv_copyID = copyID;
7619 *tofree = list2string(tv, copyID);
7620 r = *tofree;
7621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007622 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007625 if (tv->vval.v_dict == NULL)
7626 {
7627 *tofree = NULL;
7628 r = NULL;
7629 }
7630 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7631 {
7632 *tofree = NULL;
7633 r = (char_u *)"{...}";
7634 }
7635 else
7636 {
7637 tv->vval.v_dict->dv_copyID = copyID;
7638 *tofree = dict2string(tv, copyID);
7639 r = *tofree;
7640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007642
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 case VAR_STRING:
7644 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 *tofree = NULL;
7646 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007647 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007649#ifdef FEAT_FLOAT
7650 case VAR_FLOAT:
7651 *tofree = NULL;
7652 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7653 r = numbuf;
7654 break;
7655#endif
7656
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661
7662 --recurse;
7663 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664}
7665
7666/*
7667 * Return a string with the string representation of a variable.
7668 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7669 * "numbuf" is used for a number.
7670 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007671 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 */
7673 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 char_u **tofree;
7677 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007678 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679{
7680 switch (tv->v_type)
7681 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 case VAR_FUNC:
7683 *tofree = string_quote(tv->vval.v_string, TRUE);
7684 return *tofree;
7685 case VAR_STRING:
7686 *tofree = string_quote(tv->vval.v_string, FALSE);
7687 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007688#ifdef FEAT_FLOAT
7689 case VAR_FLOAT:
7690 *tofree = NULL;
7691 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7692 return numbuf;
7693#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007694 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007701 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007702}
7703
7704/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 * Return string "str" in ' quotes, doubling ' characters.
7706 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708 */
7709 static char_u *
7710string_quote(str, function)
7711 char_u *str;
7712 int function;
7713{
Bram Moolenaar33570922005-01-25 22:26:29 +00007714 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 char_u *p, *r, *s;
7716
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 len = (function ? 13 : 3);
7718 if (str != NULL)
7719 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007720 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 for (p = str; *p != NUL; mb_ptr_adv(p))
7722 if (*p == '\'')
7723 ++len;
7724 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007725 s = r = alloc(len);
7726 if (r != NULL)
7727 {
7728 if (function)
7729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 r += 10;
7732 }
7733 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007735 if (str != NULL)
7736 for (p = str; *p != NUL; )
7737 {
7738 if (*p == '\'')
7739 *r++ = '\'';
7740 MB_COPY_CHAR(p, r);
7741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 if (function)
7744 *r++ = ')';
7745 *r++ = NUL;
7746 }
7747 return s;
7748}
7749
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007750#ifdef FEAT_FLOAT
7751/*
7752 * Convert the string "text" to a floating point number.
7753 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7754 * this always uses a decimal point.
7755 * Returns the length of the text that was consumed.
7756 */
7757 static int
7758string2float(text, value)
7759 char_u *text;
7760 float_T *value; /* result stored here */
7761{
7762 char *s = (char *)text;
7763 float_T f;
7764
7765 f = strtod(s, &s);
7766 *value = f;
7767 return (int)((char_u *)s - text);
7768}
7769#endif
7770
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 * Get the value of an environment variable.
7773 * "arg" is pointing to the '$'. It is advanced to after the name.
7774 * If the environment variable was not set, silently assume it is empty.
7775 * Always return OK.
7776 */
7777 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007778get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 int evaluate;
7782{
7783 char_u *string = NULL;
7784 int len;
7785 int cc;
7786 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007787 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788
7789 ++*arg;
7790 name = *arg;
7791 len = get_env_len(arg);
7792 if (evaluate)
7793 {
7794 if (len != 0)
7795 {
7796 cc = name[len];
7797 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007798 /* first try vim_getenv(), fast for normal environment vars */
7799 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007801 {
7802 if (!mustfree)
7803 string = vim_strsave(string);
7804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 else
7806 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007807 if (mustfree)
7808 vim_free(string);
7809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 /* next try expanding things like $VIM and ${HOME} */
7811 string = expand_env_save(name - 1);
7812 if (string != NULL && *string == '$')
7813 {
7814 vim_free(string);
7815 string = NULL;
7816 }
7817 }
7818 name[len] = cc;
7819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->v_type = VAR_STRING;
7821 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 }
7823
7824 return OK;
7825}
7826
7827/*
7828 * Array with names and number of arguments of all internal functions
7829 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7830 */
7831static struct fst
7832{
7833 char *f_name; /* function name */
7834 char f_min_argc; /* minimal number of arguments */
7835 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007837 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838} functions[] =
7839{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840#ifdef FEAT_FLOAT
7841 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007842 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007843#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007844 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007845 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"append", 2, 2, f_append},
7847 {"argc", 0, 0, f_argc},
7848 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007849 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007853 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007856 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"bufexists", 1, 1, f_bufexists},
7858 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7859 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7860 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7861 {"buflisted", 1, 1, f_buflisted},
7862 {"bufloaded", 1, 1, f_bufloaded},
7863 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007864 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {"bufwinnr", 1, 1, f_bufwinnr},
7866 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007867 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007868 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 {"ceil", 1, 1, f_ceil},
7872#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007873 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007874 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007876 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007878#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007879 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007880 {"complete_add", 1, 1, f_complete_add},
7881 {"complete_check", 0, 0, f_complete_check},
7882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#ifdef FEAT_FLOAT
7886 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007887 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007889 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007891 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007892 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"delete", 1, 1, f_delete},
7894 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007895 {"diff_filler", 1, 1, f_diff_filler},
7896 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007897 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"eventhandler", 0, 0, f_eventhandler},
7901 {"executable", 1, 1, f_executable},
7902 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903#ifdef FEAT_FLOAT
7904 {"exp", 1, 1, f_exp},
7905#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007906 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007907 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007908 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7910 {"filereadable", 1, 1, f_filereadable},
7911 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007913 {"finddir", 1, 3, f_finddir},
7914 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007915#ifdef FEAT_FLOAT
7916 {"float2nr", 1, 1, f_float2nr},
7917 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007918 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007920 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"fnamemodify", 2, 2, f_fnamemodify},
7922 {"foldclosed", 1, 1, f_foldclosed},
7923 {"foldclosedend", 1, 1, f_foldclosedend},
7924 {"foldlevel", 1, 1, f_foldlevel},
7925 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007926 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007929 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007930 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007931 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007932 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"getchar", 0, 1, f_getchar},
7934 {"getcharmod", 0, 0, f_getcharmod},
7935 {"getcmdline", 0, 0, f_getcmdline},
7936 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007937 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007939 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007940 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"getfsize", 1, 1, f_getfsize},
7942 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007943 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007944 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007945 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007946 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007947 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007948 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007949 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007950 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007952 {"gettabvar", 2, 3, f_gettabvar},
7953 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getwinposx", 0, 0, f_getwinposx},
7955 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007956 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007957 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007958 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007961 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007962 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7964 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7965 {"histadd", 2, 2, f_histadd},
7966 {"histdel", 1, 2, f_histdel},
7967 {"histget", 1, 2, f_histget},
7968 {"histnr", 1, 1, f_histnr},
7969 {"hlID", 1, 1, f_hlID},
7970 {"hlexists", 1, 1, f_hlexists},
7971 {"hostname", 0, 0, f_hostname},
7972 {"iconv", 3, 3, f_iconv},
7973 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007975 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007977 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"inputrestore", 0, 0, f_inputrestore},
7979 {"inputsave", 0, 0, f_inputsave},
7980 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007981 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007982 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007984 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007985 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007987 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007989 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {"libcall", 3, 3, f_libcall},
7991 {"libcallnr", 3, 3, f_libcallnr},
7992 {"line", 1, 1, f_line},
7993 {"line2byte", 1, 1, f_line2byte},
7994 {"lispindent", 1, 1, f_lispindent},
7995 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007996#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007997 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998 {"log10", 1, 1, f_log10},
7999#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008000#ifdef FEAT_LUA
8001 {"luaeval", 1, 2, f_luaeval},
8002#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008004 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008005 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008006 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008007 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008009 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008010 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008011 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008012 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008013 {"max", 1, 1, f_max},
8014 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008015#ifdef vim_mkdir
8016 {"mkdir", 1, 3, f_mkdir},
8017#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008019#ifdef FEAT_MZSCHEME
8020 {"mzeval", 1, 1, f_mzeval},
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008023 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008024 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008025 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
8027 {"pow", 2, 2, f_pow},
8028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008030 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008031 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008032#ifdef FEAT_PYTHON3
8033 {"py3eval", 1, 1, f_py3eval},
8034#endif
8035#ifdef FEAT_PYTHON
8036 {"pyeval", 1, 1, f_pyeval},
8037#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008039 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008040 {"reltime", 0, 2, f_reltime},
8041 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"remote_expr", 2, 3, f_remote_expr},
8043 {"remote_foreground", 1, 1, f_remote_foreground},
8044 {"remote_peek", 1, 2, f_remote_peek},
8045 {"remote_read", 1, 1, f_remote_read},
8046 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008047 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008049 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008051 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008052#ifdef FEAT_FLOAT
8053 {"round", 1, 1, f_round},
8054#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008055 {"screenattr", 2, 2, f_screenattr},
8056 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008057 {"screencol", 0, 0, f_screencol},
8058 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008059 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008060 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008061 {"searchpair", 3, 7, f_searchpair},
8062 {"searchpairpos", 3, 7, f_searchpairpos},
8063 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"server2client", 2, 2, f_server2client},
8065 {"serverlist", 0, 0, f_serverlist},
8066 {"setbufvar", 3, 3, f_setbufvar},
8067 {"setcmdpos", 1, 1, f_setcmdpos},
8068 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008069 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008070 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008071 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008072 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008074 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008075 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008077#ifdef FEAT_CRYPT
8078 {"sha256", 1, 1, f_sha256},
8079#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008080 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008081 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083#ifdef FEAT_FLOAT
8084 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008085 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008087 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008088 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008089 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008090 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008091 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092#ifdef FEAT_FLOAT
8093 {"sqrt", 1, 1, f_sqrt},
8094 {"str2float", 1, 1, f_str2float},
8095#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008096 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008097 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008098 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099#ifdef HAVE_STRFTIME
8100 {"strftime", 1, 2, f_strftime},
8101#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008102 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"strlen", 1, 1, f_strlen},
8105 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008106 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008108 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"submatch", 1, 1, f_submatch},
8110 {"substitute", 4, 4, f_substitute},
8111 {"synID", 3, 3, f_synID},
8112 {"synIDattr", 2, 3, f_synIDattr},
8113 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008114 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008115 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008116 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008117 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008118 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008119 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008120 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008121 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008122#ifdef FEAT_FLOAT
8123 {"tan", 1, 1, f_tan},
8124 {"tanh", 1, 1, f_tanh},
8125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008127 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"tolower", 1, 1, f_tolower},
8129 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008130 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008131#ifdef FEAT_FLOAT
8132 {"trunc", 1, 1, f_trunc},
8133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008135 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008136 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008137 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"virtcol", 1, 1, f_virtcol},
8139 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008140 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winbufnr", 1, 1, f_winbufnr},
8142 {"wincol", 0, 0, f_wincol},
8143 {"winheight", 1, 1, f_winheight},
8144 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008145 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008147 {"winrestview", 1, 1, f_winrestview},
8148 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008150 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008151 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152};
8153
8154#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8155
8156/*
8157 * Function given to ExpandGeneric() to obtain the list of internal
8158 * or user defined function names.
8159 */
8160 char_u *
8161get_function_name(xp, idx)
8162 expand_T *xp;
8163 int idx;
8164{
8165 static int intidx = -1;
8166 char_u *name;
8167
8168 if (idx == 0)
8169 intidx = -1;
8170 if (intidx < 0)
8171 {
8172 name = get_user_func_name(xp, idx);
8173 if (name != NULL)
8174 return name;
8175 }
8176 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8177 {
8178 STRCPY(IObuff, functions[intidx].f_name);
8179 STRCAT(IObuff, "(");
8180 if (functions[intidx].f_max_argc == 0)
8181 STRCAT(IObuff, ")");
8182 return IObuff;
8183 }
8184
8185 return NULL;
8186}
8187
8188/*
8189 * Function given to ExpandGeneric() to obtain the list of internal or
8190 * user defined variable or function names.
8191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 char_u *
8193get_expr_name(xp, idx)
8194 expand_T *xp;
8195 int idx;
8196{
8197 static int intidx = -1;
8198 char_u *name;
8199
8200 if (idx == 0)
8201 intidx = -1;
8202 if (intidx < 0)
8203 {
8204 name = get_function_name(xp, idx);
8205 if (name != NULL)
8206 return name;
8207 }
8208 return get_user_var_name(xp, ++intidx);
8209}
8210
8211#endif /* FEAT_CMDL_COMPL */
8212
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008213#if defined(EBCDIC) || defined(PROTO)
8214/*
8215 * Compare struct fst by function name.
8216 */
8217 static int
8218compare_func_name(s1, s2)
8219 const void *s1;
8220 const void *s2;
8221{
8222 struct fst *p1 = (struct fst *)s1;
8223 struct fst *p2 = (struct fst *)s2;
8224
8225 return STRCMP(p1->f_name, p2->f_name);
8226}
8227
8228/*
8229 * Sort the function table by function name.
8230 * The sorting of the table above is ASCII dependant.
8231 * On machines using EBCDIC we have to sort it.
8232 */
8233 static void
8234sortFunctions()
8235{
8236 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8237
8238 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8239}
8240#endif
8241
8242
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243/*
8244 * Find internal function in table above.
8245 * Return index, or -1 if not found
8246 */
8247 static int
8248find_internal_func(name)
8249 char_u *name; /* name of the function */
8250{
8251 int first = 0;
8252 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8253 int cmp;
8254 int x;
8255
8256 /*
8257 * Find the function name in the table. Binary search.
8258 */
8259 while (first <= last)
8260 {
8261 x = first + ((unsigned)(last - first) >> 1);
8262 cmp = STRCMP(name, functions[x].f_name);
8263 if (cmp < 0)
8264 last = x - 1;
8265 else if (cmp > 0)
8266 first = x + 1;
8267 else
8268 return x;
8269 }
8270 return -1;
8271}
8272
8273/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008274 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8275 * name it contains, otherwise return "name".
8276 */
8277 static char_u *
8278deref_func_name(name, lenp)
8279 char_u *name;
8280 int *lenp;
8281{
Bram Moolenaar33570922005-01-25 22:26:29 +00008282 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 int cc;
8284
8285 cc = name[*lenp];
8286 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008287 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 {
8293 *lenp = 0;
8294 return (char_u *)""; /* just in case */
8295 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008296 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008297 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298 }
8299
8300 return name;
8301}
8302
8303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 * Allocate a variable for the result of a function.
8305 * Return OK or FAIL.
8306 */
8307 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8309 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 char_u *name; /* name of the function */
8311 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 char_u **arg; /* argument, pointing to the '(' */
8314 linenr_T firstline; /* first line of range */
8315 linenr_T lastline; /* last line of range */
8316 int *doesrange; /* return: function handled range */
8317 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
8320 char_u *argp;
8321 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008322 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 int argcount = 0; /* number of arguments found */
8324
8325 /*
8326 * Get the arguments.
8327 */
8328 argp = *arg;
8329 while (argcount < MAX_FUNC_ARGS)
8330 {
8331 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8332 if (*argp == ')' || *argp == ',' || *argp == NUL)
8333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8335 {
8336 ret = FAIL;
8337 break;
8338 }
8339 ++argcount;
8340 if (*argp != ',')
8341 break;
8342 }
8343 if (*argp == ')')
8344 ++argp;
8345 else
8346 ret = FAIL;
8347
8348 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 {
8353 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008354 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008356 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358
8359 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008360 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
8362 *arg = skipwhite(argp);
8363 return ret;
8364}
8365
8366
8367/*
8368 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008369 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008370 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 */
8372 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008373call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008374 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008375 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008379 typval_T *argvars; /* vars for arguments, must have "argcount"
8380 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 linenr_T firstline; /* first line of range */
8382 linenr_T lastline; /* last line of range */
8383 int *doesrange; /* return: function handled range */
8384 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
8387 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#define ERROR_UNKNOWN 0
8389#define ERROR_TOOMANY 1
8390#define ERROR_TOOFEW 2
8391#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008392#define ERROR_DICT 4
8393#define ERROR_NONE 5
8394#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 int error = ERROR_NONE;
8396 int i;
8397 int llen;
8398 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399#define FLEN_FIXED 40
8400 char_u fname_buf[FLEN_FIXED + 1];
8401 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008402 char_u *name;
8403
8404 /* Make a copy of the name, if it comes from a funcref variable it could
8405 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008406 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008407 if (name == NULL)
8408 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409
8410 /*
8411 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8412 * Change <SNR>123_name() to K_SNR 123_name().
8413 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 llen = eval_fname_script(name);
8416 if (llen > 0)
8417 {
8418 fname_buf[0] = K_SPECIAL;
8419 fname_buf[1] = KS_EXTRA;
8420 fname_buf[2] = (int)KE_SNR;
8421 i = 3;
8422 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8423 {
8424 if (current_SID <= 0)
8425 error = ERROR_SCRIPT;
8426 else
8427 {
8428 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8429 i = (int)STRLEN(fname_buf);
8430 }
8431 }
8432 if (i + STRLEN(name + llen) < FLEN_FIXED)
8433 {
8434 STRCPY(fname_buf + i, name + llen);
8435 fname = fname_buf;
8436 }
8437 else
8438 {
8439 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8440 if (fname == NULL)
8441 error = ERROR_OTHER;
8442 else
8443 {
8444 mch_memmove(fname, fname_buf, (size_t)i);
8445 STRCPY(fname + i, name + llen);
8446 }
8447 }
8448 }
8449 else
8450 fname = name;
8451
8452 *doesrange = FALSE;
8453
8454
8455 /* execute the function if no errors detected and executing */
8456 if (evaluate && error == ERROR_NONE)
8457 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008458 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8459 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 error = ERROR_UNKNOWN;
8461
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
8464 /*
8465 * User defined function.
8466 */
8467 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470 /* Trigger FuncUndefined event, may load the function. */
8471 if (fp == NULL
8472 && apply_autocmds(EVENT_FUNCUNDEFINED,
8473 fname, fname, TRUE, NULL)
8474 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008476 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 fp = find_func(fname);
8478 }
8479#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008480 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008481 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008482 {
8483 /* loaded a package, search for the function again */
8484 fp = find_func(fname);
8485 }
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 if (fp != NULL)
8488 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008491 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008495 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008496 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 else
8498 {
8499 /*
8500 * Call the user function.
8501 * Save and restore search patterns, script variables and
8502 * redo buffer.
8503 */
8504 save_search_patterns();
8505 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008506 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008507 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008508 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008509 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8510 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8511 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008512 /* Function was unreferenced while being used, free it
8513 * now. */
8514 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 restoreRedobuff();
8516 restore_search_patterns();
8517 error = ERROR_NONE;
8518 }
8519 }
8520 }
8521 else
8522 {
8523 /*
8524 * Find the function name in the table, call its implementation.
8525 */
8526 i = find_internal_func(fname);
8527 if (i >= 0)
8528 {
8529 if (argcount < functions[i].f_min_argc)
8530 error = ERROR_TOOFEW;
8531 else if (argcount > functions[i].f_max_argc)
8532 error = ERROR_TOOMANY;
8533 else
8534 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 error = ERROR_NONE;
8538 }
8539 }
8540 }
8541 /*
8542 * The function call (or "FuncUndefined" autocommand sequence) might
8543 * have been aborted by an error, an interrupt, or an explicitly thrown
8544 * exception that has not been caught so far. This situation can be
8545 * tested for by calling aborting(). For an error in an internal
8546 * function or for the "E132" error in call_user_func(), however, the
8547 * throw point at which the "force_abort" flag (temporarily reset by
8548 * emsg()) is normally updated has not been reached yet. We need to
8549 * update that flag first to make aborting() reliable.
8550 */
8551 update_force_abort();
8552 }
8553 if (error == ERROR_NONE)
8554 ret = OK;
8555
8556 /*
8557 * Report an error unless the argument evaluation or function call has been
8558 * cancelled due to an aborting error, an interrupt, or an exception.
8559 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 if (!aborting())
8561 {
8562 switch (error)
8563 {
8564 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008565 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 break;
8567 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008568 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008569 break;
8570 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008571 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008572 name);
8573 break;
8574 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008575 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 name);
8577 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008579 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008580 name);
8581 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008582 }
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (fname != name && fname != fname_buf)
8586 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008587 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
8589 return ret;
8590}
8591
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008592/*
8593 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008594 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008595 */
8596 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008597emsg_funcname(ermsg, name)
8598 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008599 char_u *name;
8600{
8601 char_u *p;
8602
8603 if (*name == K_SPECIAL)
8604 p = concat_str((char_u *)"<SNR>", name + 3);
8605 else
8606 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008607 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008608 if (p != name)
8609 vim_free(p);
8610}
8611
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008612/*
8613 * Return TRUE for a non-zero Number and a non-empty String.
8614 */
8615 static int
8616non_zero_arg(argvars)
8617 typval_T *argvars;
8618{
8619 return ((argvars[0].v_type == VAR_NUMBER
8620 && argvars[0].vval.v_number != 0)
8621 || (argvars[0].v_type == VAR_STRING
8622 && argvars[0].vval.v_string != NULL
8623 && *argvars[0].vval.v_string != NUL));
8624}
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626/*********************************************
8627 * Implementation of the built-in functions
8628 */
8629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008630#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008631static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8632
8633/*
8634 * Get the float value of "argvars[0]" into "f".
8635 * Returns FAIL when the argument is not a Number or Float.
8636 */
8637 static int
8638get_float_arg(argvars, f)
8639 typval_T *argvars;
8640 float_T *f;
8641{
8642 if (argvars[0].v_type == VAR_FLOAT)
8643 {
8644 *f = argvars[0].vval.v_float;
8645 return OK;
8646 }
8647 if (argvars[0].v_type == VAR_NUMBER)
8648 {
8649 *f = (float_T)argvars[0].vval.v_number;
8650 return OK;
8651 }
8652 EMSG(_("E808: Number or Float required"));
8653 return FAIL;
8654}
8655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008656/*
8657 * "abs(expr)" function
8658 */
8659 static void
8660f_abs(argvars, rettv)
8661 typval_T *argvars;
8662 typval_T *rettv;
8663{
8664 if (argvars[0].v_type == VAR_FLOAT)
8665 {
8666 rettv->v_type = VAR_FLOAT;
8667 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8668 }
8669 else
8670 {
8671 varnumber_T n;
8672 int error = FALSE;
8673
8674 n = get_tv_number_chk(&argvars[0], &error);
8675 if (error)
8676 rettv->vval.v_number = -1;
8677 else if (n > 0)
8678 rettv->vval.v_number = n;
8679 else
8680 rettv->vval.v_number = -n;
8681 }
8682}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008683
8684/*
8685 * "acos()" function
8686 */
8687 static void
8688f_acos(argvars, rettv)
8689 typval_T *argvars;
8690 typval_T *rettv;
8691{
8692 float_T f;
8693
8694 rettv->v_type = VAR_FLOAT;
8695 if (get_float_arg(argvars, &f) == OK)
8696 rettv->vval.v_float = acos(f);
8697 else
8698 rettv->vval.v_float = 0.0;
8699}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008700#endif
8701
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008703 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 */
8705 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008706f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 typval_T *argvars;
8708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
Bram Moolenaar33570922005-01-25 22:26:29 +00008710 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008715 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008716 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008717 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008719 }
8720 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008721 EMSG(_(e_listreq));
8722}
8723
8724/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008725 * "and(expr, expr)" function
8726 */
8727 static void
8728f_and(argvars, rettv)
8729 typval_T *argvars;
8730 typval_T *rettv;
8731{
8732 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8733 & get_tv_number_chk(&argvars[1], NULL);
8734}
8735
8736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737 * "append(lnum, string/list)" function
8738 */
8739 static void
8740f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 typval_T *argvars;
8742 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743{
8744 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 list_T *l = NULL;
8747 listitem_T *li = NULL;
8748 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008749 long added = 0;
8750
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008751 /* When coming here from Insert mode, sync undo, so that this can be
8752 * undone separately from what was previously inserted. */
8753 if (u_sync_once == 2)
8754 {
8755 u_sync_once = 1; /* notify that u_sync() was called */
8756 u_sync(TRUE);
8757 }
8758
Bram Moolenaar0d660222005-01-07 21:51:51 +00008759 lnum = get_tv_lnum(argvars);
8760 if (lnum >= 0
8761 && lnum <= curbuf->b_ml.ml_line_count
8762 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008763 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008764 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008765 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 l = argvars[1].vval.v_list;
8767 if (l == NULL)
8768 return;
8769 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008770 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008771 for (;;)
8772 {
8773 if (l == NULL)
8774 tv = &argvars[1]; /* append a string */
8775 else if (li == NULL)
8776 break; /* end of list */
8777 else
8778 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008779 line = get_tv_string_chk(tv);
8780 if (line == NULL) /* type error */
8781 {
8782 rettv->vval.v_number = 1; /* Failed */
8783 break;
8784 }
8785 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008786 ++added;
8787 if (l == NULL)
8788 break;
8789 li = li->li_next;
8790 }
8791
8792 appended_lines_mark(lnum, added);
8793 if (curwin->w_cursor.lnum > lnum)
8794 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 else
8797 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798}
8799
8800/*
8801 * "argc()" function
8802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008804f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809}
8810
8811/*
8812 * "argidx()" function
8813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008819 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
8822/*
8823 * "argv(nr)" function
8824 */
8825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008827 typval_T *argvars;
8828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 int idx;
8831
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008832 if (argvars[0].v_type != VAR_UNKNOWN)
8833 {
8834 idx = get_tv_number_chk(&argvars[0], NULL);
8835 if (idx >= 0 && idx < ARGCOUNT)
8836 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8837 else
8838 rettv->vval.v_string = NULL;
8839 rettv->v_type = VAR_STRING;
8840 }
8841 else if (rettv_list_alloc(rettv) == OK)
8842 for (idx = 0; idx < ARGCOUNT; ++idx)
8843 list_append_string(rettv->vval.v_list,
8844 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
8846
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008847#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008848/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008849 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008851 static void
8852f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008853 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008854 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008855{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008856 float_T f;
8857
8858 rettv->v_type = VAR_FLOAT;
8859 if (get_float_arg(argvars, &f) == OK)
8860 rettv->vval.v_float = asin(f);
8861 else
8862 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008863}
8864
8865/*
8866 * "atan()" function
8867 */
8868 static void
8869f_atan(argvars, rettv)
8870 typval_T *argvars;
8871 typval_T *rettv;
8872{
8873 float_T f;
8874
8875 rettv->v_type = VAR_FLOAT;
8876 if (get_float_arg(argvars, &f) == OK)
8877 rettv->vval.v_float = atan(f);
8878 else
8879 rettv->vval.v_float = 0.0;
8880}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881
8882/*
8883 * "atan2()" function
8884 */
8885 static void
8886f_atan2(argvars, rettv)
8887 typval_T *argvars;
8888 typval_T *rettv;
8889{
8890 float_T fx, fy;
8891
8892 rettv->v_type = VAR_FLOAT;
8893 if (get_float_arg(argvars, &fx) == OK
8894 && get_float_arg(&argvars[1], &fy) == OK)
8895 rettv->vval.v_float = atan2(fx, fy);
8896 else
8897 rettv->vval.v_float = 0.0;
8898}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008899#endif
8900
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901/*
8902 * "browse(save, title, initdir, default)" function
8903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909#ifdef FEAT_BROWSE
8910 int save;
8911 char_u *title;
8912 char_u *initdir;
8913 char_u *defname;
8914 char_u buf[NUMBUFLEN];
8915 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 save = get_tv_number_chk(&argvars[0], &error);
8919 title = get_tv_string_chk(&argvars[1]);
8920 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8921 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 if (error || title == NULL || initdir == NULL || defname == NULL)
8924 rettv->vval.v_string = NULL;
8925 else
8926 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008927 do_browse(save ? BROWSE_SAVE : 0,
8928 title, defname, NULL, initdir, NULL, curbuf);
8929#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008933}
8934
8935/*
8936 * "browsedir(title, initdir)" function
8937 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008939f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008940 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008942{
8943#ifdef FEAT_BROWSE
8944 char_u *title;
8945 char_u *initdir;
8946 char_u buf[NUMBUFLEN];
8947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 title = get_tv_string_chk(&argvars[0]);
8949 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008951 if (title == NULL || initdir == NULL)
8952 rettv->vval.v_string = NULL;
8953 else
8954 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008955 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960}
8961
Bram Moolenaar33570922005-01-25 22:26:29 +00008962static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008963
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964/*
8965 * Find a buffer by number or exact name.
8966 */
8967 static buf_T *
8968find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008969 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970{
8971 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008973 if (avar->v_type == VAR_NUMBER)
8974 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008975 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008977 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008978 if (buf == NULL)
8979 {
8980 /* No full path name match, try a match with a URL or a "nofile"
8981 * buffer, these don't use the full path. */
8982 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8983 if (buf->b_fname != NULL
8984 && (path_with_url(buf->b_fname)
8985#ifdef FEAT_QUICKFIX
8986 || bt_nofile(buf)
8987#endif
8988 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008990 break;
8991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993 return buf;
8994}
8995
8996/*
8997 * "bufexists(expr)" function
8998 */
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005}
9006
9007/*
9008 * "buflisted(expr)" function
9009 */
9010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 buf_T *buf;
9016
9017 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019}
9020
9021/*
9022 * "bufloaded(expr)" function
9023 */
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 buf_T *buf;
9030
9031 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009032 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033}
9034
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009035static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009036
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037/*
9038 * Get buffer by number or pattern.
9039 */
9040 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009041get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009043 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 int save_magic;
9047 char_u *save_cpo;
9048 buf_T *buf;
9049
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050 if (tv->v_type == VAR_NUMBER)
9051 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009052 if (tv->v_type != VAR_STRING)
9053 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (name == NULL || *name == NUL)
9055 return curbuf;
9056 if (name[0] == '$' && name[1] == NUL)
9057 return lastbuf;
9058
9059 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9060 save_magic = p_magic;
9061 p_magic = TRUE;
9062 save_cpo = p_cpo;
9063 p_cpo = (char_u *)"";
9064
9065 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009066 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 p_magic = save_magic;
9069 p_cpo = save_cpo;
9070
9071 /* If not found, try expanding the name, like done for bufexists(). */
9072 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 return buf;
9076}
9077
9078/*
9079 * "bufname(expr)" function
9080 */
9081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
9086 buf_T *buf;
9087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009090 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 --emsg_off;
9097}
9098
9099/*
9100 * "bufnr(expr)" function
9101 */
9102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009103f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009104 typval_T *argvars;
9105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009108 int error = FALSE;
9109 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009111 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009113 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009114 --emsg_off;
9115
9116 /* If the buffer isn't found and the second argument is not zero create a
9117 * new buffer. */
9118 if (buf == NULL
9119 && argvars[1].v_type != VAR_UNKNOWN
9120 && get_tv_number_chk(&argvars[1], &error) != 0
9121 && !error
9122 && (name = get_tv_string_chk(&argvars[0])) != NULL
9123 && !error)
9124 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
9132/*
9133 * "bufwinnr(nr)" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140#ifdef FEAT_WINDOWS
9141 win_T *wp;
9142 int winnr = 0;
9143#endif
9144 buf_T *buf;
9145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009148 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#ifdef FEAT_WINDOWS
9150 for (wp = firstwin; wp; wp = wp->w_next)
9151 {
9152 ++winnr;
9153 if (wp->w_buffer == buf)
9154 break;
9155 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#endif
9160 --emsg_off;
9161}
9162
9163/*
9164 * "byte2line(byte)" function
9165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009168 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#else
9174 long boff = 0;
9175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 (linenr_T)0, &boff);
9182#endif
9183}
9184
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009185 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009186byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009187 typval_T *argvars;
9188 typval_T *rettv;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009189 int comp;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009190{
9191#ifdef FEAT_MBYTE
9192 char_u *t;
9193#endif
9194 char_u *str;
9195 long idx;
9196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 str = get_tv_string_chk(&argvars[0]);
9198 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009200 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009201 return;
9202
9203#ifdef FEAT_MBYTE
9204 t = str;
9205 for ( ; idx > 0; idx--)
9206 {
9207 if (*t == NUL) /* EOL reached */
9208 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009209 if (enc_utf8 && comp)
9210 t += utf_ptr2len(t);
9211 else
9212 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009213 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009214 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009215#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009216 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009218#endif
9219}
9220
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009221/*
9222 * "byteidx()" function
9223 */
9224 static void
9225f_byteidx(argvars, rettv)
9226 typval_T *argvars;
9227 typval_T *rettv;
9228{
9229 byteidx(argvars, rettv, FALSE);
9230}
9231
9232/*
9233 * "byteidxcomp()" function
9234 */
9235 static void
9236f_byteidxcomp(argvars, rettv)
9237 typval_T *argvars;
9238 typval_T *rettv;
9239{
9240 byteidx(argvars, rettv, TRUE);
9241}
9242
Bram Moolenaardb913952012-06-29 12:54:53 +02009243 int
9244func_call(name, args, selfdict, rettv)
9245 char_u *name;
9246 typval_T *args;
9247 dict_T *selfdict;
9248 typval_T *rettv;
9249{
9250 listitem_T *item;
9251 typval_T argv[MAX_FUNC_ARGS + 1];
9252 int argc = 0;
9253 int dummy;
9254 int r = 0;
9255
9256 for (item = args->vval.v_list->lv_first; item != NULL;
9257 item = item->li_next)
9258 {
9259 if (argc == MAX_FUNC_ARGS)
9260 {
9261 EMSG(_("E699: Too many arguments"));
9262 break;
9263 }
9264 /* Make a copy of each argument. This is needed to be able to set
9265 * v_lock to VAR_FIXED in the copy without changing the original list.
9266 */
9267 copy_tv(&item->li_tv, &argv[argc++]);
9268 }
9269
9270 if (item == NULL)
9271 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9272 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9273 &dummy, TRUE, selfdict);
9274
9275 /* Free the arguments. */
9276 while (argc > 0)
9277 clear_tv(&argv[--argc]);
9278
9279 return r;
9280}
9281
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009282/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009283 * "call(func, arglist)" function
9284 */
9285 static void
9286f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009287 typval_T *argvars;
9288 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009289{
9290 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009291 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009292
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009293 if (argvars[1].v_type != VAR_LIST)
9294 {
9295 EMSG(_(e_listreq));
9296 return;
9297 }
9298 if (argvars[1].vval.v_list == NULL)
9299 return;
9300
9301 if (argvars[0].v_type == VAR_FUNC)
9302 func = argvars[0].vval.v_string;
9303 else
9304 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 if (*func == NUL)
9306 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009307
Bram Moolenaare9a41262005-01-15 22:18:47 +00009308 if (argvars[2].v_type != VAR_UNKNOWN)
9309 {
9310 if (argvars[2].v_type != VAR_DICT)
9311 {
9312 EMSG(_(e_dictreq));
9313 return;
9314 }
9315 selfdict = argvars[2].vval.v_dict;
9316 }
9317
Bram Moolenaardb913952012-06-29 12:54:53 +02009318 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009319}
9320
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009321#ifdef FEAT_FLOAT
9322/*
9323 * "ceil({float})" function
9324 */
9325 static void
9326f_ceil(argvars, rettv)
9327 typval_T *argvars;
9328 typval_T *rettv;
9329{
9330 float_T f;
9331
9332 rettv->v_type = VAR_FLOAT;
9333 if (get_float_arg(argvars, &f) == OK)
9334 rettv->vval.v_float = ceil(f);
9335 else
9336 rettv->vval.v_float = 0.0;
9337}
9338#endif
9339
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009340/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009341 * "changenr()" function
9342 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009343 static void
9344f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009345 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009346 typval_T *rettv;
9347{
9348 rettv->vval.v_number = curbuf->b_u_seq_cur;
9349}
9350
9351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 * "char2nr(string)" function
9353 */
9354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009356 typval_T *argvars;
9357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
9359#ifdef FEAT_MBYTE
9360 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009361 {
9362 int utf8 = 0;
9363
9364 if (argvars[1].v_type != VAR_UNKNOWN)
9365 utf8 = get_tv_number_chk(&argvars[1], NULL);
9366
9367 if (utf8)
9368 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9369 else
9370 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 else
9373#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375}
9376
9377/*
9378 * "cindent(lnum)" function
9379 */
9380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009382 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385#ifdef FEAT_CINDENT
9386 pos_T pos;
9387 linenr_T lnum;
9388
9389 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9392 {
9393 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 curwin->w_cursor = pos;
9396 }
9397 else
9398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009403 * "clearmatches()" function
9404 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009405 static void
9406f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009407 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009408 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009409{
9410#ifdef FEAT_SEARCH_EXTRA
9411 clear_matches(curwin);
9412#endif
9413}
9414
9415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 * "col(string)" function
9417 */
9418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009420 typval_T *argvars;
9421 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 colnr_T col = 0;
9424 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009425 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009427 fp = var2fpos(&argvars[0], FALSE, &fnum);
9428 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 {
9430 if (fp->col == MAXCOL)
9431 {
9432 /* '> can be MAXCOL, get the length of the line then */
9433 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009434 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 else
9436 col = MAXCOL;
9437 }
9438 else
9439 {
9440 col = fp->col + 1;
9441#ifdef FEAT_VIRTUALEDIT
9442 /* col(".") when the cursor is on the NUL at the end of the line
9443 * because of "coladd" can be seen as an extra column. */
9444 if (virtual_active() && fp == &curwin->w_cursor)
9445 {
9446 char_u *p = ml_get_cursor();
9447
9448 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9449 curwin->w_virtcol - curwin->w_cursor.coladd))
9450 {
9451# ifdef FEAT_MBYTE
9452 int l;
9453
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009454 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 col += l;
9456# else
9457 if (*p != NUL && p[1] == NUL)
9458 ++col;
9459# endif
9460 }
9461 }
9462#endif
9463 }
9464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
Bram Moolenaar572cb562005-08-05 21:35:02 +00009468#if defined(FEAT_INS_EXPAND)
9469/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009470 * "complete()" function
9471 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009472 static void
9473f_complete(argvars, rettv)
9474 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009475 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009476{
9477 int startcol;
9478
9479 if ((State & INSERT) == 0)
9480 {
9481 EMSG(_("E785: complete() can only be used in Insert mode"));
9482 return;
9483 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009484
9485 /* Check for undo allowed here, because if something was already inserted
9486 * the line was already saved for undo and this check isn't done. */
9487 if (!undo_allowed())
9488 return;
9489
Bram Moolenaarade00832006-03-10 21:46:58 +00009490 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9491 {
9492 EMSG(_(e_invarg));
9493 return;
9494 }
9495
9496 startcol = get_tv_number_chk(&argvars[0], NULL);
9497 if (startcol <= 0)
9498 return;
9499
9500 set_completion(startcol - 1, argvars[1].vval.v_list);
9501}
9502
9503/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009504 * "complete_add()" function
9505 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009506 static void
9507f_complete_add(argvars, rettv)
9508 typval_T *argvars;
9509 typval_T *rettv;
9510{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009511 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009512}
9513
9514/*
9515 * "complete_check()" function
9516 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009517 static void
9518f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009519 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009520 typval_T *rettv;
9521{
9522 int saved = RedrawingDisabled;
9523
9524 RedrawingDisabled = 0;
9525 ins_compl_check_keys(0);
9526 rettv->vval.v_number = compl_interrupted;
9527 RedrawingDisabled = saved;
9528}
9529#endif
9530
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531/*
9532 * "confirm(message, buttons[, default [, type]])" function
9533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009535f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009536 typval_T *argvars UNUSED;
9537 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9540 char_u *message;
9541 char_u *buttons = NULL;
9542 char_u buf[NUMBUFLEN];
9543 char_u buf2[NUMBUFLEN];
9544 int def = 1;
9545 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 char_u *typestr;
9547 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 message = get_tv_string_chk(&argvars[0]);
9550 if (message == NULL)
9551 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9555 if (buttons == NULL)
9556 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9563 if (typestr == NULL)
9564 error = TRUE;
9565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 switch (TOUPPER_ASC(*typestr))
9568 {
9569 case 'E': type = VIM_ERROR; break;
9570 case 'Q': type = VIM_QUESTION; break;
9571 case 'I': type = VIM_INFO; break;
9572 case 'W': type = VIM_WARNING; break;
9573 case 'G': type = VIM_GENERIC; break;
9574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 }
9576 }
9577 }
9578 }
9579
9580 if (buttons == NULL || *buttons == NUL)
9581 buttons = (char_u *)_("&Ok");
9582
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009583 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009585 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586#endif
9587}
9588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589/*
9590 * "copy()" function
9591 */
9592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009594 typval_T *argvars;
9595 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009597 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009598}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009600#ifdef FEAT_FLOAT
9601/*
9602 * "cos()" function
9603 */
9604 static void
9605f_cos(argvars, rettv)
9606 typval_T *argvars;
9607 typval_T *rettv;
9608{
9609 float_T f;
9610
9611 rettv->v_type = VAR_FLOAT;
9612 if (get_float_arg(argvars, &f) == OK)
9613 rettv->vval.v_float = cos(f);
9614 else
9615 rettv->vval.v_float = 0.0;
9616}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009617
9618/*
9619 * "cosh()" function
9620 */
9621 static void
9622f_cosh(argvars, rettv)
9623 typval_T *argvars;
9624 typval_T *rettv;
9625{
9626 float_T f;
9627
9628 rettv->v_type = VAR_FLOAT;
9629 if (get_float_arg(argvars, &f) == OK)
9630 rettv->vval.v_float = cosh(f);
9631 else
9632 rettv->vval.v_float = 0.0;
9633}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009634#endif
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009637 * "count()" function
9638 */
9639 static void
9640f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009641 typval_T *argvars;
9642 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009643{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009644 long n = 0;
9645 int ic = FALSE;
9646
Bram Moolenaare9a41262005-01-15 22:18:47 +00009647 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009648 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009649 listitem_T *li;
9650 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009652
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 if ((l = argvars[0].vval.v_list) != NULL)
9654 {
9655 li = l->lv_first;
9656 if (argvars[2].v_type != VAR_UNKNOWN)
9657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 int error = FALSE;
9659
9660 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 if (argvars[3].v_type != VAR_UNKNOWN)
9662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 idx = get_tv_number_chk(&argvars[3], &error);
9664 if (!error)
9665 {
9666 li = list_find(l, idx);
9667 if (li == NULL)
9668 EMSGN(_(e_listidx), idx);
9669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 if (error)
9672 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009673 }
9674
9675 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009676 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 ++n;
9678 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009679 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009680 else if (argvars[0].v_type == VAR_DICT)
9681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009682 int todo;
9683 dict_T *d;
9684 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009685
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009686 if ((d = argvars[0].vval.v_dict) != NULL)
9687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 int error = FALSE;
9689
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 if (argvars[2].v_type != VAR_UNKNOWN)
9691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009693 if (argvars[3].v_type != VAR_UNKNOWN)
9694 EMSG(_(e_invarg));
9695 }
9696
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009697 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009699 {
9700 if (!HASHITEM_EMPTY(hi))
9701 {
9702 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009703 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009704 ++n;
9705 }
9706 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 }
9708 }
9709 else
9710 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009711 rettv->vval.v_number = n;
9712}
9713
9714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9716 *
9717 * Checks the existence of a cscope connection.
9718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009720f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009721 typval_T *argvars UNUSED;
9722 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724#ifdef FEAT_CSCOPE
9725 int num = 0;
9726 char_u *dbpath = NULL;
9727 char_u *prepend = NULL;
9728 char_u buf[NUMBUFLEN];
9729
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730 if (argvars[0].v_type != VAR_UNKNOWN
9731 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 num = (int)get_tv_number(&argvars[0]);
9734 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009735 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 }
9738
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740#endif
9741}
9742
9743/*
9744 * "cursor(lnum, col)" function
9745 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009746 * Moves the cursor to the specified line and column.
9747 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009751 typval_T *argvars;
9752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009755#ifdef FEAT_VIRTUALEDIT
9756 long coladd = 0;
9757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009759 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009760 if (argvars[1].v_type == VAR_UNKNOWN)
9761 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009762 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009763
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009764 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009765 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009766 line = pos.lnum;
9767 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009768#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009769 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009770#endif
9771 }
9772 else
9773 {
9774 line = get_tv_lnum(argvars);
9775 col = get_tv_number_chk(&argvars[1], NULL);
9776#ifdef FEAT_VIRTUALEDIT
9777 if (argvars[2].v_type != VAR_UNKNOWN)
9778 coladd = get_tv_number_chk(&argvars[2], NULL);
9779#endif
9780 }
9781 if (line < 0 || col < 0
9782#ifdef FEAT_VIRTUALEDIT
9783 || coladd < 0
9784#endif
9785 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 if (line > 0)
9788 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 if (col > 0)
9790 curwin->w_cursor.col = col - 1;
9791#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009792 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793#endif
9794
9795 /* Make sure the cursor is in a valid position. */
9796 check_cursor();
9797#ifdef FEAT_MBYTE
9798 /* Correct cursor for multi-byte character. */
9799 if (has_mbyte)
9800 mb_adjust_cursor();
9801#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009802
9803 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009804 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009808 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 */
9810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009812 typval_T *argvars;
9813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009815 int noref = 0;
9816
9817 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009819 if (noref < 0 || noref > 1)
9820 EMSG(_(e_invarg));
9821 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009822 {
9823 current_copyID += COPYID_INC;
9824 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826}
9827
9828/*
9829 * "delete()" function
9830 */
9831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009833 typval_T *argvars;
9834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840}
9841
9842/*
9843 * "did_filetype()" function
9844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009847 typval_T *argvars UNUSED;
9848 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852#endif
9853}
9854
9855/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 * "diff_filler()" function
9857 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009860 typval_T *argvars UNUSED;
9861 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009862{
9863#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009865#endif
9866}
9867
9868/*
9869 * "diff_hlID()" function
9870 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009873 typval_T *argvars UNUSED;
9874 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875{
9876#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009878 static linenr_T prev_lnum = 0;
9879 static int changedtick = 0;
9880 static int fnum = 0;
9881 static int change_start = 0;
9882 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009883 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009884 int filler_lines;
9885 int col;
9886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 if (lnum < 0) /* ignore type error in {lnum} arg */
9888 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009889 if (lnum != prev_lnum
9890 || changedtick != curbuf->b_changedtick
9891 || fnum != curbuf->b_fnum)
9892 {
9893 /* New line, buffer, change: need to get the values. */
9894 filler_lines = diff_check(curwin, lnum);
9895 if (filler_lines < 0)
9896 {
9897 if (filler_lines == -1)
9898 {
9899 change_start = MAXCOL;
9900 change_end = -1;
9901 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9902 hlID = HLF_ADD; /* added line */
9903 else
9904 hlID = HLF_CHD; /* changed line */
9905 }
9906 else
9907 hlID = HLF_ADD; /* added line */
9908 }
9909 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009910 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009911 prev_lnum = lnum;
9912 changedtick = curbuf->b_changedtick;
9913 fnum = curbuf->b_fnum;
9914 }
9915
9916 if (hlID == HLF_CHD || hlID == HLF_TXD)
9917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009919 if (col >= change_start && col <= change_end)
9920 hlID = HLF_TXD; /* changed text */
9921 else
9922 hlID = HLF_CHD; /* changed line */
9923 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009924 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009925#endif
9926}
9927
9928/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009929 * "empty({expr})" function
9930 */
9931 static void
9932f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009933 typval_T *argvars;
9934 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009935{
9936 int n;
9937
9938 switch (argvars[0].v_type)
9939 {
9940 case VAR_STRING:
9941 case VAR_FUNC:
9942 n = argvars[0].vval.v_string == NULL
9943 || *argvars[0].vval.v_string == NUL;
9944 break;
9945 case VAR_NUMBER:
9946 n = argvars[0].vval.v_number == 0;
9947 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009948#ifdef FEAT_FLOAT
9949 case VAR_FLOAT:
9950 n = argvars[0].vval.v_float == 0.0;
9951 break;
9952#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009953 case VAR_LIST:
9954 n = argvars[0].vval.v_list == NULL
9955 || argvars[0].vval.v_list->lv_first == NULL;
9956 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009957 case VAR_DICT:
9958 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009959 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009961 default:
9962 EMSG2(_(e_intern2), "f_empty()");
9963 n = 0;
9964 }
9965
9966 rettv->vval.v_number = n;
9967}
9968
9969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 * "escape({string}, {chars})" function
9971 */
9972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 typval_T *argvars;
9975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976{
9977 char_u buf[NUMBUFLEN];
9978
Bram Moolenaar758711c2005-02-02 23:11:38 +00009979 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9980 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009981 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982}
9983
9984/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009985 * "eval()" function
9986 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009987 static void
9988f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009989 typval_T *argvars;
9990 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009991{
9992 char_u *s;
9993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 s = get_tv_string_chk(&argvars[0]);
9995 if (s != NULL)
9996 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009998 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9999 {
10000 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010001 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010002 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010003 else if (*s != NUL)
10004 EMSG(_(e_trailing));
10005}
10006
10007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 * "eventhandler()" function
10009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016}
10017
10018/*
10019 * "executable()" function
10020 */
10021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 typval_T *argvars;
10024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027}
10028
10029/*
10030 * "exists()" function
10031 */
10032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010034 typval_T *argvars;
10035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 char_u *p;
10038 char_u *name;
10039 int n = FALSE;
10040 int len = 0;
10041
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010042 no_autoload = TRUE;
10043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (*p == '$') /* environment variable */
10046 {
10047 /* first try "normal" environment variables (fast) */
10048 if (mch_getenv(p + 1) != NULL)
10049 n = TRUE;
10050 else
10051 {
10052 /* try expanding things like $VIM and ${HOME} */
10053 p = expand_env_save(p);
10054 if (p != NULL && *p != '$')
10055 n = TRUE;
10056 vim_free(p);
10057 }
10058 }
10059 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010062 if (*skipwhite(p) != NUL)
10063 n = FALSE; /* trailing garbage */
10064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 else if (*p == '*') /* internal or user defined function */
10066 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010067 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 }
10069 else if (*p == ':')
10070 {
10071 n = cmd_exists(p + 1);
10072 }
10073 else if (*p == '#')
10074 {
10075#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010076 if (p[1] == '#')
10077 n = autocmd_supported(p + 2);
10078 else
10079 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080#endif
10081 }
10082 else /* internal variable */
10083 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010084 char_u *tofree;
10085 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010087 /* get_name_len() takes care of expanding curly braces */
10088 name = p;
10089 len = get_name_len(&p, &tofree, TRUE, FALSE);
10090 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010092 if (tofree != NULL)
10093 name = tofree;
10094 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10095 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010097 /* handle d.key, l[idx], f(expr) */
10098 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10099 if (n)
10100 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 }
10102 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010103 if (*p != NUL)
10104 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010106 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 }
10108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010110
10111 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112}
10113
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010114#ifdef FEAT_FLOAT
10115/*
10116 * "exp()" function
10117 */
10118 static void
10119f_exp(argvars, rettv)
10120 typval_T *argvars;
10121 typval_T *rettv;
10122{
10123 float_T f;
10124
10125 rettv->v_type = VAR_FLOAT;
10126 if (get_float_arg(argvars, &f) == OK)
10127 rettv->vval.v_float = exp(f);
10128 else
10129 rettv->vval.v_float = 0.0;
10130}
10131#endif
10132
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133/*
10134 * "expand()" function
10135 */
10136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010138 typval_T *argvars;
10139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140{
10141 char_u *s;
10142 int len;
10143 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010144 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010146 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010147 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010150 if (argvars[1].v_type != VAR_UNKNOWN
10151 && argvars[2].v_type != VAR_UNKNOWN
10152 && get_tv_number_chk(&argvars[2], &error)
10153 && !error)
10154 {
10155 rettv->v_type = VAR_LIST;
10156 rettv->vval.v_list = NULL;
10157 }
10158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010159 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 if (*s == '%' || *s == '#' || *s == '<')
10161 {
10162 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010163 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010165 if (rettv->v_type == VAR_LIST)
10166 {
10167 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10168 list_append_string(rettv->vval.v_list, result, -1);
10169 else
10170 vim_free(result);
10171 }
10172 else
10173 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175 else
10176 {
10177 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010178 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010179 if (argvars[1].v_type != VAR_UNKNOWN
10180 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010181 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 if (!error)
10183 {
10184 ExpandInit(&xpc);
10185 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010186 if (p_wic)
10187 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010188 if (rettv->v_type == VAR_STRING)
10189 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10190 options, WILD_ALL);
10191 else if (rettv_list_alloc(rettv) != FAIL)
10192 {
10193 int i;
10194
10195 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10196 for (i = 0; i < xpc.xp_numfiles; i++)
10197 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10198 ExpandCleanup(&xpc);
10199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010200 }
10201 else
10202 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 }
10204}
10205
10206/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010207 * Go over all entries in "d2" and add them to "d1".
10208 * When "action" is "error" then a duplicate key is an error.
10209 * When "action" is "force" then a duplicate key is overwritten.
10210 * Otherwise duplicate keys are ignored ("action" is "keep").
10211 */
10212 void
10213dict_extend(d1, d2, action)
10214 dict_T *d1;
10215 dict_T *d2;
10216 char_u *action;
10217{
10218 dictitem_T *di1;
10219 hashitem_T *hi2;
10220 int todo;
10221
10222 todo = (int)d2->dv_hashtab.ht_used;
10223 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10224 {
10225 if (!HASHITEM_EMPTY(hi2))
10226 {
10227 --todo;
10228 di1 = dict_find(d1, hi2->hi_key, -1);
10229 if (d1->dv_scope != 0)
10230 {
10231 /* Disallow replacing a builtin function in l: and g:.
10232 * Check the key to be valid when adding to any
10233 * scope. */
10234 if (d1->dv_scope == VAR_DEF_SCOPE
10235 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10236 && var_check_func_name(hi2->hi_key,
10237 di1 == NULL))
10238 break;
10239 if (!valid_varname(hi2->hi_key))
10240 break;
10241 }
10242 if (di1 == NULL)
10243 {
10244 di1 = dictitem_copy(HI2DI(hi2));
10245 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10246 dictitem_free(di1);
10247 }
10248 else if (*action == 'e')
10249 {
10250 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10251 break;
10252 }
10253 else if (*action == 'f' && HI2DI(hi2) != di1)
10254 {
10255 clear_tv(&di1->di_tv);
10256 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10257 }
10258 }
10259 }
10260}
10261
10262/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010265 */
10266 static void
10267f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010268 typval_T *argvars;
10269 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010270{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010271 char *arg_errmsg = N_("extend() argument");
10272
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010274 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010275 list_T *l1, *l2;
10276 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 l1 = argvars[0].vval.v_list;
10281 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010282 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010283 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 {
10285 if (argvars[2].v_type != VAR_UNKNOWN)
10286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 before = get_tv_number_chk(&argvars[2], &error);
10288 if (error)
10289 return; /* type error; errmsg already given */
10290
Bram Moolenaar758711c2005-02-02 23:11:38 +000010291 if (before == l1->lv_len)
10292 item = NULL;
10293 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010295 item = list_find(l1, before);
10296 if (item == NULL)
10297 {
10298 EMSGN(_(e_listidx), before);
10299 return;
10300 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 }
10302 }
10303 else
10304 item = NULL;
10305 list_extend(l1, l2, item);
10306
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 copy_tv(&argvars[0], rettv);
10308 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010309 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010310 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10311 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010312 dict_T *d1, *d2;
10313 char_u *action;
10314 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315
10316 d1 = argvars[0].vval.v_dict;
10317 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010318 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010319 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 {
10321 /* Check the third argument. */
10322 if (argvars[2].v_type != VAR_UNKNOWN)
10323 {
10324 static char *(av[]) = {"keep", "force", "error"};
10325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 action = get_tv_string_chk(&argvars[2]);
10327 if (action == NULL)
10328 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010329 for (i = 0; i < 3; ++i)
10330 if (STRCMP(action, av[i]) == 0)
10331 break;
10332 if (i == 3)
10333 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010334 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010335 return;
10336 }
10337 }
10338 else
10339 action = (char_u *)"force";
10340
Bram Moolenaara9922d62013-05-30 13:01:18 +020010341 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343 copy_tv(&argvars[0], rettv);
10344 }
10345 }
10346 else
10347 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010348}
10349
10350/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010351 * "feedkeys()" function
10352 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010353 static void
10354f_feedkeys(argvars, rettv)
10355 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010356 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010357{
10358 int remap = TRUE;
10359 char_u *keys, *flags;
10360 char_u nbuf[NUMBUFLEN];
10361 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010362 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010363
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010364 /* This is not allowed in the sandbox. If the commands would still be
10365 * executed in the sandbox it would be OK, but it probably happens later,
10366 * when "sandbox" is no longer set. */
10367 if (check_secure())
10368 return;
10369
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370 keys = get_tv_string(&argvars[0]);
10371 if (*keys != NUL)
10372 {
10373 if (argvars[1].v_type != VAR_UNKNOWN)
10374 {
10375 flags = get_tv_string_buf(&argvars[1], nbuf);
10376 for ( ; *flags != NUL; ++flags)
10377 {
10378 switch (*flags)
10379 {
10380 case 'n': remap = FALSE; break;
10381 case 'm': remap = TRUE; break;
10382 case 't': typed = TRUE; break;
10383 }
10384 }
10385 }
10386
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010387 /* Need to escape K_SPECIAL and CSI before putting the string in the
10388 * typeahead buffer. */
10389 keys_esc = vim_strsave_escape_csi(keys);
10390 if (keys_esc != NULL)
10391 {
10392 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010393 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010394 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010395 if (vgetc_busy)
10396 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010397 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010398 }
10399}
10400
10401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * "filereadable()" function
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010409 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 char_u *p;
10411 int n;
10412
Bram Moolenaarc236c162008-07-13 17:41:49 +000010413#ifndef O_NONBLOCK
10414# define O_NONBLOCK 0
10415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010416 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010417 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10418 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 {
10420 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010421 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 }
10423 else
10424 n = FALSE;
10425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010426 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427}
10428
10429/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010430 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 * rights to write into.
10432 */
10433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010438 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439}
10440
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010441static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010442
10443 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010444findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010446 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010447 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010448{
10449#ifdef FEAT_SEARCHPATH
10450 char_u *fname;
10451 char_u *fresult = NULL;
10452 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10453 char_u *p;
10454 char_u pathbuf[NUMBUFLEN];
10455 int count = 1;
10456 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010457 int error = FALSE;
10458#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010459
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010460 rettv->vval.v_string = NULL;
10461 rettv->v_type = VAR_STRING;
10462
10463#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010465
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010466 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10469 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 else
10472 {
10473 if (*p != NUL)
10474 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010477 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479 }
10480
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010481 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10482 error = TRUE;
10483
10484 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 do
10487 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010488 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010489 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 fresult = find_file_in_path_option(first ? fname : NULL,
10491 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010492 0, first, path,
10493 find_what,
10494 curbuf->b_ffname,
10495 find_what == FINDFILE_DIR
10496 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010497 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010498
10499 if (fresult != NULL && rettv->v_type == VAR_LIST)
10500 list_append_string(rettv->vval.v_list, fresult, -1);
10501
10502 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010504
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010505 if (rettv->v_type == VAR_STRING)
10506 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010507#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010508}
10509
Bram Moolenaar33570922005-01-25 22:26:29 +000010510static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10511static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010512
10513/*
10514 * Implementation of map() and filter().
10515 */
10516 static void
10517filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010518 typval_T *argvars;
10519 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010520 int map;
10521{
10522 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010524 listitem_T *li, *nli;
10525 list_T *l = NULL;
10526 dictitem_T *di;
10527 hashtab_T *ht;
10528 hashitem_T *hi;
10529 dict_T *d = NULL;
10530 typval_T save_val;
10531 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010532 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010533 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010534 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10535 char *arg_errmsg = (map ? N_("map() argument")
10536 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010537 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010538 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539
Bram Moolenaare9a41262005-01-15 22:18:47 +000010540 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010541 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010542 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010543 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 return;
10545 }
10546 else if (argvars[0].v_type == VAR_DICT)
10547 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010548 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010549 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 return;
10551 }
10552 else
10553 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010554 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010555 return;
10556 }
10557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 expr = get_tv_string_buf_chk(&argvars[1], buf);
10559 /* On type errors, the preceding call has already displayed an error
10560 * message. Avoid a misleading error message for an empty string that
10561 * was not passed as argument. */
10562 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 prepare_vimvar(VV_VAL, &save_val);
10565 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010566
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010567 /* We reset "did_emsg" to be able to detect whether an error
10568 * occurred during evaluation of the expression. */
10569 save_did_emsg = did_emsg;
10570 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010571
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010572 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 vimvars[VV_KEY].vv_type = VAR_STRING;
10576
10577 ht = &d->dv_hashtab;
10578 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010579 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010582 if (!HASHITEM_EMPTY(hi))
10583 {
10584 --todo;
10585 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010586 if (tv_check_lock(di->di_tv.v_lock,
10587 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 break;
10589 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010590 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010591 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 break;
10593 if (!map && rem)
10594 dictitem_remove(d, di);
10595 clear_tv(&vimvars[VV_KEY].vv_tv);
10596 }
10597 }
10598 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 }
10600 else
10601 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010602 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 for (li = l->lv_first; li != NULL; li = nli)
10605 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010606 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010607 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010609 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010610 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010611 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010612 break;
10613 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010615 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010616 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010617 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010618
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010619 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010621
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010622 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010623 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624
10625 copy_tv(&argvars[0], rettv);
10626}
10627
10628 static int
10629filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010630 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 char_u *expr;
10632 int map;
10633 int *remp;
10634{
Bram Moolenaar33570922005-01-25 22:26:29 +000010635 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010637 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010640 s = expr;
10641 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010642 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 if (*s != NUL) /* check for trailing chars after expr */
10644 {
10645 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010646 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 }
10648 if (map)
10649 {
10650 /* map(): replace the list item value */
10651 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010652 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 *tv = rettv;
10654 }
10655 else
10656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010657 int error = FALSE;
10658
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010661 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010662 /* On type error, nothing has been removed; return FAIL to stop the
10663 * loop. The error message was given by get_tv_number_chk(). */
10664 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010665 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010667 retval = OK;
10668theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010669 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010670 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671}
10672
10673/*
10674 * "filter()" function
10675 */
10676 static void
10677f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010678 typval_T *argvars;
10679 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010680{
10681 filter_map(argvars, rettv, FALSE);
10682}
10683
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010685 * "finddir({fname}[, {path}[, {count}]])" function
10686 */
10687 static void
10688f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 typval_T *argvars;
10690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010691{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010692 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693}
10694
10695/*
10696 * "findfile({fname}[, {path}[, {count}]])" function
10697 */
10698 static void
10699f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010700 typval_T *argvars;
10701 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010702{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010703 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704}
10705
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010706#ifdef FEAT_FLOAT
10707/*
10708 * "float2nr({float})" function
10709 */
10710 static void
10711f_float2nr(argvars, rettv)
10712 typval_T *argvars;
10713 typval_T *rettv;
10714{
10715 float_T f;
10716
10717 if (get_float_arg(argvars, &f) == OK)
10718 {
10719 if (f < -0x7fffffff)
10720 rettv->vval.v_number = -0x7fffffff;
10721 else if (f > 0x7fffffff)
10722 rettv->vval.v_number = 0x7fffffff;
10723 else
10724 rettv->vval.v_number = (varnumber_T)f;
10725 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010726}
10727
10728/*
10729 * "floor({float})" function
10730 */
10731 static void
10732f_floor(argvars, rettv)
10733 typval_T *argvars;
10734 typval_T *rettv;
10735{
10736 float_T f;
10737
10738 rettv->v_type = VAR_FLOAT;
10739 if (get_float_arg(argvars, &f) == OK)
10740 rettv->vval.v_float = floor(f);
10741 else
10742 rettv->vval.v_float = 0.0;
10743}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010744
10745/*
10746 * "fmod()" function
10747 */
10748 static void
10749f_fmod(argvars, rettv)
10750 typval_T *argvars;
10751 typval_T *rettv;
10752{
10753 float_T fx, fy;
10754
10755 rettv->v_type = VAR_FLOAT;
10756 if (get_float_arg(argvars, &fx) == OK
10757 && get_float_arg(&argvars[1], &fy) == OK)
10758 rettv->vval.v_float = fmod(fx, fy);
10759 else
10760 rettv->vval.v_float = 0.0;
10761}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010762#endif
10763
Bram Moolenaar0d660222005-01-07 21:51:51 +000010764/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010765 * "fnameescape({string})" function
10766 */
10767 static void
10768f_fnameescape(argvars, rettv)
10769 typval_T *argvars;
10770 typval_T *rettv;
10771{
10772 rettv->vval.v_string = vim_strsave_fnameescape(
10773 get_tv_string(&argvars[0]), FALSE);
10774 rettv->v_type = VAR_STRING;
10775}
10776
10777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 * "fnamemodify({fname}, {mods})" function
10779 */
10780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010782 typval_T *argvars;
10783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784{
10785 char_u *fname;
10786 char_u *mods;
10787 int usedlen = 0;
10788 int len;
10789 char_u *fbuf = NULL;
10790 char_u buf[NUMBUFLEN];
10791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 fname = get_tv_string_chk(&argvars[0]);
10793 mods = get_tv_string_buf_chk(&argvars[1], buf);
10794 if (fname == NULL || mods == NULL)
10795 fname = NULL;
10796 else
10797 {
10798 len = (int)STRLEN(fname);
10799 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010806 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 vim_free(fbuf);
10808}
10809
Bram Moolenaar33570922005-01-25 22:26:29 +000010810static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
10812/*
10813 * "foldclosed()" function
10814 */
10815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010817 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010819 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821#ifdef FEAT_FOLDING
10822 linenr_T lnum;
10823 linenr_T first, last;
10824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10827 {
10828 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10829 {
10830 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 return;
10835 }
10836 }
10837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839}
10840
10841/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010842 * "foldclosed()" function
10843 */
10844 static void
10845f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 typval_T *argvars;
10847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010848{
10849 foldclosed_both(argvars, rettv, FALSE);
10850}
10851
10852/*
10853 * "foldclosedend()" function
10854 */
10855 static void
10856f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 typval_T *argvars;
10858 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010859{
10860 foldclosed_both(argvars, rettv, TRUE);
10861}
10862
10863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 * "foldlevel()" function
10865 */
10866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010868 typval_T *argvars UNUSED;
10869 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871#ifdef FEAT_FOLDING
10872 linenr_T lnum;
10873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010876 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878}
10879
10880/*
10881 * "foldtext()" function
10882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887{
10888#ifdef FEAT_FOLDING
10889 linenr_T lnum;
10890 char_u *s;
10891 char_u *r;
10892 int len;
10893 char *txt;
10894#endif
10895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 rettv->v_type = VAR_STRING;
10897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010899 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10900 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10901 <= curbuf->b_ml.ml_line_count
10902 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 {
10904 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10906 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 {
10908 if (!linewhite(lnum))
10909 break;
10910 ++lnum;
10911 }
10912
10913 /* Find interesting text in this line. */
10914 s = skipwhite(ml_get(lnum));
10915 /* skip C comment-start */
10916 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010919 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010921 {
10922 s = skipwhite(ml_get(lnum + 1));
10923 if (*s == '*')
10924 s = skipwhite(s + 1);
10925 }
10926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 txt = _("+-%s%3ld lines: ");
10928 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010929 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 + 20 /* for %3ld */
10931 + STRLEN(s))); /* concatenated */
10932 if (r != NULL)
10933 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10935 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10936 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 len = (int)STRLEN(r);
10938 STRCAT(r, s);
10939 /* remove 'foldmarker' and 'commentstring' */
10940 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010941 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 }
10943 }
10944#endif
10945}
10946
10947/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010948 * "foldtextresult(lnum)" function
10949 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010952 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010954{
10955#ifdef FEAT_FOLDING
10956 linenr_T lnum;
10957 char_u *text;
10958 char_u buf[51];
10959 foldinfo_T foldinfo;
10960 int fold_count;
10961#endif
10962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963 rettv->v_type = VAR_STRING;
10964 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010965#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010966 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 /* treat illegal types and illegal string values for {lnum} the same */
10968 if (lnum < 0)
10969 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010970 fold_count = foldedCount(curwin, lnum, &foldinfo);
10971 if (fold_count > 0)
10972 {
10973 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10974 &foldinfo, buf);
10975 if (text == buf)
10976 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010978 }
10979#endif
10980}
10981
10982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 * "foreground()" function
10984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010987 typval_T *argvars UNUSED;
10988 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990#ifdef FEAT_GUI
10991 if (gui.in_use)
10992 gui_mch_set_foreground();
10993#else
10994# ifdef WIN32
10995 win32_set_foreground();
10996# endif
10997#endif
10998}
10999
11000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011001 * "function()" function
11002 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011007{
11008 char_u *s;
11009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011011 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011012 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011013 /* Don't check an autoload name for existence here. */
11014 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011015 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 else
11017 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011018 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011019 {
11020 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011021 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011022
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011023 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11024 * also be called from another script. Using trans_function_name()
11025 * would also work, but some plugins depend on the name being
11026 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011027 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011028 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011029 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011030 if (rettv->vval.v_string != NULL)
11031 {
11032 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011033 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011034 }
11035 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011036 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011037 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 }
11040}
11041
11042/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011043 * "garbagecollect()" function
11044 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011045 static void
11046f_garbagecollect(argvars, rettv)
11047 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011048 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011049{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011050 /* This is postponed until we are back at the toplevel, because we may be
11051 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11052 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011053
11054 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11055 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011056}
11057
11058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011059 * "get()" function
11060 */
11061 static void
11062f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *argvars;
11064 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065{
Bram Moolenaar33570922005-01-25 22:26:29 +000011066 listitem_T *li;
11067 list_T *l;
11068 dictitem_T *di;
11069 dict_T *d;
11070 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011071
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011073 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 int error = FALSE;
11077
11078 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11079 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011080 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011081 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011082 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083 else if (argvars[0].v_type == VAR_DICT)
11084 {
11085 if ((d = argvars[0].vval.v_dict) != NULL)
11086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011087 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088 if (di != NULL)
11089 tv = &di->di_tv;
11090 }
11091 }
11092 else
11093 EMSG2(_(e_listdictarg), "get()");
11094
11095 if (tv == NULL)
11096 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011097 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 copy_tv(&argvars[2], rettv);
11099 }
11100 else
11101 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011102}
11103
Bram Moolenaar342337a2005-07-21 21:11:17 +000011104static 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 +000011105
11106/*
11107 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011108 * Return a range (from start to end) of lines in rettv from the specified
11109 * buffer.
11110 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011111 */
11112 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011113get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011114 buf_T *buf;
11115 linenr_T start;
11116 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011117 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011118 typval_T *rettv;
11119{
11120 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011121
Bram Moolenaar959a1432013-12-14 12:17:38 +010011122 rettv->v_type = VAR_STRING;
11123 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011124 if (retlist && rettv_list_alloc(rettv) == FAIL)
11125 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011126
11127 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11128 return;
11129
11130 if (!retlist)
11131 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011132 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11133 p = ml_get_buf(buf, start, FALSE);
11134 else
11135 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011136 rettv->vval.v_string = vim_strsave(p);
11137 }
11138 else
11139 {
11140 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011141 return;
11142
11143 if (start < 1)
11144 start = 1;
11145 if (end > buf->b_ml.ml_line_count)
11146 end = buf->b_ml.ml_line_count;
11147 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011148 if (list_append_string(rettv->vval.v_list,
11149 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011150 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011151 }
11152}
11153
11154/*
11155 * "getbufline()" function
11156 */
11157 static void
11158f_getbufline(argvars, rettv)
11159 typval_T *argvars;
11160 typval_T *rettv;
11161{
11162 linenr_T lnum;
11163 linenr_T end;
11164 buf_T *buf;
11165
11166 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11167 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011168 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011169 --emsg_off;
11170
Bram Moolenaar661b1822005-07-28 22:36:45 +000011171 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011172 if (argvars[2].v_type == VAR_UNKNOWN)
11173 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011174 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011175 end = get_tv_lnum_buf(&argvars[2], buf);
11176
Bram Moolenaar342337a2005-07-21 21:11:17 +000011177 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011178}
11179
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180/*
11181 * "getbufvar()" function
11182 */
11183 static void
11184f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *argvars;
11186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011187{
11188 buf_T *buf;
11189 buf_T *save_curbuf;
11190 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011191 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011192 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11195 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011197 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011198
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011199 rettv->v_type = VAR_STRING;
11200 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201
11202 if (buf != NULL && varname != NULL)
11203 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011204 /* set curbuf to be our buf, temporarily */
11205 save_curbuf = curbuf;
11206 curbuf = buf;
11207
Bram Moolenaar0d660222005-01-07 21:51:51 +000011208 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011209 {
11210 if (get_option_tv(&varname, rettv, TRUE) == OK)
11211 done = TRUE;
11212 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011213 else if (STRCMP(varname, "changedtick") == 0)
11214 {
11215 rettv->v_type = VAR_NUMBER;
11216 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011217 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011218 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011219 else
11220 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011221 /* Look up the variable. */
11222 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11223 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11224 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011225 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011228 done = TRUE;
11229 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011231
11232 /* restore previous notion of curbuf */
11233 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234 }
11235
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011236 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11237 /* use the default value */
11238 copy_tv(&argvars[2], rettv);
11239
Bram Moolenaar0d660222005-01-07 21:51:51 +000011240 --emsg_off;
11241}
11242
11243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 * "getchar()" function
11245 */
11246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011248 typval_T *argvars;
11249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250{
11251 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011254 /* Position the cursor. Needed after a message that ends in a space. */
11255 windgoto(msg_row, msg_col);
11256
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 ++no_mapping;
11258 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011259 for (;;)
11260 {
11261 if (argvars[0].v_type == VAR_UNKNOWN)
11262 /* getchar(): blocking wait. */
11263 n = safe_vgetc();
11264 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11265 /* getchar(1): only check if char avail */
11266 n = vpeekc();
11267 else if (error || vpeekc() == NUL)
11268 /* illegal argument or getchar(0) and no char avail: return zero */
11269 n = 0;
11270 else
11271 /* getchar(0) and char avail: return char */
11272 n = safe_vgetc();
11273 if (n == K_IGNORE)
11274 continue;
11275 break;
11276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 --no_mapping;
11278 --allow_keys;
11279
Bram Moolenaar219b8702006-11-01 14:32:36 +000011280 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11281 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11282 vimvars[VV_MOUSE_COL].vv_nr = 0;
11283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 if (IS_SPECIAL(n) || mod_mask != 0)
11286 {
11287 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11288 int i = 0;
11289
11290 /* Turn a special key into three bytes, plus modifier. */
11291 if (mod_mask != 0)
11292 {
11293 temp[i++] = K_SPECIAL;
11294 temp[i++] = KS_MODIFIER;
11295 temp[i++] = mod_mask;
11296 }
11297 if (IS_SPECIAL(n))
11298 {
11299 temp[i++] = K_SPECIAL;
11300 temp[i++] = K_SECOND(n);
11301 temp[i++] = K_THIRD(n);
11302 }
11303#ifdef FEAT_MBYTE
11304 else if (has_mbyte)
11305 i += (*mb_char2bytes)(n, temp + i);
11306#endif
11307 else
11308 temp[i++] = n;
11309 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 rettv->v_type = VAR_STRING;
11311 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011312
11313#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011314 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011315 {
11316 int row = mouse_row;
11317 int col = mouse_col;
11318 win_T *win;
11319 linenr_T lnum;
11320# ifdef FEAT_WINDOWS
11321 win_T *wp;
11322# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011323 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011324
11325 if (row >= 0 && col >= 0)
11326 {
11327 /* Find the window at the mouse coordinates and compute the
11328 * text position. */
11329 win = mouse_find_win(&row, &col);
11330 (void)mouse_comp_pos(win, &row, &col, &lnum);
11331# ifdef FEAT_WINDOWS
11332 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011333 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011334# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011335 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011336 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11337 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11338 }
11339 }
11340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 }
11342}
11343
11344/*
11345 * "getcharmod()" function
11346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011349 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353}
11354
11355/*
11356 * "getcmdline()" function
11357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->v_type = VAR_STRING;
11364 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365}
11366
11367/*
11368 * "getcmdpos()" function
11369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376}
11377
11378/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011379 * "getcmdtype()" function
11380 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011381 static void
11382f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011383 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011384 typval_T *rettv;
11385{
11386 rettv->v_type = VAR_STRING;
11387 rettv->vval.v_string = alloc(2);
11388 if (rettv->vval.v_string != NULL)
11389 {
11390 rettv->vval.v_string[0] = get_cmdline_type();
11391 rettv->vval.v_string[1] = NUL;
11392 }
11393}
11394
11395/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 * "getcwd()" function
11397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011400 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011403 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011406 rettv->vval.v_string = NULL;
11407 cwd = alloc(MAXPATHL);
11408 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011410 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11411 {
11412 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011414 if (rettv->vval.v_string != NULL)
11415 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011417 }
11418 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 }
11420}
11421
11422/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011423 * "getfontname()" function
11424 */
11425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011427 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011428 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 rettv->v_type = VAR_STRING;
11431 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011432#ifdef FEAT_GUI
11433 if (gui.in_use)
11434 {
11435 GuiFont font;
11436 char_u *name = NULL;
11437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011438 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011439 {
11440 /* Get the "Normal" font. Either the name saved by
11441 * hl_set_font_name() or from the font ID. */
11442 font = gui.norm_font;
11443 name = hl_get_font_name();
11444 }
11445 else
11446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011448 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11449 return;
11450 font = gui_mch_get_font(name, FALSE);
11451 if (font == NOFONT)
11452 return; /* Invalid font name, return empty string. */
11453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011455 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011456 gui_mch_free_font(font);
11457 }
11458#endif
11459}
11460
11461/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011462 * "getfperm({fname})" function
11463 */
11464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 typval_T *argvars;
11467 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011468{
11469 char_u *fname;
11470 struct stat st;
11471 char_u *perm = NULL;
11472 char_u flags[] = "rwx";
11473 int i;
11474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011478 if (mch_stat((char *)fname, &st) >= 0)
11479 {
11480 perm = vim_strsave((char_u *)"---------");
11481 if (perm != NULL)
11482 {
11483 for (i = 0; i < 9; i++)
11484 {
11485 if (st.st_mode & (1 << (8 - i)))
11486 perm[i] = flags[i % 3];
11487 }
11488 }
11489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491}
11492
11493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 * "getfsize({fname})" function
11495 */
11496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 typval_T *argvars;
11499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500{
11501 char_u *fname;
11502 struct stat st;
11503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507
11508 if (mch_stat((char *)fname, &st) >= 0)
11509 {
11510 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011515
11516 /* non-perfect check for overflow */
11517 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11518 rettv->vval.v_number = -2;
11519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 }
11521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523}
11524
11525/*
11526 * "getftime({fname})" function
11527 */
11528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011530 typval_T *argvars;
11531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
11533 char_u *fname;
11534 struct stat st;
11535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537
11538 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542}
11543
11544/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011545 * "getftype({fname})" function
11546 */
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011551{
11552 char_u *fname;
11553 struct stat st;
11554 char_u *type = NULL;
11555 char *t;
11556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011560 if (mch_lstat((char *)fname, &st) >= 0)
11561 {
11562#ifdef S_ISREG
11563 if (S_ISREG(st.st_mode))
11564 t = "file";
11565 else if (S_ISDIR(st.st_mode))
11566 t = "dir";
11567# ifdef S_ISLNK
11568 else if (S_ISLNK(st.st_mode))
11569 t = "link";
11570# endif
11571# ifdef S_ISBLK
11572 else if (S_ISBLK(st.st_mode))
11573 t = "bdev";
11574# endif
11575# ifdef S_ISCHR
11576 else if (S_ISCHR(st.st_mode))
11577 t = "cdev";
11578# endif
11579# ifdef S_ISFIFO
11580 else if (S_ISFIFO(st.st_mode))
11581 t = "fifo";
11582# endif
11583# ifdef S_ISSOCK
11584 else if (S_ISSOCK(st.st_mode))
11585 t = "fifo";
11586# endif
11587 else
11588 t = "other";
11589#else
11590# ifdef S_IFMT
11591 switch (st.st_mode & S_IFMT)
11592 {
11593 case S_IFREG: t = "file"; break;
11594 case S_IFDIR: t = "dir"; break;
11595# ifdef S_IFLNK
11596 case S_IFLNK: t = "link"; break;
11597# endif
11598# ifdef S_IFBLK
11599 case S_IFBLK: t = "bdev"; break;
11600# endif
11601# ifdef S_IFCHR
11602 case S_IFCHR: t = "cdev"; break;
11603# endif
11604# ifdef S_IFIFO
11605 case S_IFIFO: t = "fifo"; break;
11606# endif
11607# ifdef S_IFSOCK
11608 case S_IFSOCK: t = "socket"; break;
11609# endif
11610 default: t = "other";
11611 }
11612# else
11613 if (mch_isdir(fname))
11614 t = "dir";
11615 else
11616 t = "file";
11617# endif
11618#endif
11619 type = vim_strsave((char_u *)t);
11620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011622}
11623
11624/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011625 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011626 */
11627 static void
11628f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011629 typval_T *argvars;
11630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011631{
11632 linenr_T lnum;
11633 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011634 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011635
11636 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011637 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011638 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011639 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011640 retlist = FALSE;
11641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011642 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011643 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011645 retlist = TRUE;
11646 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011647
Bram Moolenaar342337a2005-07-21 21:11:17 +000011648 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649}
11650
11651/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011652 * "getmatches()" function
11653 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011654 static void
11655f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011656 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011657 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011658{
11659#ifdef FEAT_SEARCH_EXTRA
11660 dict_T *dict;
11661 matchitem_T *cur = curwin->w_match_head;
11662
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011663 if (rettv_list_alloc(rettv) == OK)
11664 {
11665 while (cur != NULL)
11666 {
11667 dict = dict_alloc();
11668 if (dict == NULL)
11669 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011670 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11671 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11672 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11673 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11674 list_append_dict(rettv->vval.v_list, dict);
11675 cur = cur->next;
11676 }
11677 }
11678#endif
11679}
11680
11681/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011682 * "getpid()" function
11683 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011684 static void
11685f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011686 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011687 typval_T *rettv;
11688{
11689 rettv->vval.v_number = mch_get_pid();
11690}
11691
11692/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011693 * "getpos(string)" function
11694 */
11695 static void
11696f_getpos(argvars, rettv)
11697 typval_T *argvars;
11698 typval_T *rettv;
11699{
11700 pos_T *fp;
11701 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011702 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011703
11704 if (rettv_list_alloc(rettv) == OK)
11705 {
11706 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011707 fp = var2fpos(&argvars[0], TRUE, &fnum);
11708 if (fnum != -1)
11709 list_append_number(l, (varnumber_T)fnum);
11710 else
11711 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011712 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11713 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011714 list_append_number(l, (fp != NULL)
11715 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011716 : (varnumber_T)0);
11717 list_append_number(l,
11718#ifdef FEAT_VIRTUALEDIT
11719 (fp != NULL) ? (varnumber_T)fp->coladd :
11720#endif
11721 (varnumber_T)0);
11722 }
11723 else
11724 rettv->vval.v_number = FALSE;
11725}
11726
11727/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011728 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011729 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011730 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011731f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011732 typval_T *argvars UNUSED;
11733 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011734{
11735#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011736 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011737#endif
11738
Bram Moolenaar2641f772005-03-25 21:58:17 +000011739#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011740 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011741 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011742 wp = NULL;
11743 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11744 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011745 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011746 if (wp == NULL)
11747 return;
11748 }
11749
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011750 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011751 }
11752#endif
11753}
11754
11755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 * "getreg()" function
11757 */
11758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 typval_T *argvars;
11761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762{
11763 char_u *strregname;
11764 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011765 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011767
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011768 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 strregname = get_tv_string_chk(&argvars[0]);
11771 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011772 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 regname = (strregname == NULL ? '"' : *strregname);
11778 if (regname == 0)
11779 regname = '"';
11780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 rettv->vval.v_string = error ? NULL :
11783 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784}
11785
11786/*
11787 * "getregtype()" function
11788 */
11789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011791 typval_T *argvars;
11792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793{
11794 char_u *strregname;
11795 int regname;
11796 char_u buf[NUMBUFLEN + 2];
11797 long reglen = 0;
11798
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011799 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 {
11801 strregname = get_tv_string_chk(&argvars[0]);
11802 if (strregname == NULL) /* type error; errmsg already given */
11803 {
11804 rettv->v_type = VAR_STRING;
11805 rettv->vval.v_string = NULL;
11806 return;
11807 }
11808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 else
11810 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011811 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812
11813 regname = (strregname == NULL ? '"' : *strregname);
11814 if (regname == 0)
11815 regname = '"';
11816
11817 buf[0] = NUL;
11818 buf[1] = NUL;
11819 switch (get_reg_type(regname, &reglen))
11820 {
11821 case MLINE: buf[0] = 'V'; break;
11822 case MCHAR: buf[0] = 'v'; break;
11823#ifdef FEAT_VISUAL
11824 case MBLOCK:
11825 buf[0] = Ctrl_V;
11826 sprintf((char *)buf + 1, "%ld", reglen + 1);
11827 break;
11828#endif
11829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011830 rettv->v_type = VAR_STRING;
11831 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832}
11833
11834/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011835 * "gettabvar()" function
11836 */
11837 static void
11838f_gettabvar(argvars, rettv)
11839 typval_T *argvars;
11840 typval_T *rettv;
11841{
11842 tabpage_T *tp;
11843 dictitem_T *v;
11844 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011845 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011846
11847 rettv->v_type = VAR_STRING;
11848 rettv->vval.v_string = NULL;
11849
11850 varname = get_tv_string_chk(&argvars[1]);
11851 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11852 if (tp != NULL && varname != NULL)
11853 {
11854 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011855 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011856 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011858 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011859 done = TRUE;
11860 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011861 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011862
11863 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011864 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011865}
11866
11867/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011868 * "gettabwinvar()" function
11869 */
11870 static void
11871f_gettabwinvar(argvars, rettv)
11872 typval_T *argvars;
11873 typval_T *rettv;
11874{
11875 getwinvar(argvars, rettv, 1);
11876}
11877
11878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 * "getwinposx()" function
11880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011882f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011886 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887#ifdef FEAT_GUI
11888 if (gui.in_use)
11889 {
11890 int x, y;
11891
11892 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 }
11895#endif
11896}
11897
11898/*
11899 * "getwinposy()" function
11900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907#ifdef FEAT_GUI
11908 if (gui.in_use)
11909 {
11910 int x, y;
11911
11912 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 }
11915#endif
11916}
11917
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011918/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011919 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011920 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011921 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011922find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011923 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011924 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011925{
11926#ifdef FEAT_WINDOWS
11927 win_T *wp;
11928#endif
11929 int nr;
11930
11931 nr = get_tv_number_chk(vp, NULL);
11932
11933#ifdef FEAT_WINDOWS
11934 if (nr < 0)
11935 return NULL;
11936 if (nr == 0)
11937 return curwin;
11938
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011939 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11940 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011941 if (--nr <= 0)
11942 break;
11943 return wp;
11944#else
11945 if (nr == 0 || nr == 1)
11946 return curwin;
11947 return NULL;
11948#endif
11949}
11950
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951/*
11952 * "getwinvar()" function
11953 */
11954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011956 typval_T *argvars;
11957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011959 getwinvar(argvars, rettv, 0);
11960}
11961
11962/*
11963 * getwinvar() and gettabwinvar()
11964 */
11965 static void
11966getwinvar(argvars, rettv, off)
11967 typval_T *argvars;
11968 typval_T *rettv;
11969 int off; /* 1 for gettabwinvar() */
11970{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971 win_T *win, *oldcurwin;
11972 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011973 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011974 tabpage_T *tp = NULL;
11975 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011976 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011978#ifdef FEAT_WINDOWS
11979 if (off == 1)
11980 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11981 else
11982 tp = curtab;
11983#endif
11984 win = find_win_by_nr(&argvars[off], tp);
11985 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011988 rettv->v_type = VAR_STRING;
11989 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
11991 if (win != NULL && varname != NULL)
11992 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011993 /* Set curwin to be our win, temporarily. Also set the tabpage,
11994 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011995 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011996
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011998 {
11999 if (get_option_tv(&varname, rettv, 1) == OK)
12000 done = TRUE;
12001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002 else
12003 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012004 /* Look up the variable. */
12005 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12006 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012009 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012010 done = TRUE;
12011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012013
12014 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012015 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 }
12017
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012018 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12019 /* use the default return value */
12020 copy_tv(&argvars[off + 2], rettv);
12021
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 --emsg_off;
12023}
12024
12025/*
12026 * "glob()" function
12027 */
12028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012030 typval_T *argvars;
12031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012033 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012035 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012037 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012038 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012040 if (argvars[1].v_type != VAR_UNKNOWN)
12041 {
12042 if (get_tv_number_chk(&argvars[1], &error))
12043 options |= WILD_KEEP_ALL;
12044 if (argvars[2].v_type != VAR_UNKNOWN
12045 && get_tv_number_chk(&argvars[2], &error))
12046 {
12047 rettv->v_type = VAR_LIST;
12048 rettv->vval.v_list = NULL;
12049 }
12050 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012051 if (!error)
12052 {
12053 ExpandInit(&xpc);
12054 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012055 if (p_wic)
12056 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012057 if (rettv->v_type == VAR_STRING)
12058 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012059 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012060 else if (rettv_list_alloc(rettv) != FAIL)
12061 {
12062 int i;
12063
12064 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12065 NULL, options, WILD_ALL_KEEP);
12066 for (i = 0; i < xpc.xp_numfiles; i++)
12067 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12068
12069 ExpandCleanup(&xpc);
12070 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012071 }
12072 else
12073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074}
12075
12076/*
12077 * "globpath()" function
12078 */
12079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 typval_T *argvars;
12082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012084 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012086 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012087 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012089 /* When the optional second argument is non-zero, don't remove matches
12090 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12091 if (argvars[2].v_type != VAR_UNKNOWN
12092 && get_tv_number_chk(&argvars[2], &error))
12093 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012094 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012095 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096 rettv->vval.v_string = NULL;
12097 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012098 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12099 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100}
12101
12102/*
12103 * "has()" function
12104 */
12105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 typval_T *argvars;
12108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109{
12110 int i;
12111 char_u *name;
12112 int n = FALSE;
12113 static char *(has_list[]) =
12114 {
12115#ifdef AMIGA
12116 "amiga",
12117# ifdef FEAT_ARP
12118 "arp",
12119# endif
12120#endif
12121#ifdef __BEOS__
12122 "beos",
12123#endif
12124#ifdef MSDOS
12125# ifdef DJGPP
12126 "dos32",
12127# else
12128 "dos16",
12129# endif
12130#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012131#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 "mac",
12133#endif
12134#if defined(MACOS_X_UNIX)
12135 "macunix",
12136#endif
12137#ifdef OS2
12138 "os2",
12139#endif
12140#ifdef __QNX__
12141 "qnx",
12142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143#ifdef UNIX
12144 "unix",
12145#endif
12146#ifdef VMS
12147 "vms",
12148#endif
12149#ifdef WIN16
12150 "win16",
12151#endif
12152#ifdef WIN32
12153 "win32",
12154#endif
12155#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12156 "win32unix",
12157#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012158#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012159 "win64",
12160#endif
12161#ifdef EBCDIC
12162 "ebcdic",
12163#endif
12164#ifndef CASE_INSENSITIVE_FILENAME
12165 "fname_case",
12166#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012167#ifdef HAVE_ACL
12168 "acl",
12169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170#ifdef FEAT_ARABIC
12171 "arabic",
12172#endif
12173#ifdef FEAT_AUTOCMD
12174 "autocmd",
12175#endif
12176#ifdef FEAT_BEVAL
12177 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12179 "balloon_multiline",
12180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181#endif
12182#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12183 "builtin_terms",
12184# ifdef ALL_BUILTIN_TCAPS
12185 "all_builtin_terms",
12186# endif
12187#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012188#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12189 || defined(FEAT_GUI_W32) \
12190 || defined(FEAT_GUI_MOTIF))
12191 "browsefilter",
12192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193#ifdef FEAT_BYTEOFF
12194 "byte_offset",
12195#endif
12196#ifdef FEAT_CINDENT
12197 "cindent",
12198#endif
12199#ifdef FEAT_CLIENTSERVER
12200 "clientserver",
12201#endif
12202#ifdef FEAT_CLIPBOARD
12203 "clipboard",
12204#endif
12205#ifdef FEAT_CMDL_COMPL
12206 "cmdline_compl",
12207#endif
12208#ifdef FEAT_CMDHIST
12209 "cmdline_hist",
12210#endif
12211#ifdef FEAT_COMMENTS
12212 "comments",
12213#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012214#ifdef FEAT_CONCEAL
12215 "conceal",
12216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217#ifdef FEAT_CRYPT
12218 "cryptv",
12219#endif
12220#ifdef FEAT_CSCOPE
12221 "cscope",
12222#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012223#ifdef FEAT_CURSORBIND
12224 "cursorbind",
12225#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012226#ifdef CURSOR_SHAPE
12227 "cursorshape",
12228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#ifdef DEBUG
12230 "debug",
12231#endif
12232#ifdef FEAT_CON_DIALOG
12233 "dialog_con",
12234#endif
12235#ifdef FEAT_GUI_DIALOG
12236 "dialog_gui",
12237#endif
12238#ifdef FEAT_DIFF
12239 "diff",
12240#endif
12241#ifdef FEAT_DIGRAPHS
12242 "digraphs",
12243#endif
12244#ifdef FEAT_DND
12245 "dnd",
12246#endif
12247#ifdef FEAT_EMACS_TAGS
12248 "emacs_tags",
12249#endif
12250 "eval", /* always present, of course! */
12251#ifdef FEAT_EX_EXTRA
12252 "ex_extra",
12253#endif
12254#ifdef FEAT_SEARCH_EXTRA
12255 "extra_search",
12256#endif
12257#ifdef FEAT_FKMAP
12258 "farsi",
12259#endif
12260#ifdef FEAT_SEARCHPATH
12261 "file_in_path",
12262#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012263#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012264 "filterpipe",
12265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#ifdef FEAT_FIND_ID
12267 "find_in_path",
12268#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012269#ifdef FEAT_FLOAT
12270 "float",
12271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#ifdef FEAT_FOLDING
12273 "folding",
12274#endif
12275#ifdef FEAT_FOOTER
12276 "footer",
12277#endif
12278#if !defined(USE_SYSTEM) && defined(UNIX)
12279 "fork",
12280#endif
12281#ifdef FEAT_GETTEXT
12282 "gettext",
12283#endif
12284#ifdef FEAT_GUI
12285 "gui",
12286#endif
12287#ifdef FEAT_GUI_ATHENA
12288# ifdef FEAT_GUI_NEXTAW
12289 "gui_neXtaw",
12290# else
12291 "gui_athena",
12292# endif
12293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294#ifdef FEAT_GUI_GTK
12295 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012298#ifdef FEAT_GUI_GNOME
12299 "gui_gnome",
12300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301#ifdef FEAT_GUI_MAC
12302 "gui_mac",
12303#endif
12304#ifdef FEAT_GUI_MOTIF
12305 "gui_motif",
12306#endif
12307#ifdef FEAT_GUI_PHOTON
12308 "gui_photon",
12309#endif
12310#ifdef FEAT_GUI_W16
12311 "gui_win16",
12312#endif
12313#ifdef FEAT_GUI_W32
12314 "gui_win32",
12315#endif
12316#ifdef FEAT_HANGULIN
12317 "hangul_input",
12318#endif
12319#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12320 "iconv",
12321#endif
12322#ifdef FEAT_INS_EXPAND
12323 "insert_expand",
12324#endif
12325#ifdef FEAT_JUMPLIST
12326 "jumplist",
12327#endif
12328#ifdef FEAT_KEYMAP
12329 "keymap",
12330#endif
12331#ifdef FEAT_LANGMAP
12332 "langmap",
12333#endif
12334#ifdef FEAT_LIBCALL
12335 "libcall",
12336#endif
12337#ifdef FEAT_LINEBREAK
12338 "linebreak",
12339#endif
12340#ifdef FEAT_LISP
12341 "lispindent",
12342#endif
12343#ifdef FEAT_LISTCMDS
12344 "listcmds",
12345#endif
12346#ifdef FEAT_LOCALMAP
12347 "localmap",
12348#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012349#ifdef FEAT_LUA
12350# ifndef DYNAMIC_LUA
12351 "lua",
12352# endif
12353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354#ifdef FEAT_MENU
12355 "menu",
12356#endif
12357#ifdef FEAT_SESSION
12358 "mksession",
12359#endif
12360#ifdef FEAT_MODIFY_FNAME
12361 "modify_fname",
12362#endif
12363#ifdef FEAT_MOUSE
12364 "mouse",
12365#endif
12366#ifdef FEAT_MOUSESHAPE
12367 "mouseshape",
12368#endif
12369#if defined(UNIX) || defined(VMS)
12370# ifdef FEAT_MOUSE_DEC
12371 "mouse_dec",
12372# endif
12373# ifdef FEAT_MOUSE_GPM
12374 "mouse_gpm",
12375# endif
12376# ifdef FEAT_MOUSE_JSB
12377 "mouse_jsbterm",
12378# endif
12379# ifdef FEAT_MOUSE_NET
12380 "mouse_netterm",
12381# endif
12382# ifdef FEAT_MOUSE_PTERM
12383 "mouse_pterm",
12384# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012385# ifdef FEAT_MOUSE_SGR
12386 "mouse_sgr",
12387# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012388# ifdef FEAT_SYSMOUSE
12389 "mouse_sysmouse",
12390# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012391# ifdef FEAT_MOUSE_URXVT
12392 "mouse_urxvt",
12393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394# ifdef FEAT_MOUSE_XTERM
12395 "mouse_xterm",
12396# endif
12397#endif
12398#ifdef FEAT_MBYTE
12399 "multi_byte",
12400#endif
12401#ifdef FEAT_MBYTE_IME
12402 "multi_byte_ime",
12403#endif
12404#ifdef FEAT_MULTI_LANG
12405 "multi_lang",
12406#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012407#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012408#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012409 "mzscheme",
12410#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412#ifdef FEAT_OLE
12413 "ole",
12414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415#ifdef FEAT_PATH_EXTRA
12416 "path_extra",
12417#endif
12418#ifdef FEAT_PERL
12419#ifndef DYNAMIC_PERL
12420 "perl",
12421#endif
12422#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012423#ifdef FEAT_PERSISTENT_UNDO
12424 "persistent_undo",
12425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426#ifdef FEAT_PYTHON
12427#ifndef DYNAMIC_PYTHON
12428 "python",
12429#endif
12430#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012431#ifdef FEAT_PYTHON3
12432#ifndef DYNAMIC_PYTHON3
12433 "python3",
12434#endif
12435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436#ifdef FEAT_POSTSCRIPT
12437 "postscript",
12438#endif
12439#ifdef FEAT_PRINTER
12440 "printer",
12441#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012442#ifdef FEAT_PROFILE
12443 "profile",
12444#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012445#ifdef FEAT_RELTIME
12446 "reltime",
12447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448#ifdef FEAT_QUICKFIX
12449 "quickfix",
12450#endif
12451#ifdef FEAT_RIGHTLEFT
12452 "rightleft",
12453#endif
12454#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12455 "ruby",
12456#endif
12457#ifdef FEAT_SCROLLBIND
12458 "scrollbind",
12459#endif
12460#ifdef FEAT_CMDL_INFO
12461 "showcmd",
12462 "cmdline_info",
12463#endif
12464#ifdef FEAT_SIGNS
12465 "signs",
12466#endif
12467#ifdef FEAT_SMARTINDENT
12468 "smartindent",
12469#endif
12470#ifdef FEAT_SNIFF
12471 "sniff",
12472#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012473#ifdef STARTUPTIME
12474 "startuptime",
12475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476#ifdef FEAT_STL_OPT
12477 "statusline",
12478#endif
12479#ifdef FEAT_SUN_WORKSHOP
12480 "sun_workshop",
12481#endif
12482#ifdef FEAT_NETBEANS_INTG
12483 "netbeans_intg",
12484#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012485#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012486 "spell",
12487#endif
12488#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 "syntax",
12490#endif
12491#if defined(USE_SYSTEM) || !defined(UNIX)
12492 "system",
12493#endif
12494#ifdef FEAT_TAG_BINS
12495 "tag_binary",
12496#endif
12497#ifdef FEAT_TAG_OLDSTATIC
12498 "tag_old_static",
12499#endif
12500#ifdef FEAT_TAG_ANYWHITE
12501 "tag_any_white",
12502#endif
12503#ifdef FEAT_TCL
12504# ifndef DYNAMIC_TCL
12505 "tcl",
12506# endif
12507#endif
12508#ifdef TERMINFO
12509 "terminfo",
12510#endif
12511#ifdef FEAT_TERMRESPONSE
12512 "termresponse",
12513#endif
12514#ifdef FEAT_TEXTOBJ
12515 "textobjects",
12516#endif
12517#ifdef HAVE_TGETENT
12518 "tgetent",
12519#endif
12520#ifdef FEAT_TITLE
12521 "title",
12522#endif
12523#ifdef FEAT_TOOLBAR
12524 "toolbar",
12525#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012526#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12527 "unnamedplus",
12528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529#ifdef FEAT_USR_CMDS
12530 "user-commands", /* was accidentally included in 5.4 */
12531 "user_commands",
12532#endif
12533#ifdef FEAT_VIMINFO
12534 "viminfo",
12535#endif
12536#ifdef FEAT_VERTSPLIT
12537 "vertsplit",
12538#endif
12539#ifdef FEAT_VIRTUALEDIT
12540 "virtualedit",
12541#endif
12542#ifdef FEAT_VISUAL
12543 "visual",
12544#endif
12545#ifdef FEAT_VISUALEXTRA
12546 "visualextra",
12547#endif
12548#ifdef FEAT_VREPLACE
12549 "vreplace",
12550#endif
12551#ifdef FEAT_WILDIGN
12552 "wildignore",
12553#endif
12554#ifdef FEAT_WILDMENU
12555 "wildmenu",
12556#endif
12557#ifdef FEAT_WINDOWS
12558 "windows",
12559#endif
12560#ifdef FEAT_WAK
12561 "winaltkeys",
12562#endif
12563#ifdef FEAT_WRITEBACKUP
12564 "writebackup",
12565#endif
12566#ifdef FEAT_XIM
12567 "xim",
12568#endif
12569#ifdef FEAT_XFONTSET
12570 "xfontset",
12571#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012572#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012573 "xpm",
12574 "xpm_w32", /* for backward compatibility */
12575#else
12576# if defined(HAVE_XPM)
12577 "xpm",
12578# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580#ifdef USE_XSMP
12581 "xsmp",
12582#endif
12583#ifdef USE_XSMP_INTERACT
12584 "xsmp_interact",
12585#endif
12586#ifdef FEAT_XCLIPBOARD
12587 "xterm_clipboard",
12588#endif
12589#ifdef FEAT_XTERM_SAVE
12590 "xterm_save",
12591#endif
12592#if defined(UNIX) && defined(FEAT_X11)
12593 "X11",
12594#endif
12595 NULL
12596 };
12597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 for (i = 0; has_list[i] != NULL; ++i)
12600 if (STRICMP(name, has_list[i]) == 0)
12601 {
12602 n = TRUE;
12603 break;
12604 }
12605
12606 if (n == FALSE)
12607 {
12608 if (STRNICMP(name, "patch", 5) == 0)
12609 n = has_patch(atoi((char *)name + 5));
12610 else if (STRICMP(name, "vim_starting") == 0)
12611 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012612#ifdef FEAT_MBYTE
12613 else if (STRICMP(name, "multi_byte_encoding") == 0)
12614 n = has_mbyte;
12615#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012616#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12617 else if (STRICMP(name, "balloon_multiline") == 0)
12618 n = multiline_balloon_available();
12619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620#ifdef DYNAMIC_TCL
12621 else if (STRICMP(name, "tcl") == 0)
12622 n = tcl_enabled(FALSE);
12623#endif
12624#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12625 else if (STRICMP(name, "iconv") == 0)
12626 n = iconv_enabled(FALSE);
12627#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012628#ifdef DYNAMIC_LUA
12629 else if (STRICMP(name, "lua") == 0)
12630 n = lua_enabled(FALSE);
12631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012632#ifdef DYNAMIC_MZSCHEME
12633 else if (STRICMP(name, "mzscheme") == 0)
12634 n = mzscheme_enabled(FALSE);
12635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636#ifdef DYNAMIC_RUBY
12637 else if (STRICMP(name, "ruby") == 0)
12638 n = ruby_enabled(FALSE);
12639#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012640#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641#ifdef DYNAMIC_PYTHON
12642 else if (STRICMP(name, "python") == 0)
12643 n = python_enabled(FALSE);
12644#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012645#endif
12646#ifdef FEAT_PYTHON3
12647#ifdef DYNAMIC_PYTHON3
12648 else if (STRICMP(name, "python3") == 0)
12649 n = python3_enabled(FALSE);
12650#endif
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef DYNAMIC_PERL
12653 else if (STRICMP(name, "perl") == 0)
12654 n = perl_enabled(FALSE);
12655#endif
12656#ifdef FEAT_GUI
12657 else if (STRICMP(name, "gui_running") == 0)
12658 n = (gui.in_use || gui.starting);
12659# ifdef FEAT_GUI_W32
12660 else if (STRICMP(name, "gui_win32s") == 0)
12661 n = gui_is_win32s();
12662# endif
12663# ifdef FEAT_BROWSE
12664 else if (STRICMP(name, "browse") == 0)
12665 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12666# endif
12667#endif
12668#ifdef FEAT_SYN_HL
12669 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012670 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671#endif
12672#if defined(WIN3264)
12673 else if (STRICMP(name, "win95") == 0)
12674 n = mch_windows95();
12675#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012676#ifdef FEAT_NETBEANS_INTG
12677 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012678 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 }
12681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683}
12684
12685/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012686 * "has_key()" function
12687 */
12688 static void
12689f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012690 typval_T *argvars;
12691 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012692{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012693 if (argvars[0].v_type != VAR_DICT)
12694 {
12695 EMSG(_(e_dictreq));
12696 return;
12697 }
12698 if (argvars[0].vval.v_dict == NULL)
12699 return;
12700
12701 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012702 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012703}
12704
12705/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012706 * "haslocaldir()" function
12707 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012708 static void
12709f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012710 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012711 typval_T *rettv;
12712{
12713 rettv->vval.v_number = (curwin->w_localdir != NULL);
12714}
12715
12716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 * "hasmapto()" function
12718 */
12719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012721 typval_T *argvars;
12722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723{
12724 char_u *name;
12725 char_u *mode;
12726 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012727 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012730 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 mode = (char_u *)"nvo";
12732 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012735 if (argvars[2].v_type != VAR_UNKNOWN)
12736 abbr = get_tv_number(&argvars[2]);
12737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
Bram Moolenaar2c932302006-03-18 21:42:09 +000012739 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743}
12744
12745/*
12746 * "histadd()" function
12747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
12753#ifdef FEAT_CMDHIST
12754 int histype;
12755 char_u *str;
12756 char_u buf[NUMBUFLEN];
12757#endif
12758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 if (check_restricted() || check_secure())
12761 return;
12762#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12764 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 if (histype >= 0)
12766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012767 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 if (*str != NUL)
12769 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012770 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 return;
12774 }
12775 }
12776#endif
12777}
12778
12779/*
12780 * "histdel()" function
12781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012784 typval_T *argvars UNUSED;
12785 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787#ifdef FEAT_CMDHIST
12788 int n;
12789 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012792 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12793 if (str == NULL)
12794 n = 0;
12795 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012798 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012800 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 else
12803 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012805 get_tv_string_buf(&argvars[1], buf));
12806 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807#endif
12808}
12809
12810/*
12811 * "histget()" function
12812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817{
12818#ifdef FEAT_CMDHIST
12819 int type;
12820 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012821 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012823 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12824 if (str == NULL)
12825 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012827 {
12828 type = get_histtype(str);
12829 if (argvars[1].v_type == VAR_UNKNOWN)
12830 idx = get_history_idx(type);
12831 else
12832 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12833 /* -1 on type error */
12834 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840}
12841
12842/*
12843 * "histnr()" function
12844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849{
12850 int i;
12851
12852#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012853 char_u *history = get_tv_string_chk(&argvars[0]);
12854
12855 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 if (i >= HIST_CMD && i < HIST_COUNT)
12857 i = get_history_idx(i);
12858 else
12859#endif
12860 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862}
12863
12864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 * "highlightID(name)" function
12866 */
12867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 typval_T *argvars;
12870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873}
12874
12875/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012876 * "highlight_exists()" function
12877 */
12878 static void
12879f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *argvars;
12881 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012882{
12883 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12884}
12885
12886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 * "hostname()" function
12888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012891 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
12894 char_u hostname[256];
12895
12896 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->v_type = VAR_STRING;
12898 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899}
12900
12901/*
12902 * iconv() function
12903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
12909#ifdef FEAT_MBYTE
12910 char_u buf1[NUMBUFLEN];
12911 char_u buf2[NUMBUFLEN];
12912 char_u *from, *to, *str;
12913 vimconv_T vimconv;
12914#endif
12915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->v_type = VAR_STRING;
12917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918
12919#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 str = get_tv_string(&argvars[0]);
12921 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12922 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 vimconv.vc_type = CONV_NONE;
12924 convert_setup(&vimconv, from, to);
12925
12926 /* If the encodings are equal, no conversion needed. */
12927 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932 convert_setup(&vimconv, NULL, NULL);
12933 vim_free(from);
12934 vim_free(to);
12935#endif
12936}
12937
12938/*
12939 * "indent()" function
12940 */
12941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *argvars;
12944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
12946 linenr_T lnum;
12947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953}
12954
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012955/*
12956 * "index()" function
12957 */
12958 static void
12959f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 typval_T *argvars;
12961 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012962{
Bram Moolenaar33570922005-01-25 22:26:29 +000012963 list_T *l;
12964 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012965 long idx = 0;
12966 int ic = FALSE;
12967
12968 rettv->vval.v_number = -1;
12969 if (argvars[0].v_type != VAR_LIST)
12970 {
12971 EMSG(_(e_listreq));
12972 return;
12973 }
12974 l = argvars[0].vval.v_list;
12975 if (l != NULL)
12976 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012977 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012980 int error = FALSE;
12981
Bram Moolenaar758711c2005-02-02 23:11:38 +000012982 /* Start at specified item. Use the cached index that list_find()
12983 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012984 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012985 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012986 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012987 ic = get_tv_number_chk(&argvars[3], &error);
12988 if (error)
12989 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012990 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012991
Bram Moolenaar758711c2005-02-02 23:11:38 +000012992 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012993 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012994 {
12995 rettv->vval.v_number = idx;
12996 break;
12997 }
12998 }
12999}
13000
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001static int inputsecret_flag = 0;
13002
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013003static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13004
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013006 * This function is used by f_input() and f_inputdialog() functions. The third
13007 * argument to f_input() specifies the type of completion to use at the
13008 * prompt. The third argument to f_inputdialog() specifies the value to return
13009 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 */
13011 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013012get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013013 typval_T *argvars;
13014 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013015 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 char_u *p = NULL;
13019 int c;
13020 char_u buf[NUMBUFLEN];
13021 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013022 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013023 int xp_type = EXPAND_NOTHING;
13024 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
13029#ifdef NO_CONSOLE_INPUT
13030 /* While starting up, there is no place to enter text. */
13031 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033#endif
13034
13035 cmd_silent = FALSE; /* Want to see the prompt. */
13036 if (prompt != NULL)
13037 {
13038 /* Only the part of the message after the last NL is considered as
13039 * prompt for the command line */
13040 p = vim_strrchr(prompt, '\n');
13041 if (p == NULL)
13042 p = prompt;
13043 else
13044 {
13045 ++p;
13046 c = *p;
13047 *p = NUL;
13048 msg_start();
13049 msg_clr_eos();
13050 msg_puts_attr(prompt, echo_attr);
13051 msg_didout = FALSE;
13052 msg_starthere();
13053 *p = c;
13054 }
13055 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 if (argvars[1].v_type != VAR_UNKNOWN)
13058 {
13059 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13060 if (defstr != NULL)
13061 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013063 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013064 {
13065 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013066 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013067 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013068
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013069 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013070 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013071
Bram Moolenaar4463f292005-09-25 22:20:24 +000013072 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13073 if (xp_name == NULL)
13074 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013075
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013076 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013077
Bram Moolenaar4463f292005-09-25 22:20:24 +000013078 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13079 &xp_arg) == FAIL)
13080 return;
13081 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013082 }
13083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013085 {
13086# ifdef FEAT_EX_EXTRA
13087 int save_ex_normal_busy = ex_normal_busy;
13088 ex_normal_busy = 0;
13089# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013091 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13092 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013093# ifdef FEAT_EX_EXTRA
13094 ex_normal_busy = save_ex_normal_busy;
13095# endif
13096 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013097 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013098 && argvars[1].v_type != VAR_UNKNOWN
13099 && argvars[2].v_type != VAR_UNKNOWN)
13100 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13101 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013102
13103 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 /* since the user typed this, no need to wait for return */
13106 need_wait_return = FALSE;
13107 msg_didout = FALSE;
13108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013109 cmd_silent = cmd_silent_save;
13110}
13111
13112/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013113 * "input()" function
13114 * Also handles inputsecret() when inputsecret is set.
13115 */
13116 static void
13117f_input(argvars, rettv)
13118 typval_T *argvars;
13119 typval_T *rettv;
13120{
13121 get_user_input(argvars, rettv, FALSE);
13122}
13123
13124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125 * "inputdialog()" function
13126 */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132#if defined(FEAT_GUI_TEXTDIALOG)
13133 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13134 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13135 {
13136 char_u *message;
13137 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013140 message = get_tv_string_chk(&argvars[0]);
13141 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013142 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013143 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 else
13145 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013146 if (message != NULL && defstr != NULL
13147 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013148 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 else
13151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 if (message != NULL && defstr != NULL
13153 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_string = vim_strsave(
13156 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 }
13162 else
13163#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013164 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165}
13166
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013167/*
13168 * "inputlist()" function
13169 */
13170 static void
13171f_inputlist(argvars, rettv)
13172 typval_T *argvars;
13173 typval_T *rettv;
13174{
13175 listitem_T *li;
13176 int selected;
13177 int mouse_used;
13178
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013179#ifdef NO_CONSOLE_INPUT
13180 /* While starting up, there is no place to enter text. */
13181 if (no_console_input())
13182 return;
13183#endif
13184 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13185 {
13186 EMSG2(_(e_listarg), "inputlist()");
13187 return;
13188 }
13189
13190 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013191 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013192 lines_left = Rows; /* avoid more prompt */
13193 msg_scroll = TRUE;
13194 msg_clr_eos();
13195
13196 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13197 {
13198 msg_puts(get_tv_string(&li->li_tv));
13199 msg_putchar('\n');
13200 }
13201
13202 /* Ask for choice. */
13203 selected = prompt_for_number(&mouse_used);
13204 if (mouse_used)
13205 selected -= lines_left;
13206
13207 rettv->vval.v_number = selected;
13208}
13209
13210
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13212
13213/*
13214 * "inputrestore()" function
13215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013218 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013219 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220{
13221 if (ga_userinput.ga_len > 0)
13222 {
13223 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13225 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013226 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 }
13228 else if (p_verbose > 1)
13229 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013230 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 }
13233}
13234
13235/*
13236 * "inputsave()" function
13237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013240 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013243 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 if (ga_grow(&ga_userinput, 1) == OK)
13245 {
13246 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13247 + ga_userinput.ga_len);
13248 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013249 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 }
13251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
13255/*
13256 * "inputsecret()" function
13257 */
13258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013260 typval_T *argvars;
13261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262{
13263 ++cmdline_star;
13264 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 --cmdline_star;
13267 --inputsecret_flag;
13268}
13269
13270/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013271 * "insert()" function
13272 */
13273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *argvars;
13276 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013277{
13278 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013279 listitem_T *item;
13280 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013282
13283 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013284 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013285 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013286 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013287 {
13288 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013289 before = get_tv_number_chk(&argvars[2], &error);
13290 if (error)
13291 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013292
Bram Moolenaar758711c2005-02-02 23:11:38 +000013293 if (before == l->lv_len)
13294 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013295 else
13296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013297 item = list_find(l, before);
13298 if (item == NULL)
13299 {
13300 EMSGN(_(e_listidx), before);
13301 l = NULL;
13302 }
13303 }
13304 if (l != NULL)
13305 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013306 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013307 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 }
13309 }
13310}
13311
13312/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013313 * "invert(expr)" function
13314 */
13315 static void
13316f_invert(argvars, rettv)
13317 typval_T *argvars;
13318 typval_T *rettv;
13319{
13320 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13321}
13322
13323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 * "isdirectory()" function
13325 */
13326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013327f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013328 typval_T *argvars;
13329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332}
13333
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013334/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013335 * "islocked()" function
13336 */
13337 static void
13338f_islocked(argvars, rettv)
13339 typval_T *argvars;
13340 typval_T *rettv;
13341{
13342 lval_T lv;
13343 char_u *end;
13344 dictitem_T *di;
13345
13346 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013347 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13348 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013349 if (end != NULL && lv.ll_name != NULL)
13350 {
13351 if (*end != NUL)
13352 EMSG(_(e_trailing));
13353 else
13354 {
13355 if (lv.ll_tv == NULL)
13356 {
13357 if (check_changedtick(lv.ll_name))
13358 rettv->vval.v_number = 1; /* always locked */
13359 else
13360 {
13361 di = find_var(lv.ll_name, NULL);
13362 if (di != NULL)
13363 {
13364 /* Consider a variable locked when:
13365 * 1. the variable itself is locked
13366 * 2. the value of the variable is locked.
13367 * 3. the List or Dict value is locked.
13368 */
13369 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13370 || tv_islocked(&di->di_tv));
13371 }
13372 }
13373 }
13374 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013375 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013376 else if (lv.ll_newkey != NULL)
13377 EMSG2(_(e_dictkey), lv.ll_newkey);
13378 else if (lv.ll_list != NULL)
13379 /* List item. */
13380 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13381 else
13382 /* Dictionary item. */
13383 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13384 }
13385 }
13386
13387 clear_lval(&lv);
13388}
13389
Bram Moolenaar33570922005-01-25 22:26:29 +000013390static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013391
13392/*
13393 * Turn a dict into a list:
13394 * "what" == 0: list of keys
13395 * "what" == 1: list of values
13396 * "what" == 2: list of items
13397 */
13398 static void
13399dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013400 typval_T *argvars;
13401 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013402 int what;
13403{
Bram Moolenaar33570922005-01-25 22:26:29 +000013404 list_T *l2;
13405 dictitem_T *di;
13406 hashitem_T *hi;
13407 listitem_T *li;
13408 listitem_T *li2;
13409 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013410 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013411
Bram Moolenaar8c711452005-01-14 21:53:12 +000013412 if (argvars[0].v_type != VAR_DICT)
13413 {
13414 EMSG(_(e_dictreq));
13415 return;
13416 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013417 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013418 return;
13419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013420 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013421 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013422
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013423 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013426 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013427 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013428 --todo;
13429 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013431 li = listitem_alloc();
13432 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013434 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013435
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013436 if (what == 0)
13437 {
13438 /* keys() */
13439 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013440 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013441 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13442 }
13443 else if (what == 1)
13444 {
13445 /* values() */
13446 copy_tv(&di->di_tv, &li->li_tv);
13447 }
13448 else
13449 {
13450 /* items() */
13451 l2 = list_alloc();
13452 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013453 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013454 li->li_tv.vval.v_list = l2;
13455 if (l2 == NULL)
13456 break;
13457 ++l2->lv_refcount;
13458
13459 li2 = listitem_alloc();
13460 if (li2 == NULL)
13461 break;
13462 list_append(l2, li2);
13463 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013464 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013465 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13466
13467 li2 = listitem_alloc();
13468 if (li2 == NULL)
13469 break;
13470 list_append(l2, li2);
13471 copy_tv(&di->di_tv, &li2->li_tv);
13472 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013473 }
13474 }
13475}
13476
13477/*
13478 * "items(dict)" function
13479 */
13480 static void
13481f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013482 typval_T *argvars;
13483 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013484{
13485 dict_list(argvars, rettv, 2);
13486}
13487
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013489 * "join()" function
13490 */
13491 static void
13492f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013493 typval_T *argvars;
13494 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013495{
13496 garray_T ga;
13497 char_u *sep;
13498
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013499 if (argvars[0].v_type != VAR_LIST)
13500 {
13501 EMSG(_(e_listreq));
13502 return;
13503 }
13504 if (argvars[0].vval.v_list == NULL)
13505 return;
13506 if (argvars[1].v_type == VAR_UNKNOWN)
13507 sep = (char_u *)" ";
13508 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510
13511 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013512
13513 if (sep != NULL)
13514 {
13515 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013516 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013517 ga_append(&ga, NUL);
13518 rettv->vval.v_string = (char_u *)ga.ga_data;
13519 }
13520 else
13521 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013522}
13523
13524/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013525 * "keys()" function
13526 */
13527 static void
13528f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013529 typval_T *argvars;
13530 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013531{
13532 dict_list(argvars, rettv, 0);
13533}
13534
13535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 * "last_buffer_nr()" function.
13537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013539f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013540 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542{
13543 int n = 0;
13544 buf_T *buf;
13545
13546 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13547 if (n < buf->b_fnum)
13548 n = buf->b_fnum;
13549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551}
13552
13553/*
13554 * "len()" function
13555 */
13556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013558 typval_T *argvars;
13559 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560{
13561 switch (argvars[0].v_type)
13562 {
13563 case VAR_STRING:
13564 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = (varnumber_T)STRLEN(
13566 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013567 break;
13568 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013571 case VAR_DICT:
13572 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13573 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013575 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013576 break;
13577 }
13578}
13579
Bram Moolenaar33570922005-01-25 22:26:29 +000013580static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581
13582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013584 typval_T *argvars;
13585 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586 int type;
13587{
13588#ifdef FEAT_LIBCALL
13589 char_u *string_in;
13590 char_u **string_result;
13591 int nr_result;
13592#endif
13593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013595 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013597
13598 if (check_restricted() || check_secure())
13599 return;
13600
13601#ifdef FEAT_LIBCALL
13602 /* The first two args must be strings, otherwise its meaningless */
13603 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13604 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013605 string_in = NULL;
13606 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013607 string_in = argvars[2].vval.v_string;
13608 if (type == VAR_NUMBER)
13609 string_result = NULL;
13610 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013611 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013612 if (mch_libcall(argvars[0].vval.v_string,
13613 argvars[1].vval.v_string,
13614 string_in,
13615 argvars[2].vval.v_number,
13616 string_result,
13617 &nr_result) == OK
13618 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013620 }
13621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622}
13623
13624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013625 * "libcall()" function
13626 */
13627 static void
13628f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013629 typval_T *argvars;
13630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013631{
13632 libcall_common(argvars, rettv, VAR_STRING);
13633}
13634
13635/*
13636 * "libcallnr()" function
13637 */
13638 static void
13639f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013642{
13643 libcall_common(argvars, rettv, VAR_NUMBER);
13644}
13645
13646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 * "line(string)" function
13648 */
13649 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013651 typval_T *argvars;
13652 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653{
13654 linenr_T lnum = 0;
13655 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013656 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013658 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 if (fp != NULL)
13660 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013661 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662}
13663
13664/*
13665 * "line2byte(lnum)" function
13666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671{
13672#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674#else
13675 linenr_T lnum;
13676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13682 if (rettv->vval.v_number >= 0)
13683 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684#endif
13685}
13686
13687/*
13688 * "lispindent(lnum)" function
13689 */
13690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013692 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694{
13695#ifdef FEAT_LISP
13696 pos_T pos;
13697 linenr_T lnum;
13698
13699 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13702 {
13703 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705 curwin->w_cursor = pos;
13706 }
13707 else
13708#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710}
13711
13712/*
13713 * "localtime()" function
13714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013717 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013720 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721}
13722
Bram Moolenaar33570922005-01-25 22:26:29 +000013723static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724
13725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013726get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013727 typval_T *argvars;
13728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 int exact;
13730{
13731 char_u *keys;
13732 char_u *which;
13733 char_u buf[NUMBUFLEN];
13734 char_u *keys_buf = NULL;
13735 char_u *rhs;
13736 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013737 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013738 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013739 mapblock_T *mp;
13740 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
13742 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->v_type = VAR_STRING;
13744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013746 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 if (*keys == NUL)
13748 return;
13749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013750 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013752 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013753 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013754 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013755 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013756 if (argvars[3].v_type != VAR_UNKNOWN)
13757 get_dict = get_tv_number(&argvars[3]);
13758 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 else
13761 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013762 if (which == NULL)
13763 return;
13764
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 mode = get_map_mode(&which, 0);
13766
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013767 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013768 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013770
13771 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013773 /* Return a string. */
13774 if (rhs != NULL)
13775 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776
Bram Moolenaarbd743252010-10-20 21:23:33 +020013777 }
13778 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13779 {
13780 /* Return a dictionary. */
13781 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13782 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13783 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784
Bram Moolenaarbd743252010-10-20 21:23:33 +020013785 dict_add_nr_str(dict, "lhs", 0L, lhs);
13786 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13787 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13788 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13789 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13790 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13791 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013792 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013793 dict_add_nr_str(dict, "mode", 0L, mapmode);
13794
13795 vim_free(lhs);
13796 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 }
13798}
13799
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013800#ifdef FEAT_FLOAT
13801/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013802 * "log()" function
13803 */
13804 static void
13805f_log(argvars, rettv)
13806 typval_T *argvars;
13807 typval_T *rettv;
13808{
13809 float_T f;
13810
13811 rettv->v_type = VAR_FLOAT;
13812 if (get_float_arg(argvars, &f) == OK)
13813 rettv->vval.v_float = log(f);
13814 else
13815 rettv->vval.v_float = 0.0;
13816}
13817
13818/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013819 * "log10()" function
13820 */
13821 static void
13822f_log10(argvars, rettv)
13823 typval_T *argvars;
13824 typval_T *rettv;
13825{
13826 float_T f;
13827
13828 rettv->v_type = VAR_FLOAT;
13829 if (get_float_arg(argvars, &f) == OK)
13830 rettv->vval.v_float = log10(f);
13831 else
13832 rettv->vval.v_float = 0.0;
13833}
13834#endif
13835
Bram Moolenaar1dced572012-04-05 16:54:08 +020013836#ifdef FEAT_LUA
13837/*
13838 * "luaeval()" function
13839 */
13840 static void
13841f_luaeval(argvars, rettv)
13842 typval_T *argvars;
13843 typval_T *rettv;
13844{
13845 char_u *str;
13846 char_u buf[NUMBUFLEN];
13847
13848 str = get_tv_string_buf(&argvars[0], buf);
13849 do_luaeval(str, argvars + 1, rettv);
13850}
13851#endif
13852
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 * "map()" function
13855 */
13856 static void
13857f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013858 typval_T *argvars;
13859 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860{
13861 filter_map(argvars, rettv, TRUE);
13862}
13863
13864/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866 */
13867 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013868f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013869 typval_T *argvars;
13870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013872 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873}
13874
13875/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013876 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 */
13878 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 typval_T *argvars;
13881 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013883 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884}
13885
Bram Moolenaar33570922005-01-25 22:26:29 +000013886static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887
13888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013889find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013890 typval_T *argvars;
13891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 int type;
13893{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013894 char_u *str = NULL;
13895 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 char_u *pat;
13897 regmatch_T regmatch;
13898 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 char_u *save_cpo;
13901 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013902 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013903 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013904 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013905 list_T *l = NULL;
13906 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013907 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013908 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909
13910 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13911 save_cpo = p_cpo;
13912 p_cpo = (char_u *)"";
13913
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013914 rettv->vval.v_number = -1;
13915 if (type == 3)
13916 {
13917 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013918 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013919 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013920 }
13921 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923 rettv->v_type = VAR_STRING;
13924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013927 if (argvars[0].v_type == VAR_LIST)
13928 {
13929 if ((l = argvars[0].vval.v_list) == NULL)
13930 goto theend;
13931 li = l->lv_first;
13932 }
13933 else
13934 expr = str = get_tv_string(&argvars[0]);
13935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013936 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13937 if (pat == NULL)
13938 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013942 int error = FALSE;
13943
13944 start = get_tv_number_chk(&argvars[2], &error);
13945 if (error)
13946 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013947 if (l != NULL)
13948 {
13949 li = list_find(l, start);
13950 if (li == NULL)
13951 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013952 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013953 }
13954 else
13955 {
13956 if (start < 0)
13957 start = 0;
13958 if (start > (long)STRLEN(str))
13959 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013960 /* When "count" argument is there ignore matches before "start",
13961 * otherwise skip part of the string. Differs when pattern is "^"
13962 * or "\<". */
13963 if (argvars[3].v_type != VAR_UNKNOWN)
13964 startcol = start;
13965 else
13966 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013968
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013969 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970 nth = get_tv_number_chk(&argvars[3], &error);
13971 if (error)
13972 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 }
13974
13975 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13976 if (regmatch.regprog != NULL)
13977 {
13978 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013979
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013980 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013981 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013982 if (l != NULL)
13983 {
13984 if (li == NULL)
13985 {
13986 match = FALSE;
13987 break;
13988 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013989 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013990 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013991 if (str == NULL)
13992 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013993 }
13994
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013995 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013996
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013997 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013998 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013999 if (l == NULL && !match)
14000 break;
14001
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014002 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014003 if (l != NULL)
14004 {
14005 li = li->li_next;
14006 ++idx;
14007 }
14008 else
14009 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014010#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014011 startcol = (colnr_T)(regmatch.startp[0]
14012 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014013#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014014 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014015#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014017 }
14018
14019 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014021 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014022 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014023 int i;
14024
14025 /* return list with matched string and submatches */
14026 for (i = 0; i < NSUBEXP; ++i)
14027 {
14028 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014029 {
14030 if (list_append_string(rettv->vval.v_list,
14031 (char_u *)"", 0) == FAIL)
14032 break;
14033 }
14034 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014035 regmatch.startp[i],
14036 (int)(regmatch.endp[i] - regmatch.startp[i]))
14037 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014038 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014039 }
14040 }
14041 else if (type == 2)
14042 {
14043 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014044 if (l != NULL)
14045 copy_tv(&li->li_tv, rettv);
14046 else
14047 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014049 }
14050 else if (l != NULL)
14051 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 else
14053 {
14054 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 (varnumber_T)(regmatch.startp[0] - str);
14057 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014060 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 }
14062 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014063 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 }
14065
14066theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014067 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 p_cpo = save_cpo;
14069}
14070
14071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014072 * "match()" function
14073 */
14074 static void
14075f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014076 typval_T *argvars;
14077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014078{
14079 find_some_match(argvars, rettv, 1);
14080}
14081
14082/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014083 * "matchadd()" function
14084 */
14085 static void
14086f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014087 typval_T *argvars UNUSED;
14088 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014089{
14090#ifdef FEAT_SEARCH_EXTRA
14091 char_u buf[NUMBUFLEN];
14092 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14093 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14094 int prio = 10; /* default priority */
14095 int id = -1;
14096 int error = FALSE;
14097
14098 rettv->vval.v_number = -1;
14099
14100 if (grp == NULL || pat == NULL)
14101 return;
14102 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014103 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014104 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014105 if (argvars[3].v_type != VAR_UNKNOWN)
14106 id = get_tv_number_chk(&argvars[3], &error);
14107 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014108 if (error == TRUE)
14109 return;
14110 if (id >= 1 && id <= 3)
14111 {
14112 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14113 return;
14114 }
14115
14116 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14117#endif
14118}
14119
14120/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014121 * "matcharg()" function
14122 */
14123 static void
14124f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014125 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014126 typval_T *rettv;
14127{
14128 if (rettv_list_alloc(rettv) == OK)
14129 {
14130#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014131 int id = get_tv_number(&argvars[0]);
14132 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014133
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014134 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014135 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014136 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14137 {
14138 list_append_string(rettv->vval.v_list,
14139 syn_id2name(m->hlg_id), -1);
14140 list_append_string(rettv->vval.v_list, m->pattern, -1);
14141 }
14142 else
14143 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014144 list_append_string(rettv->vval.v_list, NULL, -1);
14145 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014146 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014147 }
14148#endif
14149 }
14150}
14151
14152/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014153 * "matchdelete()" function
14154 */
14155 static void
14156f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014157 typval_T *argvars UNUSED;
14158 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014159{
14160#ifdef FEAT_SEARCH_EXTRA
14161 rettv->vval.v_number = match_delete(curwin,
14162 (int)get_tv_number(&argvars[0]), TRUE);
14163#endif
14164}
14165
14166/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014167 * "matchend()" function
14168 */
14169 static void
14170f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014171 typval_T *argvars;
14172 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173{
14174 find_some_match(argvars, rettv, 0);
14175}
14176
14177/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014178 * "matchlist()" function
14179 */
14180 static void
14181f_matchlist(argvars, rettv)
14182 typval_T *argvars;
14183 typval_T *rettv;
14184{
14185 find_some_match(argvars, rettv, 3);
14186}
14187
14188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014189 * "matchstr()" function
14190 */
14191 static void
14192f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014195{
14196 find_some_match(argvars, rettv, 2);
14197}
14198
Bram Moolenaar33570922005-01-25 22:26:29 +000014199static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014200
14201 static void
14202max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 typval_T *argvars;
14204 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014205 int domax;
14206{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014207 long n = 0;
14208 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014210
14211 if (argvars[0].v_type == VAR_LIST)
14212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 list_T *l;
14214 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014215
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014216 l = argvars[0].vval.v_list;
14217 if (l != NULL)
14218 {
14219 li = l->lv_first;
14220 if (li != NULL)
14221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014223 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014224 {
14225 li = li->li_next;
14226 if (li == NULL)
14227 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014229 if (domax ? i > n : i < n)
14230 n = i;
14231 }
14232 }
14233 }
14234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014235 else if (argvars[0].v_type == VAR_DICT)
14236 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014237 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014238 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014240 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014241
14242 d = argvars[0].vval.v_dict;
14243 if (d != NULL)
14244 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014245 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014246 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014247 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014248 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014249 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014250 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014252 if (first)
14253 {
14254 n = i;
14255 first = FALSE;
14256 }
14257 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014258 n = i;
14259 }
14260 }
14261 }
14262 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014263 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014264 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014266}
14267
14268/*
14269 * "max()" function
14270 */
14271 static void
14272f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014273 typval_T *argvars;
14274 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014275{
14276 max_min(argvars, rettv, TRUE);
14277}
14278
14279/*
14280 * "min()" function
14281 */
14282 static void
14283f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 typval_T *argvars;
14285 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014286{
14287 max_min(argvars, rettv, FALSE);
14288}
14289
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014290static int mkdir_recurse __ARGS((char_u *dir, int prot));
14291
14292/*
14293 * Create the directory in which "dir" is located, and higher levels when
14294 * needed.
14295 */
14296 static int
14297mkdir_recurse(dir, prot)
14298 char_u *dir;
14299 int prot;
14300{
14301 char_u *p;
14302 char_u *updir;
14303 int r = FAIL;
14304
14305 /* Get end of directory name in "dir".
14306 * We're done when it's "/" or "c:/". */
14307 p = gettail_sep(dir);
14308 if (p <= get_past_head(dir))
14309 return OK;
14310
14311 /* If the directory exists we're done. Otherwise: create it.*/
14312 updir = vim_strnsave(dir, (int)(p - dir));
14313 if (updir == NULL)
14314 return FAIL;
14315 if (mch_isdir(updir))
14316 r = OK;
14317 else if (mkdir_recurse(updir, prot) == OK)
14318 r = vim_mkdir_emsg(updir, prot);
14319 vim_free(updir);
14320 return r;
14321}
14322
14323#ifdef vim_mkdir
14324/*
14325 * "mkdir()" function
14326 */
14327 static void
14328f_mkdir(argvars, rettv)
14329 typval_T *argvars;
14330 typval_T *rettv;
14331{
14332 char_u *dir;
14333 char_u buf[NUMBUFLEN];
14334 int prot = 0755;
14335
14336 rettv->vval.v_number = FAIL;
14337 if (check_restricted() || check_secure())
14338 return;
14339
14340 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014341 if (*dir == NUL)
14342 rettv->vval.v_number = FAIL;
14343 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014344 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014345 if (*gettail(dir) == NUL)
14346 /* remove trailing slashes */
14347 *gettail_sep(dir) = NUL;
14348
14349 if (argvars[1].v_type != VAR_UNKNOWN)
14350 {
14351 if (argvars[2].v_type != VAR_UNKNOWN)
14352 prot = get_tv_number_chk(&argvars[2], NULL);
14353 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14354 mkdir_recurse(dir, prot);
14355 }
14356 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014357 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014358}
14359#endif
14360
Bram Moolenaar0d660222005-01-07 21:51:51 +000014361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 * "mode()" function
14363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014366 typval_T *argvars;
14367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014369 char_u buf[3];
14370
14371 buf[1] = NUL;
14372 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373
14374#ifdef FEAT_VISUAL
14375 if (VIsual_active)
14376 {
14377 if (VIsual_select)
14378 buf[0] = VIsual_mode + 's' - 'v';
14379 else
14380 buf[0] = VIsual_mode;
14381 }
14382 else
14383#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014384 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14385 || State == CONFIRM)
14386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014388 if (State == ASKMORE)
14389 buf[1] = 'm';
14390 else if (State == CONFIRM)
14391 buf[1] = '?';
14392 }
14393 else if (State == EXTERNCMD)
14394 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 else if (State & INSERT)
14396 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014397#ifdef FEAT_VREPLACE
14398 if (State & VREPLACE_FLAG)
14399 {
14400 buf[0] = 'R';
14401 buf[1] = 'v';
14402 }
14403 else
14404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 if (State & REPLACE_FLAG)
14406 buf[0] = 'R';
14407 else
14408 buf[0] = 'i';
14409 }
14410 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014413 if (exmode_active)
14414 buf[1] = 'v';
14415 }
14416 else if (exmode_active)
14417 {
14418 buf[0] = 'c';
14419 buf[1] = 'e';
14420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014424 if (finish_op)
14425 buf[1] = 'o';
14426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014428 /* Clear out the minor mode when the argument is not a non-zero number or
14429 * non-empty string. */
14430 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014431 buf[1] = NUL;
14432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->vval.v_string = vim_strsave(buf);
14434 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435}
14436
Bram Moolenaar429fa852013-04-15 12:27:36 +020014437#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014438/*
14439 * "mzeval()" function
14440 */
14441 static void
14442f_mzeval(argvars, rettv)
14443 typval_T *argvars;
14444 typval_T *rettv;
14445{
14446 char_u *str;
14447 char_u buf[NUMBUFLEN];
14448
14449 str = get_tv_string_buf(&argvars[0], buf);
14450 do_mzeval(str, rettv);
14451}
Bram Moolenaar75676462013-01-30 14:55:42 +010014452
14453 void
14454mzscheme_call_vim(name, args, rettv)
14455 char_u *name;
14456 typval_T *args;
14457 typval_T *rettv;
14458{
14459 typval_T argvars[3];
14460
14461 argvars[0].v_type = VAR_STRING;
14462 argvars[0].vval.v_string = name;
14463 copy_tv(args, &argvars[1]);
14464 argvars[2].v_type = VAR_UNKNOWN;
14465 f_call(argvars, rettv);
14466 clear_tv(&argvars[1]);
14467}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014468#endif
14469
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471 * "nextnonblank()" function
14472 */
14473 static void
14474f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477{
14478 linenr_T lnum;
14479
14480 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014483 {
14484 lnum = 0;
14485 break;
14486 }
14487 if (*skipwhite(ml_get(lnum)) != NUL)
14488 break;
14489 }
14490 rettv->vval.v_number = lnum;
14491}
14492
14493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 * "nr2char()" function
14495 */
14496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014498 typval_T *argvars;
14499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500{
14501 char_u buf[NUMBUFLEN];
14502
14503#ifdef FEAT_MBYTE
14504 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014505 {
14506 int utf8 = 0;
14507
14508 if (argvars[1].v_type != VAR_UNKNOWN)
14509 utf8 = get_tv_number_chk(&argvars[1], NULL);
14510 if (utf8)
14511 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14512 else
14513 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 else
14516#endif
14517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 buf[1] = NUL;
14520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 rettv->v_type = VAR_STRING;
14522 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014523}
14524
14525/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014526 * "or(expr, expr)" function
14527 */
14528 static void
14529f_or(argvars, rettv)
14530 typval_T *argvars;
14531 typval_T *rettv;
14532{
14533 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14534 | get_tv_number_chk(&argvars[1], NULL);
14535}
14536
14537/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014538 * "pathshorten()" function
14539 */
14540 static void
14541f_pathshorten(argvars, rettv)
14542 typval_T *argvars;
14543 typval_T *rettv;
14544{
14545 char_u *p;
14546
14547 rettv->v_type = VAR_STRING;
14548 p = get_tv_string_chk(&argvars[0]);
14549 if (p == NULL)
14550 rettv->vval.v_string = NULL;
14551 else
14552 {
14553 p = vim_strsave(p);
14554 rettv->vval.v_string = p;
14555 if (p != NULL)
14556 shorten_dir(p);
14557 }
14558}
14559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014560#ifdef FEAT_FLOAT
14561/*
14562 * "pow()" function
14563 */
14564 static void
14565f_pow(argvars, rettv)
14566 typval_T *argvars;
14567 typval_T *rettv;
14568{
14569 float_T fx, fy;
14570
14571 rettv->v_type = VAR_FLOAT;
14572 if (get_float_arg(argvars, &fx) == OK
14573 && get_float_arg(&argvars[1], &fy) == OK)
14574 rettv->vval.v_float = pow(fx, fy);
14575 else
14576 rettv->vval.v_float = 0.0;
14577}
14578#endif
14579
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581 * "prevnonblank()" function
14582 */
14583 static void
14584f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014585 typval_T *argvars;
14586 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587{
14588 linenr_T lnum;
14589
14590 lnum = get_tv_lnum(argvars);
14591 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14592 lnum = 0;
14593 else
14594 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14595 --lnum;
14596 rettv->vval.v_number = lnum;
14597}
14598
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014599#ifdef HAVE_STDARG_H
14600/* This dummy va_list is here because:
14601 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14602 * - locally in the function results in a "used before set" warning
14603 * - using va_start() to initialize it gives "function with fixed args" error */
14604static va_list ap;
14605#endif
14606
Bram Moolenaar8c711452005-01-14 21:53:12 +000014607/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014608 * "printf()" function
14609 */
14610 static void
14611f_printf(argvars, rettv)
14612 typval_T *argvars;
14613 typval_T *rettv;
14614{
14615 rettv->v_type = VAR_STRING;
14616 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014617#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014618 {
14619 char_u buf[NUMBUFLEN];
14620 int len;
14621 char_u *s;
14622 int saved_did_emsg = did_emsg;
14623 char *fmt;
14624
14625 /* Get the required length, allocate the buffer and do it for real. */
14626 did_emsg = FALSE;
14627 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014628 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014629 if (!did_emsg)
14630 {
14631 s = alloc(len + 1);
14632 if (s != NULL)
14633 {
14634 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014635 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014636 }
14637 }
14638 did_emsg |= saved_did_emsg;
14639 }
14640#endif
14641}
14642
14643/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014644 * "pumvisible()" function
14645 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014646 static void
14647f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014648 typval_T *argvars UNUSED;
14649 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014650{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014651#ifdef FEAT_INS_EXPAND
14652 if (pum_visible())
14653 rettv->vval.v_number = 1;
14654#endif
14655}
14656
Bram Moolenaardb913952012-06-29 12:54:53 +020014657#ifdef FEAT_PYTHON3
14658/*
14659 * "py3eval()" function
14660 */
14661 static void
14662f_py3eval(argvars, rettv)
14663 typval_T *argvars;
14664 typval_T *rettv;
14665{
14666 char_u *str;
14667 char_u buf[NUMBUFLEN];
14668
14669 str = get_tv_string_buf(&argvars[0], buf);
14670 do_py3eval(str, rettv);
14671}
14672#endif
14673
14674#ifdef FEAT_PYTHON
14675/*
14676 * "pyeval()" function
14677 */
14678 static void
14679f_pyeval(argvars, rettv)
14680 typval_T *argvars;
14681 typval_T *rettv;
14682{
14683 char_u *str;
14684 char_u buf[NUMBUFLEN];
14685
14686 str = get_tv_string_buf(&argvars[0], buf);
14687 do_pyeval(str, rettv);
14688}
14689#endif
14690
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014691/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014692 * "range()" function
14693 */
14694 static void
14695f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 typval_T *argvars;
14697 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014698{
14699 long start;
14700 long end;
14701 long stride = 1;
14702 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014706 if (argvars[1].v_type == VAR_UNKNOWN)
14707 {
14708 end = start - 1;
14709 start = 0;
14710 }
14711 else
14712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014716 }
14717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 if (error)
14719 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014720 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014721 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014722 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014723 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014724 else
14725 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014726 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014727 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014728 if (list_append_number(rettv->vval.v_list,
14729 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014730 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014731 }
14732}
14733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014734/*
14735 * "readfile()" function
14736 */
14737 static void
14738f_readfile(argvars, rettv)
14739 typval_T *argvars;
14740 typval_T *rettv;
14741{
14742 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744 char_u *fname;
14745 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14747 int io_size = sizeof(buf);
14748 int readlen; /* size of last fread() */
14749 char_u *prev = NULL; /* previously read bytes, if any */
14750 long prevlen = 0; /* length of data in prev */
14751 long prevsize = 0; /* size of prev buffer */
14752 long maxline = MAXLNUM;
14753 long cnt = 0;
14754 char_u *p; /* position in buf */
14755 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014756
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014757 if (argvars[1].v_type != VAR_UNKNOWN)
14758 {
14759 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14760 binary = TRUE;
14761 if (argvars[2].v_type != VAR_UNKNOWN)
14762 maxline = get_tv_number(&argvars[2]);
14763 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014764
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014765 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014766 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014767
14768 /* Always open the file in binary mode, library functions have a mind of
14769 * their own about CR-LF conversion. */
14770 fname = get_tv_string(&argvars[0]);
14771 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14772 {
14773 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14774 return;
14775 }
14776
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014777 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014778 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014780
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014781 /* This for loop processes what was read, but is also entered at end
14782 * of file so that either:
14783 * - an incomplete line gets written
14784 * - a "binary" file gets an empty line at the end if it ends in a
14785 * newline. */
14786 for (p = buf, start = buf;
14787 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14788 ++p)
14789 {
14790 if (*p == '\n' || readlen <= 0)
14791 {
14792 listitem_T *li;
14793 char_u *s = NULL;
14794 long_u len = p - start;
14795
14796 /* Finished a line. Remove CRs before NL. */
14797 if (readlen > 0 && !binary)
14798 {
14799 while (len > 0 && start[len - 1] == '\r')
14800 --len;
14801 /* removal may cross back to the "prev" string */
14802 if (len == 0)
14803 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14804 --prevlen;
14805 }
14806 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014807 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014808 else
14809 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014810 /* Change "prev" buffer to be the right size. This way
14811 * the bytes are only copied once, and very long lines are
14812 * allocated only once. */
14813 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014814 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014815 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014816 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014817 prev = NULL; /* the list will own the string */
14818 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014819 }
14820 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821 if (s == NULL)
14822 {
14823 do_outofmem_msg((long_u) prevlen + len + 1);
14824 failed = TRUE;
14825 break;
14826 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829 {
14830 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014831 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 break;
14833 }
14834 li->li_tv.v_type = VAR_STRING;
14835 li->li_tv.v_lock = 0;
14836 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014837 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014838
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014839 start = p + 1; /* step over newline */
14840 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841 break;
14842 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014843 else if (*p == NUL)
14844 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014845#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014846 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14847 * when finding the BF and check the previous two bytes. */
14848 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014849 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014850 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14851 * + 1, these may be in the "prev" string. */
14852 char_u back1 = p >= buf + 1 ? p[-1]
14853 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14854 char_u back2 = p >= buf + 2 ? p[-2]
14855 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14856 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014860 char_u *dest = p - 2;
14861
14862 /* Usually a BOM is at the beginning of a file, and so at
14863 * the beginning of a line; then we can just step over it.
14864 */
14865 if (start == dest)
14866 start = p + 1;
14867 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014868 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 /* have to shuffle buf to close gap */
14870 int adjust_prevlen = 0;
14871
14872 if (dest < buf)
14873 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014874 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014875 dest = buf;
14876 }
14877 if (readlen > p - buf + 1)
14878 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14879 readlen -= 3 - adjust_prevlen;
14880 prevlen -= adjust_prevlen;
14881 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014882 }
14883 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014884 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014885#endif
14886 } /* for */
14887
14888 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14889 break;
14890 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014891 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014892 /* There's part of a line in buf, store it in "prev". */
14893 if (p - start + prevlen >= prevsize)
14894 {
14895 /* need bigger "prev" buffer */
14896 char_u *newprev;
14897
14898 /* A common use case is ordinary text files and "prev" gets a
14899 * fragment of a line, so the first allocation is made
14900 * small, to avoid repeatedly 'allocing' large and
14901 * 'reallocing' small. */
14902 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014903 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014904 else
14905 {
14906 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014907 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014908 prevsize = grow50pc > growmin ? grow50pc : growmin;
14909 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014910 newprev = prev == NULL ? alloc(prevsize)
14911 : vim_realloc(prev, prevsize);
14912 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014913 {
14914 do_outofmem_msg((long_u)prevsize);
14915 failed = TRUE;
14916 break;
14917 }
14918 prev = newprev;
14919 }
14920 /* Add the line part to end of "prev". */
14921 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014922 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014923 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014924 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014925
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014926 /*
14927 * For a negative line count use only the lines at the end of the file,
14928 * free the rest.
14929 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014931 while (cnt > -maxline)
14932 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014933 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014934 --cnt;
14935 }
14936
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014937 if (failed)
14938 {
14939 list_free(rettv->vval.v_list, TRUE);
14940 /* readfile doc says an empty list is returned on error */
14941 rettv->vval.v_list = list_alloc();
14942 }
14943
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014944 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945 fclose(fd);
14946}
14947
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014948#if defined(FEAT_RELTIME)
14949static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14950
14951/*
14952 * Convert a List to proftime_T.
14953 * Return FAIL when there is something wrong.
14954 */
14955 static int
14956list2proftime(arg, tm)
14957 typval_T *arg;
14958 proftime_T *tm;
14959{
14960 long n1, n2;
14961 int error = FALSE;
14962
14963 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14964 || arg->vval.v_list->lv_len != 2)
14965 return FAIL;
14966 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14967 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14968# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014969 tm->HighPart = n1;
14970 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014971# else
14972 tm->tv_sec = n1;
14973 tm->tv_usec = n2;
14974# endif
14975 return error ? FAIL : OK;
14976}
14977#endif /* FEAT_RELTIME */
14978
14979/*
14980 * "reltime()" function
14981 */
14982 static void
14983f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014984 typval_T *argvars UNUSED;
14985 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014986{
14987#ifdef FEAT_RELTIME
14988 proftime_T res;
14989 proftime_T start;
14990
14991 if (argvars[0].v_type == VAR_UNKNOWN)
14992 {
14993 /* No arguments: get current time. */
14994 profile_start(&res);
14995 }
14996 else if (argvars[1].v_type == VAR_UNKNOWN)
14997 {
14998 if (list2proftime(&argvars[0], &res) == FAIL)
14999 return;
15000 profile_end(&res);
15001 }
15002 else
15003 {
15004 /* Two arguments: compute the difference. */
15005 if (list2proftime(&argvars[0], &start) == FAIL
15006 || list2proftime(&argvars[1], &res) == FAIL)
15007 return;
15008 profile_sub(&res, &start);
15009 }
15010
15011 if (rettv_list_alloc(rettv) == OK)
15012 {
15013 long n1, n2;
15014
15015# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015016 n1 = res.HighPart;
15017 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015018# else
15019 n1 = res.tv_sec;
15020 n2 = res.tv_usec;
15021# endif
15022 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15023 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15024 }
15025#endif
15026}
15027
15028/*
15029 * "reltimestr()" function
15030 */
15031 static void
15032f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015033 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015034 typval_T *rettv;
15035{
15036#ifdef FEAT_RELTIME
15037 proftime_T tm;
15038#endif
15039
15040 rettv->v_type = VAR_STRING;
15041 rettv->vval.v_string = NULL;
15042#ifdef FEAT_RELTIME
15043 if (list2proftime(&argvars[0], &tm) == OK)
15044 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15045#endif
15046}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015047
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15049static void make_connection __ARGS((void));
15050static int check_connection __ARGS((void));
15051
15052 static void
15053make_connection()
15054{
15055 if (X_DISPLAY == NULL
15056# ifdef FEAT_GUI
15057 && !gui.in_use
15058# endif
15059 )
15060 {
15061 x_force_connect = TRUE;
15062 setup_term_clip();
15063 x_force_connect = FALSE;
15064 }
15065}
15066
15067 static int
15068check_connection()
15069{
15070 make_connection();
15071 if (X_DISPLAY == NULL)
15072 {
15073 EMSG(_("E240: No connection to Vim server"));
15074 return FAIL;
15075 }
15076 return OK;
15077}
15078#endif
15079
15080#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015081static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082
15083 static void
15084remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *argvars;
15086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 int expr;
15088{
15089 char_u *server_name;
15090 char_u *keys;
15091 char_u *r = NULL;
15092 char_u buf[NUMBUFLEN];
15093# ifdef WIN32
15094 HWND w;
15095# else
15096 Window w;
15097# endif
15098
15099 if (check_restricted() || check_secure())
15100 return;
15101
15102# ifdef FEAT_X11
15103 if (check_connection() == FAIL)
15104 return;
15105# endif
15106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 server_name = get_tv_string_chk(&argvars[0]);
15108 if (server_name == NULL)
15109 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110 keys = get_tv_string_buf(&argvars[1], buf);
15111# ifdef WIN32
15112 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15113# else
15114 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15115 < 0)
15116# endif
15117 {
15118 if (r != NULL)
15119 EMSG(r); /* sending worked but evaluation failed */
15120 else
15121 EMSG2(_("E241: Unable to send to %s"), server_name);
15122 return;
15123 }
15124
15125 rettv->vval.v_string = r;
15126
15127 if (argvars[2].v_type != VAR_UNKNOWN)
15128 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015130 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015131 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015133 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015134 v.di_tv.v_type = VAR_STRING;
15135 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015136 idvar = get_tv_string_chk(&argvars[2]);
15137 if (idvar != NULL)
15138 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015139 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015140 }
15141}
15142#endif
15143
15144/*
15145 * "remote_expr()" function
15146 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015147 static void
15148f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015149 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015150 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151{
15152 rettv->v_type = VAR_STRING;
15153 rettv->vval.v_string = NULL;
15154#ifdef FEAT_CLIENTSERVER
15155 remote_common(argvars, rettv, TRUE);
15156#endif
15157}
15158
15159/*
15160 * "remote_foreground()" function
15161 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 static void
15163f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015164 typval_T *argvars UNUSED;
15165 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167#ifdef FEAT_CLIENTSERVER
15168# ifdef WIN32
15169 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 {
15171 char_u *server_name = get_tv_string_chk(&argvars[0]);
15172
15173 if (server_name != NULL)
15174 serverForeground(server_name);
15175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176# else
15177 /* Send a foreground() expression to the server. */
15178 argvars[1].v_type = VAR_STRING;
15179 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15180 argvars[2].v_type = VAR_UNKNOWN;
15181 remote_common(argvars, rettv, TRUE);
15182 vim_free(argvars[1].vval.v_string);
15183# endif
15184#endif
15185}
15186
Bram Moolenaar0d660222005-01-07 21:51:51 +000015187 static void
15188f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015189 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191{
15192#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194 char_u *s = NULL;
15195# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015196 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015198 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199
15200 if (check_restricted() || check_secure())
15201 {
15202 rettv->vval.v_number = -1;
15203 return;
15204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205 serverid = get_tv_string_chk(&argvars[0]);
15206 if (serverid == NULL)
15207 {
15208 rettv->vval.v_number = -1;
15209 return; /* type error; errmsg already given */
15210 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015212 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015213 if (n == 0)
15214 rettv->vval.v_number = -1;
15215 else
15216 {
15217 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15218 rettv->vval.v_number = (s != NULL);
15219 }
15220# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221 if (check_connection() == FAIL)
15222 return;
15223
15224 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015225 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015226# endif
15227
15228 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 char_u *retvar;
15231
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 v.di_tv.v_type = VAR_STRING;
15233 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015234 retvar = get_tv_string_chk(&argvars[1]);
15235 if (retvar != NULL)
15236 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015237 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015238 }
15239#else
15240 rettv->vval.v_number = -1;
15241#endif
15242}
15243
Bram Moolenaar0d660222005-01-07 21:51:51 +000015244 static void
15245f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015246 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015247 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015248{
15249 char_u *r = NULL;
15250
15251#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 char_u *serverid = get_tv_string_chk(&argvars[0]);
15253
15254 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255 {
15256# ifdef WIN32
15257 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015258 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015260 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015261 if (n != 0)
15262 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15263 if (r == NULL)
15264# else
15265 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267# endif
15268 EMSG(_("E277: Unable to read a server reply"));
15269 }
15270#endif
15271 rettv->v_type = VAR_STRING;
15272 rettv->vval.v_string = r;
15273}
15274
15275/*
15276 * "remote_send()" function
15277 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015278 static void
15279f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015281 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015282{
15283 rettv->v_type = VAR_STRING;
15284 rettv->vval.v_string = NULL;
15285#ifdef FEAT_CLIENTSERVER
15286 remote_common(argvars, rettv, FALSE);
15287#endif
15288}
15289
15290/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015291 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015292 */
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015297{
Bram Moolenaar33570922005-01-25 22:26:29 +000015298 list_T *l;
15299 listitem_T *item, *item2;
15300 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015301 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015302 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015303 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 dict_T *d;
15305 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015306 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015307
Bram Moolenaar8c711452005-01-14 21:53:12 +000015308 if (argvars[0].v_type == VAR_DICT)
15309 {
15310 if (argvars[2].v_type != VAR_UNKNOWN)
15311 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015312 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015313 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015315 key = get_tv_string_chk(&argvars[1]);
15316 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 di = dict_find(d, key, -1);
15319 if (di == NULL)
15320 EMSG2(_(e_dictkey), key);
15321 else
15322 {
15323 *rettv = di->di_tv;
15324 init_tv(&di->di_tv);
15325 dictitem_remove(d, di);
15326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015327 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015328 }
15329 }
15330 else if (argvars[0].v_type != VAR_LIST)
15331 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015332 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015333 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015335 int error = FALSE;
15336
15337 idx = get_tv_number_chk(&argvars[1], &error);
15338 if (error)
15339 ; /* type error: do nothing, errmsg already given */
15340 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015341 EMSGN(_(e_listidx), idx);
15342 else
15343 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 if (argvars[2].v_type == VAR_UNKNOWN)
15345 {
15346 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015347 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015348 *rettv = item->li_tv;
15349 vim_free(item);
15350 }
15351 else
15352 {
15353 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015354 end = get_tv_number_chk(&argvars[2], &error);
15355 if (error)
15356 ; /* type error: do nothing */
15357 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015358 EMSGN(_(e_listidx), end);
15359 else
15360 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015361 int cnt = 0;
15362
15363 for (li = item; li != NULL; li = li->li_next)
15364 {
15365 ++cnt;
15366 if (li == item2)
15367 break;
15368 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015369 if (li == NULL) /* didn't find "item2" after "item" */
15370 EMSG(_(e_invrange));
15371 else
15372 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015373 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015374 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015375 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015376 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015377 l->lv_first = item;
15378 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015379 item->li_prev = NULL;
15380 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015381 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015382 }
15383 }
15384 }
15385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015386 }
15387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388}
15389
15390/*
15391 * "rename({from}, {to})" function
15392 */
15393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015395 typval_T *argvars;
15396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 char_u buf[NUMBUFLEN];
15399
15400 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15404 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405}
15406
15407/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015408 * "repeat()" function
15409 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015410 static void
15411f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015412 typval_T *argvars;
15413 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015414{
15415 char_u *p;
15416 int n;
15417 int slen;
15418 int len;
15419 char_u *r;
15420 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015421
15422 n = get_tv_number(&argvars[1]);
15423 if (argvars[0].v_type == VAR_LIST)
15424 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015425 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015426 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015427 if (list_extend(rettv->vval.v_list,
15428 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015430 }
15431 else
15432 {
15433 p = get_tv_string(&argvars[0]);
15434 rettv->v_type = VAR_STRING;
15435 rettv->vval.v_string = NULL;
15436
15437 slen = (int)STRLEN(p);
15438 len = slen * n;
15439 if (len <= 0)
15440 return;
15441
15442 r = alloc(len + 1);
15443 if (r != NULL)
15444 {
15445 for (i = 0; i < n; i++)
15446 mch_memmove(r + i * slen, p, (size_t)slen);
15447 r[len] = NUL;
15448 }
15449
15450 rettv->vval.v_string = r;
15451 }
15452}
15453
15454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 * "resolve()" function
15456 */
15457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 typval_T *argvars;
15460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461{
15462 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015463#ifdef HAVE_READLINK
15464 char_u *buf = NULL;
15465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015467 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468#ifdef FEAT_SHORTCUT
15469 {
15470 char_u *v = NULL;
15471
15472 v = mch_resolve_shortcut(p);
15473 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 }
15478#else
15479# ifdef HAVE_READLINK
15480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 char_u *cpy;
15482 int len;
15483 char_u *remain = NULL;
15484 char_u *q;
15485 int is_relative_to_current = FALSE;
15486 int has_trailing_pathsep = FALSE;
15487 int limit = 100;
15488
15489 p = vim_strsave(p);
15490
15491 if (p[0] == '.' && (vim_ispathsep(p[1])
15492 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15493 is_relative_to_current = TRUE;
15494
15495 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015496 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015499 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501
15502 q = getnextcomp(p);
15503 if (*q != NUL)
15504 {
15505 /* Separate the first path component in "p", and keep the
15506 * remainder (beginning with the path separator). */
15507 remain = vim_strsave(q - 1);
15508 q[-1] = NUL;
15509 }
15510
Bram Moolenaard9462e32011-04-11 21:35:11 +020015511 buf = alloc(MAXPATHL + 1);
15512 if (buf == NULL)
15513 goto fail;
15514
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 for (;;)
15516 {
15517 for (;;)
15518 {
15519 len = readlink((char *)p, (char *)buf, MAXPATHL);
15520 if (len <= 0)
15521 break;
15522 buf[len] = NUL;
15523
15524 if (limit-- == 0)
15525 {
15526 vim_free(p);
15527 vim_free(remain);
15528 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 goto fail;
15531 }
15532
15533 /* Ensure that the result will have a trailing path separator
15534 * if the argument has one. */
15535 if (remain == NULL && has_trailing_pathsep)
15536 add_pathsep(buf);
15537
15538 /* Separate the first path component in the link value and
15539 * concatenate the remainders. */
15540 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15541 if (*q != NUL)
15542 {
15543 if (remain == NULL)
15544 remain = vim_strsave(q - 1);
15545 else
15546 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015547 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 if (cpy != NULL)
15549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 vim_free(remain);
15551 remain = cpy;
15552 }
15553 }
15554 q[-1] = NUL;
15555 }
15556
15557 q = gettail(p);
15558 if (q > p && *q == NUL)
15559 {
15560 /* Ignore trailing path separator. */
15561 q[-1] = NUL;
15562 q = gettail(p);
15563 }
15564 if (q > p && !mch_isFullName(buf))
15565 {
15566 /* symlink is relative to directory of argument */
15567 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15568 if (cpy != NULL)
15569 {
15570 STRCPY(cpy, p);
15571 STRCPY(gettail(cpy), buf);
15572 vim_free(p);
15573 p = cpy;
15574 }
15575 }
15576 else
15577 {
15578 vim_free(p);
15579 p = vim_strsave(buf);
15580 }
15581 }
15582
15583 if (remain == NULL)
15584 break;
15585
15586 /* Append the first path component of "remain" to "p". */
15587 q = getnextcomp(remain + 1);
15588 len = q - remain - (*q != NUL);
15589 cpy = vim_strnsave(p, STRLEN(p) + len);
15590 if (cpy != NULL)
15591 {
15592 STRNCAT(cpy, remain, len);
15593 vim_free(p);
15594 p = cpy;
15595 }
15596 /* Shorten "remain". */
15597 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015598 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 else
15600 {
15601 vim_free(remain);
15602 remain = NULL;
15603 }
15604 }
15605
15606 /* If the result is a relative path name, make it explicitly relative to
15607 * the current directory if and only if the argument had this form. */
15608 if (!vim_ispathsep(*p))
15609 {
15610 if (is_relative_to_current
15611 && *p != NUL
15612 && !(p[0] == '.'
15613 && (p[1] == NUL
15614 || vim_ispathsep(p[1])
15615 || (p[1] == '.'
15616 && (p[2] == NUL
15617 || vim_ispathsep(p[2]))))))
15618 {
15619 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015620 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 if (cpy != NULL)
15622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 vim_free(p);
15624 p = cpy;
15625 }
15626 }
15627 else if (!is_relative_to_current)
15628 {
15629 /* Strip leading "./". */
15630 q = p;
15631 while (q[0] == '.' && vim_ispathsep(q[1]))
15632 q += 2;
15633 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015634 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 }
15636 }
15637
15638 /* Ensure that the result will have no trailing path separator
15639 * if the argument had none. But keep "/" or "//". */
15640 if (!has_trailing_pathsep)
15641 {
15642 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015643 if (after_pathsep(p, q))
15644 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 }
15646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015647 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015650 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651# endif
15652#endif
15653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655
15656#ifdef HAVE_READLINK
15657fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015658 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661}
15662
15663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015664 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 */
15666 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015668 typval_T *argvars;
15669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670{
Bram Moolenaar33570922005-01-25 22:26:29 +000015671 list_T *l;
15672 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673
Bram Moolenaar0d660222005-01-07 21:51:51 +000015674 if (argvars[0].v_type != VAR_LIST)
15675 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015676 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015677 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015678 {
15679 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015680 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015681 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015682 while (li != NULL)
15683 {
15684 ni = li->li_prev;
15685 list_append(l, li);
15686 li = ni;
15687 }
15688 rettv->vval.v_list = l;
15689 rettv->v_type = VAR_LIST;
15690 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015691 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693}
15694
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015695#define SP_NOMOVE 0x01 /* don't move cursor */
15696#define SP_REPEAT 0x02 /* repeat to find outer pair */
15697#define SP_RETCOUNT 0x04 /* return matchcount */
15698#define SP_SETPCMARK 0x08 /* set previous context mark */
15699#define SP_START 0x10 /* accept match at start position */
15700#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15701#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015702
Bram Moolenaar33570922005-01-25 22:26:29 +000015703static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704
15705/*
15706 * Get flags for a search function.
15707 * Possibly sets "p_ws".
15708 * Returns BACKWARD, FORWARD or zero (for an error).
15709 */
15710 static int
15711get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015713 int *flagsp;
15714{
15715 int dir = FORWARD;
15716 char_u *flags;
15717 char_u nbuf[NUMBUFLEN];
15718 int mask;
15719
15720 if (varp->v_type != VAR_UNKNOWN)
15721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015722 flags = get_tv_string_buf_chk(varp, nbuf);
15723 if (flags == NULL)
15724 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 while (*flags != NUL)
15726 {
15727 switch (*flags)
15728 {
15729 case 'b': dir = BACKWARD; break;
15730 case 'w': p_ws = TRUE; break;
15731 case 'W': p_ws = FALSE; break;
15732 default: mask = 0;
15733 if (flagsp != NULL)
15734 switch (*flags)
15735 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015736 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015737 case 'e': mask = SP_END; break;
15738 case 'm': mask = SP_RETCOUNT; break;
15739 case 'n': mask = SP_NOMOVE; break;
15740 case 'p': mask = SP_SUBPAT; break;
15741 case 'r': mask = SP_REPEAT; break;
15742 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 }
15744 if (mask == 0)
15745 {
15746 EMSG2(_(e_invarg2), flags);
15747 dir = 0;
15748 }
15749 else
15750 *flagsp |= mask;
15751 }
15752 if (dir == 0)
15753 break;
15754 ++flags;
15755 }
15756 }
15757 return dir;
15758}
15759
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015761 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015763 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015764search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015765 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015766 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015767 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015769 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 char_u *pat;
15771 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015772 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 int save_p_ws = p_ws;
15774 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015775 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015776 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015777 proftime_T tm;
15778#ifdef FEAT_RELTIME
15779 long time_limit = 0;
15780#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015781 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015782 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015785 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015786 if (dir == 0)
15787 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015788 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015789 if (flags & SP_START)
15790 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015791 if (flags & SP_END)
15792 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015793
Bram Moolenaar76929292008-01-06 19:07:36 +000015794 /* Optional arguments: line number to stop searching and timeout. */
15795 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015796 {
15797 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15798 if (lnum_stop < 0)
15799 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015800#ifdef FEAT_RELTIME
15801 if (argvars[3].v_type != VAR_UNKNOWN)
15802 {
15803 time_limit = get_tv_number_chk(&argvars[3], NULL);
15804 if (time_limit < 0)
15805 goto theend;
15806 }
15807#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015808 }
15809
Bram Moolenaar76929292008-01-06 19:07:36 +000015810#ifdef FEAT_RELTIME
15811 /* Set the time limit, if there is one. */
15812 profile_setlimit(time_limit, &tm);
15813#endif
15814
Bram Moolenaar231334e2005-07-25 20:46:57 +000015815 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015816 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015817 * Check to make sure only those flags are set.
15818 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15819 * flags cannot be set. Check for that condition also.
15820 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015821 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015822 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015824 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015825 goto theend;
15826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015828 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015829 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015830 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015831 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015833 if (flags & SP_SUBPAT)
15834 retval = subpatnum;
15835 else
15836 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015837 if (flags & SP_SETPCMARK)
15838 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015840 if (match_pos != NULL)
15841 {
15842 /* Store the match cursor position */
15843 match_pos->lnum = pos.lnum;
15844 match_pos->col = pos.col + 1;
15845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 /* "/$" will put the cursor after the end of the line, may need to
15847 * correct that here */
15848 check_cursor();
15849 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015850
15851 /* If 'n' flag is used: restore cursor position. */
15852 if (flags & SP_NOMOVE)
15853 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015854 else
15855 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015856theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015858
15859 return retval;
15860}
15861
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015862#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015863
15864/*
15865 * round() is not in C90, use ceil() or floor() instead.
15866 */
15867 float_T
15868vim_round(f)
15869 float_T f;
15870{
15871 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15872}
15873
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015874/*
15875 * "round({float})" function
15876 */
15877 static void
15878f_round(argvars, rettv)
15879 typval_T *argvars;
15880 typval_T *rettv;
15881{
15882 float_T f;
15883
15884 rettv->v_type = VAR_FLOAT;
15885 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015886 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015887 else
15888 rettv->vval.v_float = 0.0;
15889}
15890#endif
15891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015892/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015893 * "screenattr()" function
15894 */
15895 static void
15896f_screenattr(argvars, rettv)
15897 typval_T *argvars UNUSED;
15898 typval_T *rettv;
15899{
15900 int row;
15901 int col;
15902 int c;
15903
15904 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15905 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15906 if (row < 0 || row >= screen_Rows
15907 || col < 0 || col >= screen_Columns)
15908 c = -1;
15909 else
15910 c = ScreenAttrs[LineOffset[row] + col];
15911 rettv->vval.v_number = c;
15912}
15913
15914/*
15915 * "screenchar()" function
15916 */
15917 static void
15918f_screenchar(argvars, rettv)
15919 typval_T *argvars UNUSED;
15920 typval_T *rettv;
15921{
15922 int row;
15923 int col;
15924 int off;
15925 int c;
15926
15927 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15928 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15929 if (row < 0 || row >= screen_Rows
15930 || col < 0 || col >= screen_Columns)
15931 c = -1;
15932 else
15933 {
15934 off = LineOffset[row] + col;
15935#ifdef FEAT_MBYTE
15936 if (enc_utf8 && ScreenLinesUC[off] != 0)
15937 c = ScreenLinesUC[off];
15938 else
15939#endif
15940 c = ScreenLines[off];
15941 }
15942 rettv->vval.v_number = c;
15943}
15944
15945/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015946 * "screencol()" function
15947 *
15948 * First column is 1 to be consistent with virtcol().
15949 */
15950 static void
15951f_screencol(argvars, rettv)
15952 typval_T *argvars UNUSED;
15953 typval_T *rettv;
15954{
15955 rettv->vval.v_number = screen_screencol() + 1;
15956}
15957
15958/*
15959 * "screenrow()" function
15960 */
15961 static void
15962f_screenrow(argvars, rettv)
15963 typval_T *argvars UNUSED;
15964 typval_T *rettv;
15965{
15966 rettv->vval.v_number = screen_screenrow() + 1;
15967}
15968
15969/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015970 * "search()" function
15971 */
15972 static void
15973f_search(argvars, rettv)
15974 typval_T *argvars;
15975 typval_T *rettv;
15976{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015977 int flags = 0;
15978
15979 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980}
15981
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015983 * "searchdecl()" function
15984 */
15985 static void
15986f_searchdecl(argvars, rettv)
15987 typval_T *argvars;
15988 typval_T *rettv;
15989{
15990 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015991 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015992 int error = FALSE;
15993 char_u *name;
15994
15995 rettv->vval.v_number = 1; /* default: FAIL */
15996
15997 name = get_tv_string_chk(&argvars[0]);
15998 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015999 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016000 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016001 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16002 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16003 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016004 if (!error && name != NULL)
16005 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016006 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016007}
16008
16009/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016010 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016012 static int
16013searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016014 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016015 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016{
16017 char_u *spat, *mpat, *epat;
16018 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 int dir;
16021 int flags = 0;
16022 char_u nbuf1[NUMBUFLEN];
16023 char_u nbuf2[NUMBUFLEN];
16024 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016025 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016026 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016027 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 spat = get_tv_string_chk(&argvars[0]);
16031 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16032 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16033 if (spat == NULL || mpat == NULL || epat == NULL)
16034 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 /* Handle the optional fourth argument: flags */
16037 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016038 if (dir == 0)
16039 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016040
16041 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016042 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16043 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016044 if ((flags & (SP_END | SP_SUBPAT)) != 0
16045 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016046 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016047 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016048 goto theend;
16049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016051 /* Using 'r' implies 'W', otherwise it doesn't work. */
16052 if (flags & SP_REPEAT)
16053 p_ws = FALSE;
16054
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016055 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016056 if (argvars[3].v_type == VAR_UNKNOWN
16057 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 skip = (char_u *)"";
16059 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016061 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016062 if (argvars[5].v_type != VAR_UNKNOWN)
16063 {
16064 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16065 if (lnum_stop < 0)
16066 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016067#ifdef FEAT_RELTIME
16068 if (argvars[6].v_type != VAR_UNKNOWN)
16069 {
16070 time_limit = get_tv_number_chk(&argvars[6], NULL);
16071 if (time_limit < 0)
16072 goto theend;
16073 }
16074#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 }
16076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016077 if (skip == NULL)
16078 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016080 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016081 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016082
16083theend:
16084 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016085
16086 return retval;
16087}
16088
16089/*
16090 * "searchpair()" function
16091 */
16092 static void
16093f_searchpair(argvars, rettv)
16094 typval_T *argvars;
16095 typval_T *rettv;
16096{
16097 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16098}
16099
16100/*
16101 * "searchpairpos()" function
16102 */
16103 static void
16104f_searchpairpos(argvars, rettv)
16105 typval_T *argvars;
16106 typval_T *rettv;
16107{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016108 pos_T match_pos;
16109 int lnum = 0;
16110 int col = 0;
16111
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016112 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016113 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016114
16115 if (searchpair_cmn(argvars, &match_pos) > 0)
16116 {
16117 lnum = match_pos.lnum;
16118 col = match_pos.col;
16119 }
16120
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016121 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16122 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016123}
16124
16125/*
16126 * Search for a start/middle/end thing.
16127 * Used by searchpair(), see its documentation for the details.
16128 * Returns 0 or -1 for no match,
16129 */
16130 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016131do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16132 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016133 char_u *spat; /* start pattern */
16134 char_u *mpat; /* middle pattern */
16135 char_u *epat; /* end pattern */
16136 int dir; /* BACKWARD or FORWARD */
16137 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016138 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016141 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016142{
16143 char_u *save_cpo;
16144 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16145 long retval = 0;
16146 pos_T pos;
16147 pos_T firstpos;
16148 pos_T foundpos;
16149 pos_T save_cursor;
16150 pos_T save_pos;
16151 int n;
16152 int r;
16153 int nest = 1;
16154 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016155 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016156 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016157
16158 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16159 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016160 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016161
Bram Moolenaar76929292008-01-06 19:07:36 +000016162#ifdef FEAT_RELTIME
16163 /* Set the time limit, if there is one. */
16164 profile_setlimit(time_limit, &tm);
16165#endif
16166
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016167 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16168 * start/middle/end (pat3, for the top pair). */
16169 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16170 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16171 if (pat2 == NULL || pat3 == NULL)
16172 goto theend;
16173 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16174 if (*mpat == NUL)
16175 STRCPY(pat3, pat2);
16176 else
16177 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16178 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016179 if (flags & SP_START)
16180 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016181
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 save_cursor = curwin->w_cursor;
16183 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016184 clearpos(&firstpos);
16185 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 pat = pat3;
16187 for (;;)
16188 {
16189 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016190 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16192 /* didn't find it or found the first match again: FAIL */
16193 break;
16194
16195 if (firstpos.lnum == 0)
16196 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016197 if (equalpos(pos, foundpos))
16198 {
16199 /* Found the same position again. Can happen with a pattern that
16200 * has "\zs" at the end and searching backwards. Advance one
16201 * character and try again. */
16202 if (dir == BACKWARD)
16203 decl(&pos);
16204 else
16205 incl(&pos);
16206 }
16207 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016209 /* clear the start flag to avoid getting stuck here */
16210 options &= ~SEARCH_START;
16211
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 /* If the skip pattern matches, ignore this match. */
16213 if (*skip != NUL)
16214 {
16215 save_pos = curwin->w_cursor;
16216 curwin->w_cursor = pos;
16217 r = eval_to_bool(skip, &err, NULL, FALSE);
16218 curwin->w_cursor = save_pos;
16219 if (err)
16220 {
16221 /* Evaluating {skip} caused an error, break here. */
16222 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016223 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 break;
16225 }
16226 if (r)
16227 continue;
16228 }
16229
16230 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16231 {
16232 /* Found end when searching backwards or start when searching
16233 * forward: nested pair. */
16234 ++nest;
16235 pat = pat2; /* nested, don't search for middle */
16236 }
16237 else
16238 {
16239 /* Found end when searching forward or start when searching
16240 * backward: end of (nested) pair; or found middle in outer pair. */
16241 if (--nest == 1)
16242 pat = pat3; /* outer level, search for middle */
16243 }
16244
16245 if (nest == 0)
16246 {
16247 /* Found the match: return matchcount or line number. */
16248 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016249 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016251 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016252 if (flags & SP_SETPCMARK)
16253 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 curwin->w_cursor = pos;
16255 if (!(flags & SP_REPEAT))
16256 break;
16257 nest = 1; /* search for next unmatched */
16258 }
16259 }
16260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016261 if (match_pos != NULL)
16262 {
16263 /* Store the match cursor position */
16264 match_pos->lnum = curwin->w_cursor.lnum;
16265 match_pos->col = curwin->w_cursor.col + 1;
16266 }
16267
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016269 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 curwin->w_cursor = save_cursor;
16271
16272theend:
16273 vim_free(pat2);
16274 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016275 if (p_cpo == empty_option)
16276 p_cpo = save_cpo;
16277 else
16278 /* Darn, evaluating the {skip} expression changed the value. */
16279 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016280
16281 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282}
16283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016284/*
16285 * "searchpos()" function
16286 */
16287 static void
16288f_searchpos(argvars, rettv)
16289 typval_T *argvars;
16290 typval_T *rettv;
16291{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016292 pos_T match_pos;
16293 int lnum = 0;
16294 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016295 int n;
16296 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016297
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016298 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016299 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016300
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016301 n = search_cmn(argvars, &match_pos, &flags);
16302 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016303 {
16304 lnum = match_pos.lnum;
16305 col = match_pos.col;
16306 }
16307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016308 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16309 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016310 if (flags & SP_SUBPAT)
16311 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016312}
16313
16314
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315 static void
16316f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016317 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320#ifdef FEAT_CLIENTSERVER
16321 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016322 char_u *server = get_tv_string_chk(&argvars[0]);
16323 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016326 if (server == NULL || reply == NULL)
16327 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 if (check_restricted() || check_secure())
16329 return;
16330# ifdef FEAT_X11
16331 if (check_connection() == FAIL)
16332 return;
16333# endif
16334
16335 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 EMSG(_("E258: Unable to send to client"));
16338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 rettv->vval.v_number = 0;
16341#else
16342 rettv->vval.v_number = -1;
16343#endif
16344}
16345
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 static void
16347f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016348 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016349 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350{
16351 char_u *r = NULL;
16352
16353#ifdef FEAT_CLIENTSERVER
16354# ifdef WIN32
16355 r = serverGetVimNames();
16356# else
16357 make_connection();
16358 if (X_DISPLAY != NULL)
16359 r = serverGetVimNames(X_DISPLAY);
16360# endif
16361#endif
16362 rettv->v_type = VAR_STRING;
16363 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
16366/*
16367 * "setbufvar()" function
16368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016372 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373{
16374 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 char_u nbuf[NUMBUFLEN];
16379
16380 if (check_restricted() || check_secure())
16381 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16383 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016384 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 varp = &argvars[2];
16386
16387 if (buf != NULL && varname != NULL && varp != NULL)
16388 {
16389 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391
16392 if (*varname == '&')
16393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 long numval;
16395 char_u *strval;
16396 int error = FALSE;
16397
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 numval = get_tv_number_chk(varp, &error);
16400 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016401 if (!error && strval != NULL)
16402 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 }
16404 else
16405 {
16406 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16407 if (bufvarname != NULL)
16408 {
16409 STRCPY(bufvarname, "b:");
16410 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016411 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412 vim_free(bufvarname);
16413 }
16414 }
16415
16416 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419}
16420
16421/*
16422 * "setcmdpos()" function
16423 */
16424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 typval_T *argvars;
16427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 int pos = (int)get_tv_number(&argvars[0]) - 1;
16430
16431 if (pos >= 0)
16432 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433}
16434
16435/*
16436 * "setline()" function
16437 */
16438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016439f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016440 typval_T *argvars;
16441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016442{
16443 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016444 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016445 list_T *l = NULL;
16446 listitem_T *li = NULL;
16447 long added = 0;
16448 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016450 lnum = get_tv_lnum(&argvars[0]);
16451 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016453 l = argvars[1].vval.v_list;
16454 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016456 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016458
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016459 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016460 for (;;)
16461 {
16462 if (l != NULL)
16463 {
16464 /* list argument, get next string */
16465 if (li == NULL)
16466 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016467 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016468 li = li->li_next;
16469 }
16470
16471 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016472 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016473 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016474
16475 /* When coming here from Insert mode, sync undo, so that this can be
16476 * undone separately from what was previously inserted. */
16477 if (u_sync_once == 2)
16478 {
16479 u_sync_once = 1; /* notify that u_sync() was called */
16480 u_sync(TRUE);
16481 }
16482
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016483 if (lnum <= curbuf->b_ml.ml_line_count)
16484 {
16485 /* existing line, replace it */
16486 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16487 {
16488 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016489 if (lnum == curwin->w_cursor.lnum)
16490 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016491 rettv->vval.v_number = 0; /* OK */
16492 }
16493 }
16494 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16495 {
16496 /* lnum is one past the last line, append the line */
16497 ++added;
16498 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16499 rettv->vval.v_number = 0; /* OK */
16500 }
16501
16502 if (l == NULL) /* only one string argument */
16503 break;
16504 ++lnum;
16505 }
16506
16507 if (added > 0)
16508 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509}
16510
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016511static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16512
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016514 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016515 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016516 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016517set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016518 win_T *wp UNUSED;
16519 typval_T *list_arg UNUSED;
16520 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016521 typval_T *rettv;
16522{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016523#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016524 char_u *act;
16525 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016526#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016527
Bram Moolenaar2641f772005-03-25 21:58:17 +000016528 rettv->vval.v_number = -1;
16529
16530#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016531 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016532 EMSG(_(e_listreq));
16533 else
16534 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016535 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016536
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016537 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016538 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016539 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016540 if (act == NULL)
16541 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016542 if (*act == 'a' || *act == 'r')
16543 action = *act;
16544 }
16545
Bram Moolenaar81484f42012-12-05 15:16:47 +010016546 if (l != NULL && set_errorlist(wp, l, action,
16547 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016548 rettv->vval.v_number = 0;
16549 }
16550#endif
16551}
16552
16553/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016554 * "setloclist()" function
16555 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016556 static void
16557f_setloclist(argvars, rettv)
16558 typval_T *argvars;
16559 typval_T *rettv;
16560{
16561 win_T *win;
16562
16563 rettv->vval.v_number = -1;
16564
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016565 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016566 if (win != NULL)
16567 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16568}
16569
16570/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016571 * "setmatches()" function
16572 */
16573 static void
16574f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016575 typval_T *argvars UNUSED;
16576 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016577{
16578#ifdef FEAT_SEARCH_EXTRA
16579 list_T *l;
16580 listitem_T *li;
16581 dict_T *d;
16582
16583 rettv->vval.v_number = -1;
16584 if (argvars[0].v_type != VAR_LIST)
16585 {
16586 EMSG(_(e_listreq));
16587 return;
16588 }
16589 if ((l = argvars[0].vval.v_list) != NULL)
16590 {
16591
16592 /* To some extent make sure that we are dealing with a list from
16593 * "getmatches()". */
16594 li = l->lv_first;
16595 while (li != NULL)
16596 {
16597 if (li->li_tv.v_type != VAR_DICT
16598 || (d = li->li_tv.vval.v_dict) == NULL)
16599 {
16600 EMSG(_(e_invarg));
16601 return;
16602 }
16603 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16604 && dict_find(d, (char_u *)"pattern", -1) != NULL
16605 && dict_find(d, (char_u *)"priority", -1) != NULL
16606 && dict_find(d, (char_u *)"id", -1) != NULL))
16607 {
16608 EMSG(_(e_invarg));
16609 return;
16610 }
16611 li = li->li_next;
16612 }
16613
16614 clear_matches(curwin);
16615 li = l->lv_first;
16616 while (li != NULL)
16617 {
16618 d = li->li_tv.vval.v_dict;
16619 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16620 get_dict_string(d, (char_u *)"pattern", FALSE),
16621 (int)get_dict_number(d, (char_u *)"priority"),
16622 (int)get_dict_number(d, (char_u *)"id"));
16623 li = li->li_next;
16624 }
16625 rettv->vval.v_number = 0;
16626 }
16627#endif
16628}
16629
16630/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016631 * "setpos()" function
16632 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016633 static void
16634f_setpos(argvars, rettv)
16635 typval_T *argvars;
16636 typval_T *rettv;
16637{
16638 pos_T pos;
16639 int fnum;
16640 char_u *name;
16641
Bram Moolenaar08250432008-02-13 11:42:46 +000016642 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016643 name = get_tv_string_chk(argvars);
16644 if (name != NULL)
16645 {
16646 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16647 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016648 if (--pos.col < 0)
16649 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016650 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016651 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016652 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016653 if (fnum == curbuf->b_fnum)
16654 {
16655 curwin->w_cursor = pos;
16656 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016657 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016658 }
16659 else
16660 EMSG(_(e_invarg));
16661 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016662 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16663 {
16664 /* set mark */
16665 if (setmark_pos(name[1], &pos, fnum) == OK)
16666 rettv->vval.v_number = 0;
16667 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016668 else
16669 EMSG(_(e_invarg));
16670 }
16671 }
16672}
16673
16674/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016675 * "setqflist()" function
16676 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016677 static void
16678f_setqflist(argvars, rettv)
16679 typval_T *argvars;
16680 typval_T *rettv;
16681{
16682 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16683}
16684
16685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 * "setreg()" function
16687 */
16688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016689f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016690 typval_T *argvars;
16691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692{
16693 int regname;
16694 char_u *strregname;
16695 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 int append;
16698 char_u yank_type;
16699 long block_len;
16700
16701 block_len = -1;
16702 yank_type = MAUTO;
16703 append = FALSE;
16704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016705 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016706 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 if (strregname == NULL)
16709 return; /* type error; errmsg already given */
16710 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 if (regname == 0 || regname == '@')
16712 regname = '"';
16713 else if (regname == '=')
16714 return;
16715
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016718 stropt = get_tv_string_chk(&argvars[2]);
16719 if (stropt == NULL)
16720 return; /* type error */
16721 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 switch (*stropt)
16723 {
16724 case 'a': case 'A': /* append */
16725 append = TRUE;
16726 break;
16727 case 'v': case 'c': /* character-wise selection */
16728 yank_type = MCHAR;
16729 break;
16730 case 'V': case 'l': /* line-wise selection */
16731 yank_type = MLINE;
16732 break;
16733#ifdef FEAT_VISUAL
16734 case 'b': case Ctrl_V: /* block-wise selection */
16735 yank_type = MBLOCK;
16736 if (VIM_ISDIGIT(stropt[1]))
16737 {
16738 ++stropt;
16739 block_len = getdigits(&stropt) - 1;
16740 --stropt;
16741 }
16742 break;
16743#endif
16744 }
16745 }
16746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016747 strval = get_tv_string_chk(&argvars[1]);
16748 if (strval != NULL)
16749 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016752}
16753
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016754/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016755 * "settabvar()" function
16756 */
16757 static void
16758f_settabvar(argvars, rettv)
16759 typval_T *argvars;
16760 typval_T *rettv;
16761{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016762#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016763 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016764 tabpage_T *tp;
16765#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016766 char_u *varname, *tabvarname;
16767 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016768
16769 rettv->vval.v_number = 0;
16770
16771 if (check_restricted() || check_secure())
16772 return;
16773
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016774#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016775 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016776#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016777 varname = get_tv_string_chk(&argvars[1]);
16778 varp = &argvars[2];
16779
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016780 if (varname != NULL && varp != NULL
16781#ifdef FEAT_WINDOWS
16782 && tp != NULL
16783#endif
16784 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016785 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016786#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016787 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016788 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016789#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016790
16791 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16792 if (tabvarname != NULL)
16793 {
16794 STRCPY(tabvarname, "t:");
16795 STRCPY(tabvarname + 2, varname);
16796 set_var(tabvarname, varp, TRUE);
16797 vim_free(tabvarname);
16798 }
16799
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016800#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016801 /* Restore current tabpage */
16802 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016803 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016804#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016805 }
16806}
16807
16808/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016809 * "settabwinvar()" function
16810 */
16811 static void
16812f_settabwinvar(argvars, rettv)
16813 typval_T *argvars;
16814 typval_T *rettv;
16815{
16816 setwinvar(argvars, rettv, 1);
16817}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818
16819/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016820 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016823f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016824 typval_T *argvars;
16825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016827 setwinvar(argvars, rettv, 0);
16828}
16829
16830/*
16831 * "setwinvar()" and "settabwinvar()" functions
16832 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016833
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016834 static void
16835setwinvar(argvars, rettv, off)
16836 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016837 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016838 int off;
16839{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840 win_T *win;
16841#ifdef FEAT_WINDOWS
16842 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016843 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844#endif
16845 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016846 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016848 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849
16850 if (check_restricted() || check_secure())
16851 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016852
16853#ifdef FEAT_WINDOWS
16854 if (off == 1)
16855 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16856 else
16857 tp = curtab;
16858#endif
16859 win = find_win_by_nr(&argvars[off], tp);
16860 varname = get_tv_string_chk(&argvars[off + 1]);
16861 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
16863 if (win != NULL && varname != NULL && varp != NULL)
16864 {
16865#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016866 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868#endif
16869
16870 if (*varname == '&')
16871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016872 long numval;
16873 char_u *strval;
16874 int error = FALSE;
16875
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 numval = get_tv_number_chk(varp, &error);
16878 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 if (!error && strval != NULL)
16880 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 }
16882 else
16883 {
16884 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16885 if (winvarname != NULL)
16886 {
16887 STRCPY(winvarname, "w:");
16888 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016889 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890 vim_free(winvarname);
16891 }
16892 }
16893
16894#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016895 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896#endif
16897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898}
16899
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016900#ifdef FEAT_CRYPT
16901/*
16902 * "sha256({string})" function
16903 */
16904 static void
16905f_sha256(argvars, rettv)
16906 typval_T *argvars;
16907 typval_T *rettv;
16908{
16909 char_u *p;
16910
16911 p = get_tv_string(&argvars[0]);
16912 rettv->vval.v_string = vim_strsave(
16913 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16914 rettv->v_type = VAR_STRING;
16915}
16916#endif /* FEAT_CRYPT */
16917
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016919 * "shellescape({string})" function
16920 */
16921 static void
16922f_shellescape(argvars, rettv)
16923 typval_T *argvars;
16924 typval_T *rettv;
16925{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016926 rettv->vval.v_string = vim_strsave_shellescape(
16927 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016928 rettv->v_type = VAR_STRING;
16929}
16930
16931/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016932 * shiftwidth() function
16933 */
16934 static void
16935f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016936 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016937 typval_T *rettv;
16938{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016939 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016940}
16941
16942/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 */
16945 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016947 typval_T *argvars;
16948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 p = get_tv_string(&argvars[0]);
16953 rettv->vval.v_string = vim_strsave(p);
16954 simplify_filename(rettv->vval.v_string); /* simplify in place */
16955 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956}
16957
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016958#ifdef FEAT_FLOAT
16959/*
16960 * "sin()" function
16961 */
16962 static void
16963f_sin(argvars, rettv)
16964 typval_T *argvars;
16965 typval_T *rettv;
16966{
16967 float_T f;
16968
16969 rettv->v_type = VAR_FLOAT;
16970 if (get_float_arg(argvars, &f) == OK)
16971 rettv->vval.v_float = sin(f);
16972 else
16973 rettv->vval.v_float = 0.0;
16974}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016975
16976/*
16977 * "sinh()" function
16978 */
16979 static void
16980f_sinh(argvars, rettv)
16981 typval_T *argvars;
16982 typval_T *rettv;
16983{
16984 float_T f;
16985
16986 rettv->v_type = VAR_FLOAT;
16987 if (get_float_arg(argvars, &f) == OK)
16988 rettv->vval.v_float = sinh(f);
16989 else
16990 rettv->vval.v_float = 0.0;
16991}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016992#endif
16993
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994static int
16995#ifdef __BORLANDC__
16996 _RTLENTRYF
16997#endif
16998 item_compare __ARGS((const void *s1, const void *s2));
16999static int
17000#ifdef __BORLANDC__
17001 _RTLENTRYF
17002#endif
17003 item_compare2 __ARGS((const void *s1, const void *s2));
17004
17005static int item_compare_ic;
17006static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017007static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009#define ITEM_COMPARE_FAIL 999
17010
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 static int
17015#ifdef __BORLANDC__
17016_RTLENTRYF
17017#endif
17018item_compare(s1, s2)
17019 const void *s1;
17020 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022 char_u *p1, *p2;
17023 char_u *tofree1, *tofree2;
17024 int res;
17025 char_u numbuf1[NUMBUFLEN];
17026 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017028 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17029 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017030 if (p1 == NULL)
17031 p1 = (char_u *)"";
17032 if (p2 == NULL)
17033 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 if (item_compare_ic)
17035 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017037 res = STRCMP(p1, p2);
17038 vim_free(tofree1);
17039 vim_free(tofree2);
17040 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041}
17042
17043 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044#ifdef __BORLANDC__
17045_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047item_compare2(s1, s2)
17048 const void *s1;
17049 const void *s2;
17050{
17051 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017053 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 /* shortcut after failure in previous call; compare all items equal */
17057 if (item_compare_func_err)
17058 return 0;
17059
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017060 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17061 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017062 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17063 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064
17065 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017066 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017067 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17068 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069 clear_tv(&argv[0]);
17070 clear_tv(&argv[1]);
17071
17072 if (res == FAIL)
17073 res = ITEM_COMPARE_FAIL;
17074 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17076 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017077 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 clear_tv(&rettv);
17079 return res;
17080}
17081
17082/*
17083 * "sort({list})" function
17084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017085 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017087 typval_T *argvars;
17088 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089{
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 list_T *l;
17091 listitem_T *li;
17092 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 long len;
17094 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 if (argvars[0].v_type != VAR_LIST)
17097 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 else
17099 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017101 if (l == NULL || tv_check_lock(l->lv_lock,
17102 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103 return;
17104 rettv->vval.v_list = l;
17105 rettv->v_type = VAR_LIST;
17106 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108 len = list_len(l);
17109 if (len <= 1)
17110 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112 item_compare_ic = FALSE;
17113 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017114 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115 if (argvars[1].v_type != VAR_UNKNOWN)
17116 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017117 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120 else
17121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 int error = FALSE;
17123
17124 i = get_tv_number_chk(&argvars[1], &error);
17125 if (error)
17126 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017127 if (i == 1)
17128 item_compare_ic = TRUE;
17129 else
17130 item_compare_func = get_tv_string(&argvars[1]);
17131 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017132
17133 if (argvars[2].v_type != VAR_UNKNOWN)
17134 {
17135 /* optional third argument: {dict} */
17136 if (argvars[2].v_type != VAR_DICT)
17137 {
17138 EMSG(_(e_dictreq));
17139 return;
17140 }
17141 item_compare_selfdict = argvars[2].vval.v_dict;
17142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017146 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147 if (ptrs == NULL)
17148 return;
17149 i = 0;
17150 for (li = l->lv_first; li != NULL; li = li->li_next)
17151 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 /* test the compare function */
17155 if (item_compare_func != NULL
17156 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17157 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017158 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 {
17161 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163 item_compare_func == NULL ? item_compare : item_compare2);
17164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 if (!item_compare_func_err)
17166 {
17167 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017168 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 l->lv_len = 0;
17170 for (i = 0; i < len; ++i)
17171 list_append(l, ptrs[i]);
17172 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 }
17174
17175 vim_free(ptrs);
17176 }
17177}
17178
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017179/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017180 * "soundfold({word})" function
17181 */
17182 static void
17183f_soundfold(argvars, rettv)
17184 typval_T *argvars;
17185 typval_T *rettv;
17186{
17187 char_u *s;
17188
17189 rettv->v_type = VAR_STRING;
17190 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017191#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017192 rettv->vval.v_string = eval_soundfold(s);
17193#else
17194 rettv->vval.v_string = vim_strsave(s);
17195#endif
17196}
17197
17198/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017199 * "spellbadword()" function
17200 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017201 static void
17202f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017203 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017204 typval_T *rettv;
17205{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017206 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017207 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017208 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017209
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017210 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017211 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017212
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017213#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017214 if (argvars[0].v_type == VAR_UNKNOWN)
17215 {
17216 /* Find the start and length of the badly spelled word. */
17217 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17218 if (len != 0)
17219 word = ml_get_cursor();
17220 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017221 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017222 {
17223 char_u *str = get_tv_string_chk(&argvars[0]);
17224 int capcol = -1;
17225
17226 if (str != NULL)
17227 {
17228 /* Check the argument for spelling. */
17229 while (*str != NUL)
17230 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017231 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017232 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017233 {
17234 word = str;
17235 break;
17236 }
17237 str += len;
17238 }
17239 }
17240 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017241#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017243 list_append_string(rettv->vval.v_list, word, len);
17244 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017245 attr == HLF_SPB ? "bad" :
17246 attr == HLF_SPR ? "rare" :
17247 attr == HLF_SPL ? "local" :
17248 attr == HLF_SPC ? "caps" :
17249 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017250}
17251
17252/*
17253 * "spellsuggest()" function
17254 */
17255 static void
17256f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017257 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017258 typval_T *rettv;
17259{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017260#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017261 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017262 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017263 int maxcount;
17264 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017265 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017266 listitem_T *li;
17267 int need_capital = FALSE;
17268#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017269
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017270 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017271 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017272
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017273#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017274 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017275 {
17276 str = get_tv_string(&argvars[0]);
17277 if (argvars[1].v_type != VAR_UNKNOWN)
17278 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017279 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017280 if (maxcount <= 0)
17281 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017282 if (argvars[2].v_type != VAR_UNKNOWN)
17283 {
17284 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17285 if (typeerr)
17286 return;
17287 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017288 }
17289 else
17290 maxcount = 25;
17291
Bram Moolenaar4770d092006-01-12 23:22:24 +000017292 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017293
17294 for (i = 0; i < ga.ga_len; ++i)
17295 {
17296 str = ((char_u **)ga.ga_data)[i];
17297
17298 li = listitem_alloc();
17299 if (li == NULL)
17300 vim_free(str);
17301 else
17302 {
17303 li->li_tv.v_type = VAR_STRING;
17304 li->li_tv.v_lock = 0;
17305 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017306 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017307 }
17308 }
17309 ga_clear(&ga);
17310 }
17311#endif
17312}
17313
Bram Moolenaar0d660222005-01-07 21:51:51 +000017314 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017315f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017316 typval_T *argvars;
17317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318{
17319 char_u *str;
17320 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017321 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017322 regmatch_T regmatch;
17323 char_u patbuf[NUMBUFLEN];
17324 char_u *save_cpo;
17325 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017326 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017327 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329
17330 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17331 save_cpo = p_cpo;
17332 p_cpo = (char_u *)"";
17333
17334 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017335 if (argvars[1].v_type != VAR_UNKNOWN)
17336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17338 if (pat == NULL)
17339 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017340 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017342 }
17343 if (pat == NULL || *pat == NUL)
17344 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017346 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 if (typeerr)
17349 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17352 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017355 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017356 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017357 if (*str == NUL)
17358 match = FALSE; /* empty item at the end */
17359 else
17360 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361 if (match)
17362 end = regmatch.startp[0];
17363 else
17364 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017365 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17366 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017367 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017368 if (list_append_string(rettv->vval.v_list, str,
17369 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017371 }
17372 if (!match)
17373 break;
17374 /* Advance to just after the match. */
17375 if (regmatch.endp[0] > str)
17376 col = 0;
17377 else
17378 {
17379 /* Don't get stuck at the same match. */
17380#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017381 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382#else
17383 col = 1;
17384#endif
17385 }
17386 str = regmatch.endp[0];
17387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388
Bram Moolenaar473de612013-06-08 18:19:48 +020017389 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017395#ifdef FEAT_FLOAT
17396/*
17397 * "sqrt()" function
17398 */
17399 static void
17400f_sqrt(argvars, rettv)
17401 typval_T *argvars;
17402 typval_T *rettv;
17403{
17404 float_T f;
17405
17406 rettv->v_type = VAR_FLOAT;
17407 if (get_float_arg(argvars, &f) == OK)
17408 rettv->vval.v_float = sqrt(f);
17409 else
17410 rettv->vval.v_float = 0.0;
17411}
17412
17413/*
17414 * "str2float()" function
17415 */
17416 static void
17417f_str2float(argvars, rettv)
17418 typval_T *argvars;
17419 typval_T *rettv;
17420{
17421 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17422
17423 if (*p == '+')
17424 p = skipwhite(p + 1);
17425 (void)string2float(p, &rettv->vval.v_float);
17426 rettv->v_type = VAR_FLOAT;
17427}
17428#endif
17429
Bram Moolenaar2c932302006-03-18 21:42:09 +000017430/*
17431 * "str2nr()" function
17432 */
17433 static void
17434f_str2nr(argvars, rettv)
17435 typval_T *argvars;
17436 typval_T *rettv;
17437{
17438 int base = 10;
17439 char_u *p;
17440 long n;
17441
17442 if (argvars[1].v_type != VAR_UNKNOWN)
17443 {
17444 base = get_tv_number(&argvars[1]);
17445 if (base != 8 && base != 10 && base != 16)
17446 {
17447 EMSG(_(e_invarg));
17448 return;
17449 }
17450 }
17451
17452 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017453 if (*p == '+')
17454 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017455 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17456 rettv->vval.v_number = n;
17457}
17458
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459#ifdef HAVE_STRFTIME
17460/*
17461 * "strftime({format}[, {time}])" function
17462 */
17463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017464f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 typval_T *argvars;
17466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467{
17468 char_u result_buf[256];
17469 struct tm *curtime;
17470 time_t seconds;
17471 char_u *p;
17472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017475 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017476 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 seconds = time(NULL);
17478 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 curtime = localtime(&seconds);
17481 /* MSVC returns NULL for an invalid value of seconds. */
17482 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017483 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 else
17485 {
17486# ifdef FEAT_MBYTE
17487 vimconv_T conv;
17488 char_u *enc;
17489
17490 conv.vc_type = CONV_NONE;
17491 enc = enc_locale();
17492 convert_setup(&conv, p_enc, enc);
17493 if (conv.vc_type != CONV_NONE)
17494 p = string_convert(&conv, p, NULL);
17495# endif
17496 if (p != NULL)
17497 (void)strftime((char *)result_buf, sizeof(result_buf),
17498 (char *)p, curtime);
17499 else
17500 result_buf[0] = NUL;
17501
17502# ifdef FEAT_MBYTE
17503 if (conv.vc_type != CONV_NONE)
17504 vim_free(p);
17505 convert_setup(&conv, enc, p_enc);
17506 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 else
17509# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017510 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511
17512# ifdef FEAT_MBYTE
17513 /* Release conversion descriptors */
17514 convert_setup(&conv, NULL, NULL);
17515 vim_free(enc);
17516# endif
17517 }
17518}
17519#endif
17520
17521/*
17522 * "stridx()" function
17523 */
17524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
17529 char_u buf[NUMBUFLEN];
17530 char_u *needle;
17531 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017532 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017534 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017536 needle = get_tv_string_chk(&argvars[1]);
17537 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017538 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017539 if (needle == NULL || haystack == NULL)
17540 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541
Bram Moolenaar33570922005-01-25 22:26:29 +000017542 if (argvars[2].v_type != VAR_UNKNOWN)
17543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 int error = FALSE;
17545
17546 start_idx = get_tv_number_chk(&argvars[2], &error);
17547 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017548 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017549 if (start_idx >= 0)
17550 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 }
17552
17553 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17554 if (pos != NULL)
17555 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556}
17557
17558/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017559 * "string()" function
17560 */
17561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017562f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017563 typval_T *argvars;
17564 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017565{
17566 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017567 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017569 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017570 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017571 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017572 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017573 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574}
17575
17576/*
17577 * "strlen()" function
17578 */
17579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 typval_T *argvars;
17582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017584 rettv->vval.v_number = (varnumber_T)(STRLEN(
17585 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586}
17587
17588/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017589 * "strchars()" function
17590 */
17591 static void
17592f_strchars(argvars, rettv)
17593 typval_T *argvars;
17594 typval_T *rettv;
17595{
17596 char_u *s = get_tv_string(&argvars[0]);
17597#ifdef FEAT_MBYTE
17598 varnumber_T len = 0;
17599
17600 while (*s != NUL)
17601 {
17602 mb_cptr2char_adv(&s);
17603 ++len;
17604 }
17605 rettv->vval.v_number = len;
17606#else
17607 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17608#endif
17609}
17610
17611/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017612 * "strdisplaywidth()" function
17613 */
17614 static void
17615f_strdisplaywidth(argvars, rettv)
17616 typval_T *argvars;
17617 typval_T *rettv;
17618{
17619 char_u *s = get_tv_string(&argvars[0]);
17620 int col = 0;
17621
17622 if (argvars[1].v_type != VAR_UNKNOWN)
17623 col = get_tv_number(&argvars[1]);
17624
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017625 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017626}
17627
17628/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017629 * "strwidth()" function
17630 */
17631 static void
17632f_strwidth(argvars, rettv)
17633 typval_T *argvars;
17634 typval_T *rettv;
17635{
17636 char_u *s = get_tv_string(&argvars[0]);
17637
17638 rettv->vval.v_number = (varnumber_T)(
17639#ifdef FEAT_MBYTE
17640 mb_string2cells(s, -1)
17641#else
17642 STRLEN(s)
17643#endif
17644 );
17645}
17646
17647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 * "strpart()" function
17649 */
17650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017652 typval_T *argvars;
17653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654{
17655 char_u *p;
17656 int n;
17657 int len;
17658 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017659 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 slen = (int)STRLEN(p);
17663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017664 n = get_tv_number_chk(&argvars[1], &error);
17665 if (error)
17666 len = 0;
17667 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 else
17670 len = slen - n; /* default len: all bytes that are available. */
17671
17672 /*
17673 * Only return the overlap between the specified part and the actual
17674 * string.
17675 */
17676 if (n < 0)
17677 {
17678 len += n;
17679 n = 0;
17680 }
17681 else if (n > slen)
17682 n = slen;
17683 if (len < 0)
17684 len = 0;
17685 else if (n + len > slen)
17686 len = slen - n;
17687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017688 rettv->v_type = VAR_STRING;
17689 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690}
17691
17692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693 * "strridx()" function
17694 */
17695 static void
17696f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 typval_T *argvars;
17698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699{
17700 char_u buf[NUMBUFLEN];
17701 char_u *needle;
17702 char_u *haystack;
17703 char_u *rest;
17704 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017705 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 needle = get_tv_string_chk(&argvars[1]);
17708 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709
17710 rettv->vval.v_number = -1;
17711 if (needle == NULL || haystack == NULL)
17712 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017713
17714 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017715 if (argvars[2].v_type != VAR_UNKNOWN)
17716 {
17717 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017718 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017719 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017721 }
17722 else
17723 end_idx = haystack_len;
17724
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017727 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017728 lastmatch = haystack + end_idx;
17729 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017730 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017731 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732 for (rest = haystack; *rest != '\0'; ++rest)
17733 {
17734 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017735 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017736 break;
17737 lastmatch = rest;
17738 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017739 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017740
17741 if (lastmatch == NULL)
17742 rettv->vval.v_number = -1;
17743 else
17744 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17745}
17746
17747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748 * "strtrans()" function
17749 */
17750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017751f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017752 typval_T *argvars;
17753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755 rettv->v_type = VAR_STRING;
17756 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
17759/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017760 * "submatch()" function
17761 */
17762 static void
17763f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 typval_T *argvars;
17765 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766{
17767 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 rettv->vval.v_string =
17769 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017770}
17771
17772/*
17773 * "substitute()" function
17774 */
17775 static void
17776f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779{
17780 char_u patbuf[NUMBUFLEN];
17781 char_u subbuf[NUMBUFLEN];
17782 char_u flagsbuf[NUMBUFLEN];
17783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017784 char_u *str = get_tv_string_chk(&argvars[0]);
17785 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17786 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17787 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17788
Bram Moolenaar0d660222005-01-07 21:51:51 +000017789 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017790 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17791 rettv->vval.v_string = NULL;
17792 else
17793 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017794}
17795
17796/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017797 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017800f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017801 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803{
17804 int id = 0;
17805#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017806 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 long col;
17808 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017809 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017811 lnum = get_tv_lnum(argvars); /* -1 on type error */
17812 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17813 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017815 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017816 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017817 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818#endif
17819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017820 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821}
17822
17823/*
17824 * "synIDattr(id, what [, mode])" function
17825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017827f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830{
17831 char_u *p = NULL;
17832#ifdef FEAT_SYN_HL
17833 int id;
17834 char_u *what;
17835 char_u *mode;
17836 char_u modebuf[NUMBUFLEN];
17837 int modec;
17838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 id = get_tv_number(&argvars[0]);
17840 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017841 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017845 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 modec = 0; /* replace invalid with current */
17847 }
17848 else
17849 {
17850#ifdef FEAT_GUI
17851 if (gui.in_use)
17852 modec = 'g';
17853 else
17854#endif
17855 if (t_colors > 1)
17856 modec = 'c';
17857 else
17858 modec = 't';
17859 }
17860
17861
17862 switch (TOLOWER_ASC(what[0]))
17863 {
17864 case 'b':
17865 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17866 p = highlight_color(id, what, modec);
17867 else /* bold */
17868 p = highlight_has_attr(id, HL_BOLD, modec);
17869 break;
17870
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017871 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 p = highlight_color(id, what, modec);
17873 break;
17874
17875 case 'i':
17876 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17877 p = highlight_has_attr(id, HL_INVERSE, modec);
17878 else /* italic */
17879 p = highlight_has_attr(id, HL_ITALIC, modec);
17880 break;
17881
17882 case 'n': /* name */
17883 p = get_highlight_name(NULL, id - 1);
17884 break;
17885
17886 case 'r': /* reverse */
17887 p = highlight_has_attr(id, HL_INVERSE, modec);
17888 break;
17889
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017890 case 's':
17891 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17892 p = highlight_color(id, what, modec);
17893 else /* standout */
17894 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 break;
17896
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017897 case 'u':
17898 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17899 /* underline */
17900 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17901 else
17902 /* undercurl */
17903 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 break;
17905 }
17906
17907 if (p != NULL)
17908 p = vim_strsave(p);
17909#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017910 rettv->v_type = VAR_STRING;
17911 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912}
17913
17914/*
17915 * "synIDtrans(id)" function
17916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017918f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921{
17922 int id;
17923
17924#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017925 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
17927 if (id > 0)
17928 id = syn_get_final_id(id);
17929 else
17930#endif
17931 id = 0;
17932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934}
17935
17936/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017937 * "synconcealed(lnum, col)" function
17938 */
17939 static void
17940f_synconcealed(argvars, rettv)
17941 typval_T *argvars UNUSED;
17942 typval_T *rettv;
17943{
17944#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17945 long lnum;
17946 long col;
17947 int syntax_flags = 0;
17948 int cchar;
17949 int matchid = 0;
17950 char_u str[NUMBUFLEN];
17951#endif
17952
17953 rettv->v_type = VAR_LIST;
17954 rettv->vval.v_list = NULL;
17955
17956#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17957 lnum = get_tv_lnum(argvars); /* -1 on type error */
17958 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17959
17960 vim_memset(str, NUL, sizeof(str));
17961
17962 if (rettv_list_alloc(rettv) != FAIL)
17963 {
17964 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17965 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17966 && curwin->w_p_cole > 0)
17967 {
17968 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17969 syntax_flags = get_syntax_info(&matchid);
17970
17971 /* get the conceal character */
17972 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17973 {
17974 cchar = syn_get_sub_char();
17975 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17976 cchar = lcs_conceal;
17977 if (cchar != NUL)
17978 {
17979# ifdef FEAT_MBYTE
17980 if (has_mbyte)
17981 (*mb_char2bytes)(cchar, str);
17982 else
17983# endif
17984 str[0] = cchar;
17985 }
17986 }
17987 }
17988
17989 list_append_number(rettv->vval.v_list,
17990 (syntax_flags & HL_CONCEAL) != 0);
17991 /* -1 to auto-determine strlen */
17992 list_append_string(rettv->vval.v_list, str, -1);
17993 list_append_number(rettv->vval.v_list, matchid);
17994 }
17995#endif
17996}
17997
17998/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017999 * "synstack(lnum, col)" function
18000 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018001 static void
18002f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018003 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018004 typval_T *rettv;
18005{
18006#ifdef FEAT_SYN_HL
18007 long lnum;
18008 long col;
18009 int i;
18010 int id;
18011#endif
18012
18013 rettv->v_type = VAR_LIST;
18014 rettv->vval.v_list = NULL;
18015
18016#ifdef FEAT_SYN_HL
18017 lnum = get_tv_lnum(argvars); /* -1 on type error */
18018 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18019
18020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018021 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018022 && rettv_list_alloc(rettv) != FAIL)
18023 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018024 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018025 for (i = 0; ; ++i)
18026 {
18027 id = syn_get_stack_item(i);
18028 if (id < 0)
18029 break;
18030 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18031 break;
18032 }
18033 }
18034#endif
18035}
18036
18037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 * "system()" function
18039 */
18040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018041f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 typval_T *argvars;
18043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018045 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018047 char_u *infile = NULL;
18048 char_u buf[NUMBUFLEN];
18049 int err = FALSE;
18050 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018052 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018053 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018055 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018056 {
18057 /*
18058 * Write the string to a temp file, to be used for input of the shell
18059 * command.
18060 */
18061 if ((infile = vim_tempname('i')) == NULL)
18062 {
18063 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018064 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018065 }
18066
18067 fd = mch_fopen((char *)infile, WRITEBIN);
18068 if (fd == NULL)
18069 {
18070 EMSG2(_(e_notopen), infile);
18071 goto done;
18072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018073 p = get_tv_string_buf_chk(&argvars[1], buf);
18074 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018075 {
18076 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018077 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018078 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018079 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18080 err = TRUE;
18081 if (fclose(fd) != 0)
18082 err = TRUE;
18083 if (err)
18084 {
18085 EMSG(_("E677: Error writing temp file"));
18086 goto done;
18087 }
18088 }
18089
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018090 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18091 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018092
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093#ifdef USE_CR
18094 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018095 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 {
18097 char_u *s;
18098
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018099 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 {
18101 if (*s == CAR)
18102 *s = NL;
18103 }
18104 }
18105#else
18106# ifdef USE_CRNL
18107 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018108 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 {
18110 char_u *s, *d;
18111
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018112 d = res;
18113 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114 {
18115 if (s[0] == CAR && s[1] == NL)
18116 ++s;
18117 *d++ = *s;
18118 }
18119 *d = NUL;
18120 }
18121# endif
18122#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018123
18124done:
18125 if (infile != NULL)
18126 {
18127 mch_remove(infile);
18128 vim_free(infile);
18129 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018130 rettv->v_type = VAR_STRING;
18131 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
18134/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018135 * "tabpagebuflist()" function
18136 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018137 static void
18138f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018139 typval_T *argvars UNUSED;
18140 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018141{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018142#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018143 tabpage_T *tp;
18144 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018145
18146 if (argvars[0].v_type == VAR_UNKNOWN)
18147 wp = firstwin;
18148 else
18149 {
18150 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18151 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018152 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018153 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018154 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018155 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018156 for (; wp != NULL; wp = wp->w_next)
18157 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018158 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018159 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018160 }
18161#endif
18162}
18163
18164
18165/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018166 * "tabpagenr()" function
18167 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018168 static void
18169f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018170 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018171 typval_T *rettv;
18172{
18173 int nr = 1;
18174#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018175 char_u *arg;
18176
18177 if (argvars[0].v_type != VAR_UNKNOWN)
18178 {
18179 arg = get_tv_string_chk(&argvars[0]);
18180 nr = 0;
18181 if (arg != NULL)
18182 {
18183 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018184 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018185 else
18186 EMSG2(_(e_invexpr2), arg);
18187 }
18188 }
18189 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018190 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018191#endif
18192 rettv->vval.v_number = nr;
18193}
18194
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018195
18196#ifdef FEAT_WINDOWS
18197static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18198
18199/*
18200 * Common code for tabpagewinnr() and winnr().
18201 */
18202 static int
18203get_winnr(tp, argvar)
18204 tabpage_T *tp;
18205 typval_T *argvar;
18206{
18207 win_T *twin;
18208 int nr = 1;
18209 win_T *wp;
18210 char_u *arg;
18211
18212 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18213 if (argvar->v_type != VAR_UNKNOWN)
18214 {
18215 arg = get_tv_string_chk(argvar);
18216 if (arg == NULL)
18217 nr = 0; /* type error; errmsg already given */
18218 else if (STRCMP(arg, "$") == 0)
18219 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18220 else if (STRCMP(arg, "#") == 0)
18221 {
18222 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18223 if (twin == NULL)
18224 nr = 0;
18225 }
18226 else
18227 {
18228 EMSG2(_(e_invexpr2), arg);
18229 nr = 0;
18230 }
18231 }
18232
18233 if (nr > 0)
18234 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18235 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018236 {
18237 if (wp == NULL)
18238 {
18239 /* didn't find it in this tabpage */
18240 nr = 0;
18241 break;
18242 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018243 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018244 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018245 return nr;
18246}
18247#endif
18248
18249/*
18250 * "tabpagewinnr()" function
18251 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018252 static void
18253f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018254 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018255 typval_T *rettv;
18256{
18257 int nr = 1;
18258#ifdef FEAT_WINDOWS
18259 tabpage_T *tp;
18260
18261 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18262 if (tp == NULL)
18263 nr = 0;
18264 else
18265 nr = get_winnr(tp, &argvars[1]);
18266#endif
18267 rettv->vval.v_number = nr;
18268}
18269
18270
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018271/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018272 * "tagfiles()" function
18273 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018274 static void
18275f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018276 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018277 typval_T *rettv;
18278{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018279 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018280 tagname_T tn;
18281 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018282
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018283 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018284 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018285 fname = alloc(MAXPATHL);
18286 if (fname == NULL)
18287 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018289 for (first = TRUE; ; first = FALSE)
18290 if (get_tagfname(&tn, first, fname) == FAIL
18291 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018292 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018293 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018294 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018295}
18296
18297/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018298 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018299 */
18300 static void
18301f_taglist(argvars, rettv)
18302 typval_T *argvars;
18303 typval_T *rettv;
18304{
18305 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018306
18307 tag_pattern = get_tv_string(&argvars[0]);
18308
18309 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018310 if (*tag_pattern == NUL)
18311 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018312
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018313 if (rettv_list_alloc(rettv) == OK)
18314 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018315}
18316
18317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 * "tempname()" function
18319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018321f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018322 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324{
18325 static int x = 'A';
18326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018327 rettv->v_type = VAR_STRING;
18328 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329
18330 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18331 * names. Skip 'I' and 'O', they are used for shell redirection. */
18332 do
18333 {
18334 if (x == 'Z')
18335 x = '0';
18336 else if (x == '9')
18337 x = 'A';
18338 else
18339 {
18340#ifdef EBCDIC
18341 if (x == 'I')
18342 x = 'J';
18343 else if (x == 'R')
18344 x = 'S';
18345 else
18346#endif
18347 ++x;
18348 }
18349 } while (x == 'I' || x == 'O');
18350}
18351
18352/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018353 * "test(list)" function: Just checking the walls...
18354 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018355 static void
18356f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018357 typval_T *argvars UNUSED;
18358 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018359{
18360 /* Used for unit testing. Change the code below to your liking. */
18361#if 0
18362 listitem_T *li;
18363 list_T *l;
18364 char_u *bad, *good;
18365
18366 if (argvars[0].v_type != VAR_LIST)
18367 return;
18368 l = argvars[0].vval.v_list;
18369 if (l == NULL)
18370 return;
18371 li = l->lv_first;
18372 if (li == NULL)
18373 return;
18374 bad = get_tv_string(&li->li_tv);
18375 li = li->li_next;
18376 if (li == NULL)
18377 return;
18378 good = get_tv_string(&li->li_tv);
18379 rettv->vval.v_number = test_edit_score(bad, good);
18380#endif
18381}
18382
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018383#ifdef FEAT_FLOAT
18384/*
18385 * "tan()" function
18386 */
18387 static void
18388f_tan(argvars, rettv)
18389 typval_T *argvars;
18390 typval_T *rettv;
18391{
18392 float_T f;
18393
18394 rettv->v_type = VAR_FLOAT;
18395 if (get_float_arg(argvars, &f) == OK)
18396 rettv->vval.v_float = tan(f);
18397 else
18398 rettv->vval.v_float = 0.0;
18399}
18400
18401/*
18402 * "tanh()" function
18403 */
18404 static void
18405f_tanh(argvars, rettv)
18406 typval_T *argvars;
18407 typval_T *rettv;
18408{
18409 float_T f;
18410
18411 rettv->v_type = VAR_FLOAT;
18412 if (get_float_arg(argvars, &f) == OK)
18413 rettv->vval.v_float = tanh(f);
18414 else
18415 rettv->vval.v_float = 0.0;
18416}
18417#endif
18418
Bram Moolenaard52d9742005-08-21 22:20:28 +000018419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018420 * "tolower(string)" function
18421 */
18422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018424 typval_T *argvars;
18425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426{
18427 char_u *p;
18428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 p = vim_strsave(get_tv_string(&argvars[0]));
18430 rettv->v_type = VAR_STRING;
18431 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432
18433 if (p != NULL)
18434 while (*p != NUL)
18435 {
18436#ifdef FEAT_MBYTE
18437 int l;
18438
18439 if (enc_utf8)
18440 {
18441 int c, lc;
18442
18443 c = utf_ptr2char(p);
18444 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018445 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 /* TODO: reallocate string when byte count changes. */
18447 if (utf_char2len(lc) == l)
18448 utf_char2bytes(lc, p);
18449 p += l;
18450 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018451 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 p += l; /* skip multi-byte character */
18453 else
18454#endif
18455 {
18456 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18457 ++p;
18458 }
18459 }
18460}
18461
18462/*
18463 * "toupper(string)" function
18464 */
18465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018466f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018467 typval_T *argvars;
18468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018470 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018471 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472}
18473
18474/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018475 * "tr(string, fromstr, tostr)" function
18476 */
18477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 typval_T *argvars;
18480 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018481{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018482 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018483 char_u *fromstr;
18484 char_u *tostr;
18485 char_u *p;
18486#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018487 int inlen;
18488 int fromlen;
18489 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018490 int idx;
18491 char_u *cpstr;
18492 int cplen;
18493 int first = TRUE;
18494#endif
18495 char_u buf[NUMBUFLEN];
18496 char_u buf2[NUMBUFLEN];
18497 garray_T ga;
18498
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018499 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018500 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18501 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018502
18503 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 rettv->v_type = VAR_STRING;
18505 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018506 if (fromstr == NULL || tostr == NULL)
18507 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018508 ga_init2(&ga, (int)sizeof(char), 80);
18509
18510#ifdef FEAT_MBYTE
18511 if (!has_mbyte)
18512#endif
18513 /* not multi-byte: fromstr and tostr must be the same length */
18514 if (STRLEN(fromstr) != STRLEN(tostr))
18515 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018516#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018517error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018518#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018519 EMSG2(_(e_invarg2), fromstr);
18520 ga_clear(&ga);
18521 return;
18522 }
18523
18524 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018525 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018526 {
18527#ifdef FEAT_MBYTE
18528 if (has_mbyte)
18529 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018530 inlen = (*mb_ptr2len)(in_str);
18531 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018532 cplen = inlen;
18533 idx = 0;
18534 for (p = fromstr; *p != NUL; p += fromlen)
18535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018536 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018537 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018538 {
18539 for (p = tostr; *p != NUL; p += tolen)
18540 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018541 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018542 if (idx-- == 0)
18543 {
18544 cplen = tolen;
18545 cpstr = p;
18546 break;
18547 }
18548 }
18549 if (*p == NUL) /* tostr is shorter than fromstr */
18550 goto error;
18551 break;
18552 }
18553 ++idx;
18554 }
18555
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018556 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018557 {
18558 /* Check that fromstr and tostr have the same number of
18559 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018560 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018561 first = FALSE;
18562 for (p = tostr; *p != NUL; p += tolen)
18563 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018564 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018565 --idx;
18566 }
18567 if (idx != 0)
18568 goto error;
18569 }
18570
18571 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018572 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018573 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018574
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018575 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018576 }
18577 else
18578#endif
18579 {
18580 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018581 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018582 if (p != NULL)
18583 ga_append(&ga, tostr[p - fromstr]);
18584 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018585 ga_append(&ga, *in_str);
18586 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018587 }
18588 }
18589
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018590 /* add a terminating NUL */
18591 ga_grow(&ga, 1);
18592 ga_append(&ga, NUL);
18593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018595}
18596
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018597#ifdef FEAT_FLOAT
18598/*
18599 * "trunc({float})" function
18600 */
18601 static void
18602f_trunc(argvars, rettv)
18603 typval_T *argvars;
18604 typval_T *rettv;
18605{
18606 float_T f;
18607
18608 rettv->v_type = VAR_FLOAT;
18609 if (get_float_arg(argvars, &f) == OK)
18610 /* trunc() is not in C90, use floor() or ceil() instead. */
18611 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18612 else
18613 rettv->vval.v_float = 0.0;
18614}
18615#endif
18616
Bram Moolenaar8299df92004-07-10 09:47:34 +000018617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 * "type(expr)" function
18619 */
18620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018622 typval_T *argvars;
18623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018625 int n;
18626
18627 switch (argvars[0].v_type)
18628 {
18629 case VAR_NUMBER: n = 0; break;
18630 case VAR_STRING: n = 1; break;
18631 case VAR_FUNC: n = 2; break;
18632 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018633 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018634#ifdef FEAT_FLOAT
18635 case VAR_FLOAT: n = 5; break;
18636#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018637 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18638 }
18639 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640}
18641
18642/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018643 * "undofile(name)" function
18644 */
18645 static void
18646f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018647 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018648 typval_T *rettv;
18649{
18650 rettv->v_type = VAR_STRING;
18651#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018652 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018653 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018654
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018655 if (*fname == NUL)
18656 {
18657 /* If there is no file name there will be no undo file. */
18658 rettv->vval.v_string = NULL;
18659 }
18660 else
18661 {
18662 char_u *ffname = FullName_save(fname, FALSE);
18663
18664 if (ffname != NULL)
18665 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18666 vim_free(ffname);
18667 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018668 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018669#else
18670 rettv->vval.v_string = NULL;
18671#endif
18672}
18673
18674/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018675 * "undotree()" function
18676 */
18677 static void
18678f_undotree(argvars, rettv)
18679 typval_T *argvars UNUSED;
18680 typval_T *rettv;
18681{
18682 if (rettv_dict_alloc(rettv) == OK)
18683 {
18684 dict_T *dict = rettv->vval.v_dict;
18685 list_T *list;
18686
Bram Moolenaar730cde92010-06-27 05:18:54 +020018687 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018688 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018689 dict_add_nr_str(dict, "save_last",
18690 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018691 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18692 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018693 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018694
18695 list = list_alloc();
18696 if (list != NULL)
18697 {
18698 u_eval_tree(curbuf->b_u_oldhead, list);
18699 dict_add_list(dict, "entries", list);
18700 }
18701 }
18702}
18703
18704/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018705 * "values(dict)" function
18706 */
18707 static void
18708f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018709 typval_T *argvars;
18710 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018711{
18712 dict_list(argvars, rettv, 1);
18713}
18714
18715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716 * "virtcol(string)" function
18717 */
18718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018719f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 typval_T *argvars;
18721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722{
18723 colnr_T vcol = 0;
18724 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018725 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018727 fp = var2fpos(&argvars[0], FALSE, &fnum);
18728 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18729 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 {
18731 getvvcol(curwin, fp, NULL, NULL, &vcol);
18732 ++vcol;
18733 }
18734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736}
18737
18738/*
18739 * "visualmode()" function
18740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018743 typval_T *argvars UNUSED;
18744 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745{
18746#ifdef FEAT_VISUAL
18747 char_u str[2];
18748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 str[0] = curbuf->b_visual_mode_eval;
18751 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753
18754 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018755 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757#endif
18758}
18759
18760/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018761 * "wildmenumode()" function
18762 */
18763 static void
18764f_wildmenumode(argvars, rettv)
18765 typval_T *argvars UNUSED;
18766 typval_T *rettv UNUSED;
18767{
18768#ifdef FEAT_WILDMENU
18769 if (wild_menu_showing)
18770 rettv->vval.v_number = 1;
18771#endif
18772}
18773
18774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 * "winbufnr(nr)" function
18776 */
18777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018779 typval_T *argvars;
18780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781{
18782 win_T *wp;
18783
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018784 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
18791/*
18792 * "wincol()" function
18793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018795f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018796 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798{
18799 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
18803/*
18804 * "winheight(nr)" function
18805 */
18806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018808 typval_T *argvars;
18809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810{
18811 win_T *wp;
18812
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018813 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018817 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818}
18819
18820/*
18821 * "winline()" function
18822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827{
18828 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018829 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830}
18831
18832/*
18833 * "winnr()" function
18834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839{
18840 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018841
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018843 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018845 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
18848/*
18849 * "winrestcmd()" function
18850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018852f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855{
18856#ifdef FEAT_WINDOWS
18857 win_T *wp;
18858 int winnr = 1;
18859 garray_T ga;
18860 char_u buf[50];
18861
18862 ga_init2(&ga, (int)sizeof(char), 70);
18863 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18864 {
18865 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18866 ga_concat(&ga, buf);
18867# ifdef FEAT_VERTSPLIT
18868 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18869 ga_concat(&ga, buf);
18870# endif
18871 ++winnr;
18872 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018873 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018879 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880}
18881
18882/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018883 * "winrestview()" function
18884 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018885 static void
18886f_winrestview(argvars, rettv)
18887 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018888 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018889{
18890 dict_T *dict;
18891
18892 if (argvars[0].v_type != VAR_DICT
18893 || (dict = argvars[0].vval.v_dict) == NULL)
18894 EMSG(_(e_invarg));
18895 else
18896 {
18897 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18898 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18899#ifdef FEAT_VIRTUALEDIT
18900 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18901#endif
18902 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018903 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018904
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018905 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018906#ifdef FEAT_DIFF
18907 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18908#endif
18909 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18910 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18911
18912 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018913 win_new_height(curwin, curwin->w_height);
18914# ifdef FEAT_VERTSPLIT
18915 win_new_width(curwin, W_WIDTH(curwin));
18916# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018917 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018918
18919 if (curwin->w_topline == 0)
18920 curwin->w_topline = 1;
18921 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18922 curwin->w_topline = curbuf->b_ml.ml_line_count;
18923#ifdef FEAT_DIFF
18924 check_topfill(curwin, TRUE);
18925#endif
18926 }
18927}
18928
18929/*
18930 * "winsaveview()" function
18931 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018932 static void
18933f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018934 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018935 typval_T *rettv;
18936{
18937 dict_T *dict;
18938
Bram Moolenaara800b422010-06-27 01:15:55 +020018939 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018940 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018941 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018942
18943 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18944 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18945#ifdef FEAT_VIRTUALEDIT
18946 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18947#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018948 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018949 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18950
18951 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18952#ifdef FEAT_DIFF
18953 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18954#endif
18955 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18956 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18957}
18958
18959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 * "winwidth(nr)" function
18961 */
18962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018963f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018964 typval_T *argvars;
18965 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966{
18967 win_T *wp;
18968
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018969 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 else
18973#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977#endif
18978}
18979
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018981 * "writefile()" function
18982 */
18983 static void
18984f_writefile(argvars, rettv)
18985 typval_T *argvars;
18986 typval_T *rettv;
18987{
18988 int binary = FALSE;
18989 char_u *fname;
18990 FILE *fd;
18991 listitem_T *li;
18992 char_u *s;
18993 int ret = 0;
18994 int c;
18995
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018996 if (check_restricted() || check_secure())
18997 return;
18998
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018999 if (argvars[0].v_type != VAR_LIST)
19000 {
19001 EMSG2(_(e_listarg), "writefile()");
19002 return;
19003 }
19004 if (argvars[0].vval.v_list == NULL)
19005 return;
19006
19007 if (argvars[2].v_type != VAR_UNKNOWN
19008 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19009 binary = TRUE;
19010
19011 /* Always open the file in binary mode, library functions have a mind of
19012 * their own about CR-LF conversion. */
19013 fname = get_tv_string(&argvars[1]);
19014 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19015 {
19016 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19017 ret = -1;
19018 }
19019 else
19020 {
19021 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19022 li = li->li_next)
19023 {
19024 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19025 {
19026 if (*s == '\n')
19027 c = putc(NUL, fd);
19028 else
19029 c = putc(*s, fd);
19030 if (c == EOF)
19031 {
19032 ret = -1;
19033 break;
19034 }
19035 }
19036 if (!binary || li->li_next != NULL)
19037 if (putc('\n', fd) == EOF)
19038 {
19039 ret = -1;
19040 break;
19041 }
19042 if (ret < 0)
19043 {
19044 EMSG(_(e_write));
19045 break;
19046 }
19047 }
19048 fclose(fd);
19049 }
19050
19051 rettv->vval.v_number = ret;
19052}
19053
19054/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019055 * "xor(expr, expr)" function
19056 */
19057 static void
19058f_xor(argvars, rettv)
19059 typval_T *argvars;
19060 typval_T *rettv;
19061{
19062 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19063 ^ get_tv_number_chk(&argvars[1], NULL);
19064}
19065
19066
19067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019069 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 */
19071 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019072var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019073 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019074 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019075 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019077 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019079 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080
Bram Moolenaara5525202006-03-02 22:52:09 +000019081 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019082 if (varp->v_type == VAR_LIST)
19083 {
19084 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019085 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019086 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019087 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019088
19089 l = varp->vval.v_list;
19090 if (l == NULL)
19091 return NULL;
19092
19093 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019094 pos.lnum = list_find_nr(l, 0L, &error);
19095 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019096 return NULL; /* invalid line number */
19097
19098 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019099 pos.col = list_find_nr(l, 1L, &error);
19100 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019101 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019102 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019103
19104 /* We accept "$" for the column number: last column. */
19105 li = list_find(l, 1L);
19106 if (li != NULL && li->li_tv.v_type == VAR_STRING
19107 && li->li_tv.vval.v_string != NULL
19108 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19109 pos.col = len + 1;
19110
Bram Moolenaara5525202006-03-02 22:52:09 +000019111 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019112 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019113 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019114 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019115
Bram Moolenaara5525202006-03-02 22:52:09 +000019116#ifdef FEAT_VIRTUALEDIT
19117 /* Get the virtual offset. Defaults to zero. */
19118 pos.coladd = list_find_nr(l, 2L, &error);
19119 if (error)
19120 pos.coladd = 0;
19121#endif
19122
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019123 return &pos;
19124 }
19125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019126 name = get_tv_string_chk(varp);
19127 if (name == NULL)
19128 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019129 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019131#ifdef FEAT_VISUAL
19132 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19133 {
19134 if (VIsual_active)
19135 return &VIsual;
19136 return &curwin->w_cursor;
19137 }
19138#endif
19139 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019141 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19143 return NULL;
19144 return pp;
19145 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019146
19147#ifdef FEAT_VIRTUALEDIT
19148 pos.coladd = 0;
19149#endif
19150
Bram Moolenaar477933c2007-07-17 14:32:23 +000019151 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019152 {
19153 pos.col = 0;
19154 if (name[1] == '0') /* "w0": first visible line */
19155 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019156 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019157 pos.lnum = curwin->w_topline;
19158 return &pos;
19159 }
19160 else if (name[1] == '$') /* "w$": last visible line */
19161 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019162 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019163 pos.lnum = curwin->w_botline - 1;
19164 return &pos;
19165 }
19166 }
19167 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019169 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 {
19171 pos.lnum = curbuf->b_ml.ml_line_count;
19172 pos.col = 0;
19173 }
19174 else
19175 {
19176 pos.lnum = curwin->w_cursor.lnum;
19177 pos.col = (colnr_T)STRLEN(ml_get_curline());
19178 }
19179 return &pos;
19180 }
19181 return NULL;
19182}
19183
19184/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019185 * Convert list in "arg" into a position and optional file number.
19186 * When "fnump" is NULL there is no file number, only 3 items.
19187 * Note that the column is passed on as-is, the caller may want to decrement
19188 * it to use 1 for the first column.
19189 * Return FAIL when conversion is not possible, doesn't check the position for
19190 * validity.
19191 */
19192 static int
19193list2fpos(arg, posp, fnump)
19194 typval_T *arg;
19195 pos_T *posp;
19196 int *fnump;
19197{
19198 list_T *l = arg->vval.v_list;
19199 long i = 0;
19200 long n;
19201
Bram Moolenaarbde35262006-07-23 20:12:24 +000019202 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19203 * when "fnump" isn't NULL and "coladd" is optional. */
19204 if (arg->v_type != VAR_LIST
19205 || l == NULL
19206 || l->lv_len < (fnump == NULL ? 2 : 3)
19207 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019208 return FAIL;
19209
19210 if (fnump != NULL)
19211 {
19212 n = list_find_nr(l, i++, NULL); /* fnum */
19213 if (n < 0)
19214 return FAIL;
19215 if (n == 0)
19216 n = curbuf->b_fnum; /* current buffer */
19217 *fnump = n;
19218 }
19219
19220 n = list_find_nr(l, i++, NULL); /* lnum */
19221 if (n < 0)
19222 return FAIL;
19223 posp->lnum = n;
19224
19225 n = list_find_nr(l, i++, NULL); /* col */
19226 if (n < 0)
19227 return FAIL;
19228 posp->col = n;
19229
19230#ifdef FEAT_VIRTUALEDIT
19231 n = list_find_nr(l, i, NULL);
19232 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019233 posp->coladd = 0;
19234 else
19235 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019236#endif
19237
19238 return OK;
19239}
19240
19241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 * Get the length of an environment variable name.
19243 * Advance "arg" to the first character after the name.
19244 * Return 0 for error.
19245 */
19246 static int
19247get_env_len(arg)
19248 char_u **arg;
19249{
19250 char_u *p;
19251 int len;
19252
19253 for (p = *arg; vim_isIDc(*p); ++p)
19254 ;
19255 if (p == *arg) /* no name found */
19256 return 0;
19257
19258 len = (int)(p - *arg);
19259 *arg = p;
19260 return len;
19261}
19262
19263/*
19264 * Get the length of the name of a function or internal variable.
19265 * "arg" is advanced to the first non-white character after the name.
19266 * Return 0 if something is wrong.
19267 */
19268 static int
19269get_id_len(arg)
19270 char_u **arg;
19271{
19272 char_u *p;
19273 int len;
19274
19275 /* Find the end of the name. */
19276 for (p = *arg; eval_isnamec(*p); ++p)
19277 ;
19278 if (p == *arg) /* no name found */
19279 return 0;
19280
19281 len = (int)(p - *arg);
19282 *arg = skipwhite(p);
19283
19284 return len;
19285}
19286
19287/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019288 * Get the length of the name of a variable or function.
19289 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019291 * Return -1 if curly braces expansion failed.
19292 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 * If the name contains 'magic' {}'s, expand them and return the
19294 * expanded name in an allocated string via 'alias' - caller must free.
19295 */
19296 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019297get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 char_u **arg;
19299 char_u **alias;
19300 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019301 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302{
19303 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 char_u *p;
19305 char_u *expr_start;
19306 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307
19308 *alias = NULL; /* default to no alias */
19309
19310 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19311 && (*arg)[2] == (int)KE_SNR)
19312 {
19313 /* hard coded <SNR>, already translated */
19314 *arg += 3;
19315 return get_id_len(arg) + 3;
19316 }
19317 len = eval_fname_script(*arg);
19318 if (len > 0)
19319 {
19320 /* literal "<SID>", "s:" or "<SNR>" */
19321 *arg += len;
19322 }
19323
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019327 p = find_name_end(*arg, &expr_start, &expr_end,
19328 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 if (expr_start != NULL)
19330 {
19331 char_u *temp_string;
19332
19333 if (!evaluate)
19334 {
19335 len += (int)(p - *arg);
19336 *arg = skipwhite(p);
19337 return len;
19338 }
19339
19340 /*
19341 * Include any <SID> etc in the expanded string:
19342 * Thus the -len here.
19343 */
19344 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19345 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019346 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 *alias = temp_string;
19348 *arg = skipwhite(p);
19349 return (int)STRLEN(temp_string);
19350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351
19352 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019353 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 EMSG2(_(e_invexpr2), *arg);
19355
19356 return len;
19357}
19358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359/*
19360 * Find the end of a variable or function name, taking care of magic braces.
19361 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19362 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019363 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019364 * Return a pointer to just after the name. Equal to "arg" if there is no
19365 * valid name.
19366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019368find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 char_u *arg;
19370 char_u **expr_start;
19371 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019372 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019374 int mb_nest = 0;
19375 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 char_u *p;
19377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019380 *expr_start = NULL;
19381 *expr_end = NULL;
19382 }
19383
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019384 /* Quick check for valid starting character. */
19385 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19386 return arg;
19387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388 for (p = arg; *p != NUL
19389 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019390 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019391 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019392 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019393 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019394 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019395 if (*p == '\'')
19396 {
19397 /* skip over 'string' to avoid counting [ and ] inside it. */
19398 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19399 ;
19400 if (*p == NUL)
19401 break;
19402 }
19403 else if (*p == '"')
19404 {
19405 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19406 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19407 if (*p == '\\' && p[1] != NUL)
19408 ++p;
19409 if (*p == NUL)
19410 break;
19411 }
19412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019413 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019415 if (*p == '[')
19416 ++br_nest;
19417 else if (*p == ']')
19418 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019421 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019423 if (*p == '{')
19424 {
19425 mb_nest++;
19426 if (expr_start != NULL && *expr_start == NULL)
19427 *expr_start = p;
19428 }
19429 else if (*p == '}')
19430 {
19431 mb_nest--;
19432 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19433 *expr_end = p;
19434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 }
19437
19438 return p;
19439}
19440
19441/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019442 * Expands out the 'magic' {}'s in a variable/function name.
19443 * Note that this can call itself recursively, to deal with
19444 * constructs like foo{bar}{baz}{bam}
19445 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19446 * "in_start" ^
19447 * "expr_start" ^
19448 * "expr_end" ^
19449 * "in_end" ^
19450 *
19451 * Returns a new allocated string, which the caller must free.
19452 * Returns NULL for failure.
19453 */
19454 static char_u *
19455make_expanded_name(in_start, expr_start, expr_end, in_end)
19456 char_u *in_start;
19457 char_u *expr_start;
19458 char_u *expr_end;
19459 char_u *in_end;
19460{
19461 char_u c1;
19462 char_u *retval = NULL;
19463 char_u *temp_result;
19464 char_u *nextcmd = NULL;
19465
19466 if (expr_end == NULL || in_end == NULL)
19467 return NULL;
19468 *expr_start = NUL;
19469 *expr_end = NUL;
19470 c1 = *in_end;
19471 *in_end = NUL;
19472
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019473 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019474 if (temp_result != NULL && nextcmd == NULL)
19475 {
19476 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19477 + (in_end - expr_end) + 1));
19478 if (retval != NULL)
19479 {
19480 STRCPY(retval, in_start);
19481 STRCAT(retval, temp_result);
19482 STRCAT(retval, expr_end + 1);
19483 }
19484 }
19485 vim_free(temp_result);
19486
19487 *in_end = c1; /* put char back for error messages */
19488 *expr_start = '{';
19489 *expr_end = '}';
19490
19491 if (retval != NULL)
19492 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019493 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019494 if (expr_start != NULL)
19495 {
19496 /* Further expansion! */
19497 temp_result = make_expanded_name(retval, expr_start,
19498 expr_end, temp_result);
19499 vim_free(retval);
19500 retval = temp_result;
19501 }
19502 }
19503
19504 return retval;
19505}
19506
19507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019509 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 */
19511 static int
19512eval_isnamec(c)
19513 int c;
19514{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019515 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19516}
19517
19518/*
19519 * Return TRUE if character "c" can be used as the first character in a
19520 * variable or function name (excluding '{' and '}').
19521 */
19522 static int
19523eval_isnamec1(c)
19524 int c;
19525{
19526 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527}
19528
19529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 * Set number v: variable to "val".
19531 */
19532 void
19533set_vim_var_nr(idx, val)
19534 int idx;
19535 long val;
19536{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019537 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538}
19539
19540/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019541 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 */
19543 long
19544get_vim_var_nr(idx)
19545 int idx;
19546{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019547 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548}
19549
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019550/*
19551 * Get string v: variable value. Uses a static buffer, can only be used once.
19552 */
19553 char_u *
19554get_vim_var_str(idx)
19555 int idx;
19556{
19557 return get_tv_string(&vimvars[idx].vv_tv);
19558}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019559
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019561 * Get List v: variable value. Caller must take care of reference count when
19562 * needed.
19563 */
19564 list_T *
19565get_vim_var_list(idx)
19566 int idx;
19567{
19568 return vimvars[idx].vv_list;
19569}
19570
19571/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019572 * Set v:char to character "c".
19573 */
19574 void
19575set_vim_var_char(c)
19576 int c;
19577{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019578 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019579
19580#ifdef FEAT_MBYTE
19581 if (has_mbyte)
19582 buf[(*mb_char2bytes)(c, buf)] = NUL;
19583 else
19584#endif
19585 {
19586 buf[0] = c;
19587 buf[1] = NUL;
19588 }
19589 set_vim_var_string(VV_CHAR, buf, -1);
19590}
19591
19592/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019593 * Set v:count to "count" and v:count1 to "count1".
19594 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 */
19596 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019597set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 long count;
19599 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019600 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019602 if (set_prevcount)
19603 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019604 vimvars[VV_COUNT].vv_nr = count;
19605 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606}
19607
19608/*
19609 * Set string v: variable to a copy of "val".
19610 */
19611 void
19612set_vim_var_string(idx, val, len)
19613 int idx;
19614 char_u *val;
19615 int len; /* length of "val" to use or -1 (whole string) */
19616{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019617 /* Need to do this (at least) once, since we can't initialize a union.
19618 * Will always be invoked when "v:progname" is set. */
19619 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19620
Bram Moolenaare9a41262005-01-15 22:18:47 +000019621 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019623 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019625 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019627 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628}
19629
19630/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019631 * Set List v: variable to "val".
19632 */
19633 void
19634set_vim_var_list(idx, val)
19635 int idx;
19636 list_T *val;
19637{
19638 list_unref(vimvars[idx].vv_list);
19639 vimvars[idx].vv_list = val;
19640 if (val != NULL)
19641 ++val->lv_refcount;
19642}
19643
19644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 * Set v:register if needed.
19646 */
19647 void
19648set_reg_var(c)
19649 int c;
19650{
19651 char_u regname;
19652
19653 if (c == 0 || c == ' ')
19654 regname = '"';
19655 else
19656 regname = c;
19657 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019658 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 set_vim_var_string(VV_REG, &regname, 1);
19660}
19661
19662/*
19663 * Get or set v:exception. If "oldval" == NULL, return the current value.
19664 * Otherwise, restore the value to "oldval" and return NULL.
19665 * Must always be called in pairs to save and restore v:exception! Does not
19666 * take care of memory allocations.
19667 */
19668 char_u *
19669v_exception(oldval)
19670 char_u *oldval;
19671{
19672 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019673 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674
Bram Moolenaare9a41262005-01-15 22:18:47 +000019675 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 return NULL;
19677}
19678
19679/*
19680 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19681 * Otherwise, restore the value to "oldval" and return NULL.
19682 * Must always be called in pairs to save and restore v:throwpoint! Does not
19683 * take care of memory allocations.
19684 */
19685 char_u *
19686v_throwpoint(oldval)
19687 char_u *oldval;
19688{
19689 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019690 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691
Bram Moolenaare9a41262005-01-15 22:18:47 +000019692 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 return NULL;
19694}
19695
19696#if defined(FEAT_AUTOCMD) || defined(PROTO)
19697/*
19698 * Set v:cmdarg.
19699 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19700 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19701 * Must always be called in pairs!
19702 */
19703 char_u *
19704set_cmdarg(eap, oldarg)
19705 exarg_T *eap;
19706 char_u *oldarg;
19707{
19708 char_u *oldval;
19709 char_u *newval;
19710 unsigned len;
19711
Bram Moolenaare9a41262005-01-15 22:18:47 +000019712 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019713 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019715 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019716 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019717 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 }
19719
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019720 if (eap->force_bin == FORCE_BIN)
19721 len = 6;
19722 else if (eap->force_bin == FORCE_NOBIN)
19723 len = 8;
19724 else
19725 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019726
19727 if (eap->read_edit)
19728 len += 7;
19729
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019730 if (eap->force_ff != 0)
19731 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19732# ifdef FEAT_MBYTE
19733 if (eap->force_enc != 0)
19734 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019735 if (eap->bad_char != 0)
19736 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019737# endif
19738
19739 newval = alloc(len + 1);
19740 if (newval == NULL)
19741 return NULL;
19742
19743 if (eap->force_bin == FORCE_BIN)
19744 sprintf((char *)newval, " ++bin");
19745 else if (eap->force_bin == FORCE_NOBIN)
19746 sprintf((char *)newval, " ++nobin");
19747 else
19748 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019749
19750 if (eap->read_edit)
19751 STRCAT(newval, " ++edit");
19752
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019753 if (eap->force_ff != 0)
19754 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19755 eap->cmd + eap->force_ff);
19756# ifdef FEAT_MBYTE
19757 if (eap->force_enc != 0)
19758 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19759 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019760 if (eap->bad_char == BAD_KEEP)
19761 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19762 else if (eap->bad_char == BAD_DROP)
19763 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19764 else if (eap->bad_char != 0)
19765 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019766# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019767 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019768 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769}
19770#endif
19771
19772/*
19773 * Get the value of internal variable "name".
19774 * Return OK or FAIL.
19775 */
19776 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019777get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 char_u *name;
19779 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019781 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782{
19783 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019784 typval_T *tv = NULL;
19785 typval_T atv;
19786 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788
19789 /* truncate the name, so that we can use strcmp() */
19790 cc = name[len];
19791 name[len] = NUL;
19792
19793 /*
19794 * Check for "b:changedtick".
19795 */
19796 if (STRCMP(name, "b:changedtick") == 0)
19797 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019798 atv.v_type = VAR_NUMBER;
19799 atv.vval.v_number = curbuf->b_changedtick;
19800 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 }
19802
19803 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 * Check for user-defined variables.
19805 */
19806 else
19807 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019808 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811 }
19812
Bram Moolenaare9a41262005-01-15 22:18:47 +000019813 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019815 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019816 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 ret = FAIL;
19818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019820 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821
19822 name[len] = cc;
19823
19824 return ret;
19825}
19826
19827/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019828 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19829 * Also handle function call with Funcref variable: func(expr)
19830 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19831 */
19832 static int
19833handle_subscript(arg, rettv, evaluate, verbose)
19834 char_u **arg;
19835 typval_T *rettv;
19836 int evaluate; /* do more than finding the end */
19837 int verbose; /* give error messages */
19838{
19839 int ret = OK;
19840 dict_T *selfdict = NULL;
19841 char_u *s;
19842 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019843 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019844
19845 while (ret == OK
19846 && (**arg == '['
19847 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019848 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019849 && !vim_iswhite(*(*arg - 1)))
19850 {
19851 if (**arg == '(')
19852 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019853 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019854 if (evaluate)
19855 {
19856 functv = *rettv;
19857 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019858
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019859 /* Invoke the function. Recursive! */
19860 s = functv.vval.v_string;
19861 }
19862 else
19863 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019864 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019865 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19866 &len, evaluate, selfdict);
19867
19868 /* Clear the funcref afterwards, so that deleting it while
19869 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019870 if (evaluate)
19871 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019872
19873 /* Stop the expression evaluation when immediately aborting on
19874 * error, or when an interrupt occurred or an exception was thrown
19875 * but not caught. */
19876 if (aborting())
19877 {
19878 if (ret == OK)
19879 clear_tv(rettv);
19880 ret = FAIL;
19881 }
19882 dict_unref(selfdict);
19883 selfdict = NULL;
19884 }
19885 else /* **arg == '[' || **arg == '.' */
19886 {
19887 dict_unref(selfdict);
19888 if (rettv->v_type == VAR_DICT)
19889 {
19890 selfdict = rettv->vval.v_dict;
19891 if (selfdict != NULL)
19892 ++selfdict->dv_refcount;
19893 }
19894 else
19895 selfdict = NULL;
19896 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19897 {
19898 clear_tv(rettv);
19899 ret = FAIL;
19900 }
19901 }
19902 }
19903 dict_unref(selfdict);
19904 return ret;
19905}
19906
19907/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019908 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 * value).
19910 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019911 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019912alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019913{
Bram Moolenaar33570922005-01-25 22:26:29 +000019914 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019915}
19916
19917/*
19918 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 * The string "s" must have been allocated, it is consumed.
19920 * Return NULL for out of memory, the variable otherwise.
19921 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 char_u *s;
19925{
Bram Moolenaar33570922005-01-25 22:26:29 +000019926 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928 rettv = alloc_tv();
19929 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931 rettv->v_type = VAR_STRING;
19932 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 }
19934 else
19935 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937}
19938
19939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019940 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019942 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019943free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019944 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019945{
19946 if (varp != NULL)
19947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019948 switch (varp->v_type)
19949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019950 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019951 func_unref(varp->vval.v_string);
19952 /*FALLTHROUGH*/
19953 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 vim_free(varp->vval.v_string);
19955 break;
19956 case VAR_LIST:
19957 list_unref(varp->vval.v_list);
19958 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019959 case VAR_DICT:
19960 dict_unref(varp->vval.v_dict);
19961 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019962 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019963#ifdef FEAT_FLOAT
19964 case VAR_FLOAT:
19965#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019966 case VAR_UNKNOWN:
19967 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019969 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019970 break;
19971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 vim_free(varp);
19973 }
19974}
19975
19976/*
19977 * Free the memory for a variable value and set the value to NULL or 0.
19978 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019979 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019980clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019981 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982{
19983 if (varp != NULL)
19984 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019987 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019988 func_unref(varp->vval.v_string);
19989 /*FALLTHROUGH*/
19990 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 vim_free(varp->vval.v_string);
19992 varp->vval.v_string = NULL;
19993 break;
19994 case VAR_LIST:
19995 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019996 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019997 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019998 case VAR_DICT:
19999 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020000 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020001 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 varp->vval.v_number = 0;
20004 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020005#ifdef FEAT_FLOAT
20006 case VAR_FLOAT:
20007 varp->vval.v_float = 0.0;
20008 break;
20009#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010 case VAR_UNKNOWN:
20011 break;
20012 default:
20013 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020015 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 }
20017}
20018
20019/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 * Set the value of a variable to NULL without freeing items.
20021 */
20022 static void
20023init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020024 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025{
20026 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020027 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020028}
20029
20030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 * Get the number value of a variable.
20032 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020033 * For incompatible types, return 0.
20034 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20035 * caller of incompatible types: it sets *denote to TRUE if "denote"
20036 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 */
20038 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020042 int error = FALSE;
20043
20044 return get_tv_number_chk(varp, &error); /* return 0L on error */
20045}
20046
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020047 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020048get_tv_number_chk(varp, denote)
20049 typval_T *varp;
20050 int *denote;
20051{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020052 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020054 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020056 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020057 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020058#ifdef FEAT_FLOAT
20059 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020060 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020061 break;
20062#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020063 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020064 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 break;
20066 case VAR_STRING:
20067 if (varp->vval.v_string != NULL)
20068 vim_str2nr(varp->vval.v_string, NULL, NULL,
20069 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020071 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020072 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020073 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020074 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020075 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020076 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020078 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020081 if (denote == NULL) /* useful for values that must be unsigned */
20082 n = -1;
20083 else
20084 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020085 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086}
20087
20088/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020089 * Get the lnum from the first argument.
20090 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020091 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 */
20093 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020094get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020095 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096{
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 linenr_T lnum;
20099
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020100 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 if (lnum == 0) /* no valid number, try using line() */
20102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020103 rettv.v_type = VAR_NUMBER;
20104 f_line(argvars, &rettv);
20105 lnum = rettv.vval.v_number;
20106 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 }
20108 return lnum;
20109}
20110
20111/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020112 * Get the lnum from the first argument.
20113 * Also accepts "$", then "buf" is used.
20114 * Returns 0 on error.
20115 */
20116 static linenr_T
20117get_tv_lnum_buf(argvars, buf)
20118 typval_T *argvars;
20119 buf_T *buf;
20120{
20121 if (argvars[0].v_type == VAR_STRING
20122 && argvars[0].vval.v_string != NULL
20123 && argvars[0].vval.v_string[0] == '$'
20124 && buf != NULL)
20125 return buf->b_ml.ml_line_count;
20126 return get_tv_number_chk(&argvars[0], NULL);
20127}
20128
20129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 * Get the string value of a variable.
20131 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020132 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20133 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 * If the String variable has never been set, return an empty string.
20135 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020136 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20137 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 */
20139 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020141 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142{
20143 static char_u mybuf[NUMBUFLEN];
20144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020145 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146}
20147
20148 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020151 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020153 char_u *res = get_tv_string_buf_chk(varp, buf);
20154
20155 return res != NULL ? res : (char_u *)"";
20156}
20157
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020158 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020159get_tv_string_chk(varp)
20160 typval_T *varp;
20161{
20162 static char_u mybuf[NUMBUFLEN];
20163
20164 return get_tv_string_buf_chk(varp, mybuf);
20165}
20166
20167 static char_u *
20168get_tv_string_buf_chk(varp, buf)
20169 typval_T *varp;
20170 char_u *buf;
20171{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020172 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020174 case VAR_NUMBER:
20175 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20176 return buf;
20177 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020178 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020179 break;
20180 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020181 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020182 break;
20183 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020184 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020185 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020186#ifdef FEAT_FLOAT
20187 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020188 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020189 break;
20190#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020191 case VAR_STRING:
20192 if (varp->vval.v_string != NULL)
20193 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020194 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020195 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020197 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020199 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200}
20201
20202/*
20203 * Find variable "name" in the list of variables.
20204 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020206 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020209 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020210find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020215 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216
Bram Moolenaara7043832005-01-21 11:56:39 +000020217 ht = find_var_ht(name, &varname);
20218 if (htp != NULL)
20219 *htp = ht;
20220 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020222 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223}
20224
20225/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020226 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020227 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020230find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020231 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020232 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020233 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020234 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020235{
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 hashitem_T *hi;
20237
20238 if (*varname == NUL)
20239 {
20240 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020241 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020242 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020243 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 case 'g': return &globvars_var;
20245 case 'v': return &vimvars_var;
20246 case 'b': return &curbuf->b_bufvar;
20247 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020248#ifdef FEAT_WINDOWS
20249 case 't': return &curtab->tp_winvar;
20250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020251 case 'l': return current_funccal == NULL
20252 ? NULL : &current_funccal->l_vars_var;
20253 case 'a': return current_funccal == NULL
20254 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020255 }
20256 return NULL;
20257 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020258
20259 hi = hash_find(ht, varname);
20260 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020261 {
20262 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020263 * worked find the variable again. Don't auto-load a script if it was
20264 * loaded already, otherwise it would be loaded every time when
20265 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020266 if (ht == &globvarht && !writing)
20267 {
20268 /* Note: script_autoload() may make "hi" invalid. It must either
20269 * be obtained again or not used. */
20270 if (!script_autoload(varname, FALSE) || aborting())
20271 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020272 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020273 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020274 if (HASHITEM_EMPTY(hi))
20275 return NULL;
20276 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020278}
20279
20280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020282 * Set "varname" to the start of name without ':'.
20283 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020284 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020285find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 char_u *name;
20287 char_u **varname;
20288{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020289 hashitem_T *hi;
20290
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 if (name[1] != ':')
20292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020293 /* The name must not start with a colon or #. */
20294 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 return NULL;
20296 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020297
20298 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020299 hi = hash_find(&compat_hashtab, name);
20300 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020301 return &compat_hashtab;
20302
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 return &globvarht; /* global variable */
20305 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 }
20307 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020308 if (*name == 'g') /* global variable */
20309 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020310 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20311 */
20312 if (vim_strchr(name + 2, ':') != NULL
20313 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020314 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020316 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020318 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020319#ifdef FEAT_WINDOWS
20320 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020321 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020322#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 if (*name == 'v') /* v: variable */
20324 return &vimvarht;
20325 if (*name == 'a' && current_funccal != NULL) /* function argument */
20326 return &current_funccal->l_avars.dv_hashtab;
20327 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20328 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 if (*name == 's' /* script variable */
20330 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20331 return &SCRIPT_VARS(current_SID);
20332 return NULL;
20333}
20334
20335/*
20336 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020337 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 * Returns NULL when it doesn't exist.
20339 */
20340 char_u *
20341get_var_value(name)
20342 char_u *name;
20343{
Bram Moolenaar33570922005-01-25 22:26:29 +000020344 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345
Bram Moolenaara7043832005-01-21 11:56:39 +000020346 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 if (v == NULL)
20348 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350}
20351
20352/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020353 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354 * sourcing this script and when executing functions defined in the script.
20355 */
20356 void
20357new_script_vars(id)
20358 scid_T id;
20359{
Bram Moolenaara7043832005-01-21 11:56:39 +000020360 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 hashtab_T *ht;
20362 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020363
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20365 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020366 /* Re-allocating ga_data means that an ht_array pointing to
20367 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020369 for (i = 1; i <= ga_scripts.ga_len; ++i)
20370 {
20371 ht = &SCRIPT_VARS(i);
20372 if (ht->ht_mask == HT_INIT_SIZE - 1)
20373 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020374 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020376 }
20377
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 while (ga_scripts.ga_len < id)
20379 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020380 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020381 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020382 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 }
20385 }
20386}
20387
20388/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20390 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 */
20392 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020393init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 dict_T *dict;
20395 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020396 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397{
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020399 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020400 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020401 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020402 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 dict_var->di_tv.vval.v_dict = dict;
20404 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020405 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020406 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20407 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408}
20409
20410/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020411 * Unreference a dictionary initialized by init_var_dict().
20412 */
20413 void
20414unref_var_dict(dict)
20415 dict_T *dict;
20416{
20417 /* Now the dict needs to be freed if no one else is using it, go back to
20418 * normal reference counting. */
20419 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20420 dict_unref(dict);
20421}
20422
20423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020425 * Frees all allocated variables and the value they contain.
20426 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 */
20428 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020429vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020430 hashtab_T *ht;
20431{
20432 vars_clear_ext(ht, TRUE);
20433}
20434
20435/*
20436 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20437 */
20438 static void
20439vars_clear_ext(ht, free_val)
20440 hashtab_T *ht;
20441 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442{
Bram Moolenaara7043832005-01-21 11:56:39 +000020443 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 hashitem_T *hi;
20445 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020448 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020449 for (hi = ht->ht_array; todo > 0; ++hi)
20450 {
20451 if (!HASHITEM_EMPTY(hi))
20452 {
20453 --todo;
20454
Bram Moolenaar33570922005-01-25 22:26:29 +000020455 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020456 * ht_array might change then. hash_clear() takes care of it
20457 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 v = HI2DI(hi);
20459 if (free_val)
20460 clear_tv(&v->di_tv);
20461 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20462 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020463 }
20464 }
20465 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020466 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467}
20468
Bram Moolenaara7043832005-01-21 11:56:39 +000020469/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 * Delete a variable from hashtab "ht" at item "hi".
20471 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020474delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020475 hashtab_T *ht;
20476 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477{
Bram Moolenaar33570922005-01-25 22:26:29 +000020478 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020479
20480 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020481 clear_tv(&di->di_tv);
20482 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483}
20484
20485/*
20486 * List the value of one internal variable.
20487 */
20488 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020489list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020492 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 char_u *tofree;
20495 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020496 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020497
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020498 current_copyID += COPYID_INC;
20499 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020501 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020502 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503}
20504
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020506list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 char_u *prefix;
20508 char_u *name;
20509 int type;
20510 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020511 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512{
Bram Moolenaar31859182007-08-14 20:41:13 +000020513 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20514 msg_start();
20515 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 if (name != NULL) /* "a:" vars don't have a name stored */
20517 msg_puts(name);
20518 msg_putchar(' ');
20519 msg_advance(22);
20520 if (type == VAR_NUMBER)
20521 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020522 else if (type == VAR_FUNC)
20523 msg_putchar('*');
20524 else if (type == VAR_LIST)
20525 {
20526 msg_putchar('[');
20527 if (*string == '[')
20528 ++string;
20529 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020530 else if (type == VAR_DICT)
20531 {
20532 msg_putchar('{');
20533 if (*string == '{')
20534 ++string;
20535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 else
20537 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020538
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020540
20541 if (type == VAR_FUNC)
20542 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020543 if (*first)
20544 {
20545 msg_clr_eos();
20546 *first = FALSE;
20547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548}
20549
20550/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 * If the variable already exists, the value is updated.
20553 * Otherwise the variable is created.
20554 */
20555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020556set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020558 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020559 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020565 ht = find_var_ht(name, &varname);
20566 if (ht == NULL || *varname == NUL)
20567 {
20568 EMSG2(_(e_illvar), name);
20569 return;
20570 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020571 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020572
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020573 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20574 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020575
Bram Moolenaar33570922005-01-25 22:26:29 +000020576 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020579 if (var_check_ro(v->di_flags, name)
20580 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 return;
20582 if (v->di_tv.v_type != tv->v_type
20583 && !((v->di_tv.v_type == VAR_STRING
20584 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020585 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020586 || tv->v_type == VAR_NUMBER))
20587#ifdef FEAT_FLOAT
20588 && !((v->di_tv.v_type == VAR_NUMBER
20589 || v->di_tv.v_type == VAR_FLOAT)
20590 && (tv->v_type == VAR_NUMBER
20591 || tv->v_type == VAR_FLOAT))
20592#endif
20593 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020594 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020595 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020596 return;
20597 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020598
20599 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020600 * Handle setting internal v: variables separately: we don't change
20601 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 */
20603 if (ht == &vimvarht)
20604 {
20605 if (v->di_tv.v_type == VAR_STRING)
20606 {
20607 vim_free(v->di_tv.vval.v_string);
20608 if (copy || tv->v_type != VAR_STRING)
20609 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20610 else
20611 {
20612 /* Take over the string to avoid an extra alloc/free. */
20613 v->di_tv.vval.v_string = tv->vval.v_string;
20614 tv->vval.v_string = NULL;
20615 }
20616 }
20617 else if (v->di_tv.v_type != VAR_NUMBER)
20618 EMSG2(_(e_intern2), "set_var()");
20619 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020622 if (STRCMP(varname, "searchforward") == 0)
20623 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020624#ifdef FEAT_SEARCH_EXTRA
20625 else if (STRCMP(varname, "hlsearch") == 0)
20626 {
20627 no_hlsearch = !v->di_tv.vval.v_number;
20628 redraw_all_later(SOME_VALID);
20629 }
20630#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020631 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020632 return;
20633 }
20634
20635 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 }
20637 else /* add a new variable */
20638 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020639 /* Can't add "v:" variable. */
20640 if (ht == &vimvarht)
20641 {
20642 EMSG2(_(e_illvar), name);
20643 return;
20644 }
20645
Bram Moolenaar92124a32005-06-17 22:03:40 +000020646 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020647 if (!valid_varname(varname))
20648 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020649
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020650 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20651 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020652 if (v == NULL)
20653 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020654 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020655 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020657 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 return;
20659 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020660 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020663 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020664 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020665 else
20666 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020667 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020668 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020669 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671}
20672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020673/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020674 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020675 * Also give an error message.
20676 */
20677 static int
20678var_check_ro(flags, name)
20679 int flags;
20680 char_u *name;
20681{
20682 if (flags & DI_FLAGS_RO)
20683 {
20684 EMSG2(_(e_readonlyvar), name);
20685 return TRUE;
20686 }
20687 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20688 {
20689 EMSG2(_(e_readonlysbx), name);
20690 return TRUE;
20691 }
20692 return FALSE;
20693}
20694
20695/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020696 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20697 * Also give an error message.
20698 */
20699 static int
20700var_check_fixed(flags, name)
20701 int flags;
20702 char_u *name;
20703{
20704 if (flags & DI_FLAGS_FIX)
20705 {
20706 EMSG2(_("E795: Cannot delete variable %s"), name);
20707 return TRUE;
20708 }
20709 return FALSE;
20710}
20711
20712/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020713 * Check if a funcref is assigned to a valid variable name.
20714 * Return TRUE and give an error if not.
20715 */
20716 static int
20717var_check_func_name(name, new_var)
20718 char_u *name; /* points to start of variable name */
20719 int new_var; /* TRUE when creating the variable */
20720{
20721 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20722 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20723 ? name[2] : name[0]))
20724 {
20725 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20726 name);
20727 return TRUE;
20728 }
20729 /* Don't allow hiding a function. When "v" is not NULL we might be
20730 * assigning another function to the same var, the type is checked
20731 * below. */
20732 if (new_var && function_exists(name))
20733 {
20734 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20735 name);
20736 return TRUE;
20737 }
20738 return FALSE;
20739}
20740
20741/*
20742 * Check if a variable name is valid.
20743 * Return FALSE and give an error if not.
20744 */
20745 static int
20746valid_varname(varname)
20747 char_u *varname;
20748{
20749 char_u *p;
20750
20751 for (p = varname; *p != NUL; ++p)
20752 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20753 && *p != AUTOLOAD_CHAR)
20754 {
20755 EMSG2(_(e_illvar), varname);
20756 return FALSE;
20757 }
20758 return TRUE;
20759}
20760
20761/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020762 * Return TRUE if typeval "tv" is set to be locked (immutable).
20763 * Also give an error message, using "name".
20764 */
20765 static int
20766tv_check_lock(lock, name)
20767 int lock;
20768 char_u *name;
20769{
20770 if (lock & VAR_LOCKED)
20771 {
20772 EMSG2(_("E741: Value is locked: %s"),
20773 name == NULL ? (char_u *)_("Unknown") : name);
20774 return TRUE;
20775 }
20776 if (lock & VAR_FIXED)
20777 {
20778 EMSG2(_("E742: Cannot change value of %s"),
20779 name == NULL ? (char_u *)_("Unknown") : name);
20780 return TRUE;
20781 }
20782 return FALSE;
20783}
20784
20785/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020786 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020787 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020788 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020789 * It is OK for "from" and "to" to point to the same item. This is used to
20790 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020791 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020792 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020793copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 typval_T *from;
20795 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020797 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020798 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020799 switch (from->v_type)
20800 {
20801 case VAR_NUMBER:
20802 to->vval.v_number = from->vval.v_number;
20803 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020804#ifdef FEAT_FLOAT
20805 case VAR_FLOAT:
20806 to->vval.v_float = from->vval.v_float;
20807 break;
20808#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020809 case VAR_STRING:
20810 case VAR_FUNC:
20811 if (from->vval.v_string == NULL)
20812 to->vval.v_string = NULL;
20813 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020814 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020815 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020816 if (from->v_type == VAR_FUNC)
20817 func_ref(to->vval.v_string);
20818 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020819 break;
20820 case VAR_LIST:
20821 if (from->vval.v_list == NULL)
20822 to->vval.v_list = NULL;
20823 else
20824 {
20825 to->vval.v_list = from->vval.v_list;
20826 ++to->vval.v_list->lv_refcount;
20827 }
20828 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020829 case VAR_DICT:
20830 if (from->vval.v_dict == NULL)
20831 to->vval.v_dict = NULL;
20832 else
20833 {
20834 to->vval.v_dict = from->vval.v_dict;
20835 ++to->vval.v_dict->dv_refcount;
20836 }
20837 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020838 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020840 break;
20841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842}
20843
20844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020845 * Make a copy of an item.
20846 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020847 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20848 * reference to an already copied list/dict can be used.
20849 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020850 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020851 static int
20852item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020853 typval_T *from;
20854 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020855 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020856 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020857{
20858 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020859 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020860
Bram Moolenaar33570922005-01-25 22:26:29 +000020861 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020862 {
20863 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020864 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865 }
20866 ++recurse;
20867
20868 switch (from->v_type)
20869 {
20870 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020871#ifdef FEAT_FLOAT
20872 case VAR_FLOAT:
20873#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020874 case VAR_STRING:
20875 case VAR_FUNC:
20876 copy_tv(from, to);
20877 break;
20878 case VAR_LIST:
20879 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020880 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020881 if (from->vval.v_list == NULL)
20882 to->vval.v_list = NULL;
20883 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20884 {
20885 /* use the copy made earlier */
20886 to->vval.v_list = from->vval.v_list->lv_copylist;
20887 ++to->vval.v_list->lv_refcount;
20888 }
20889 else
20890 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20891 if (to->vval.v_list == NULL)
20892 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020893 break;
20894 case VAR_DICT:
20895 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020896 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020897 if (from->vval.v_dict == NULL)
20898 to->vval.v_dict = NULL;
20899 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20900 {
20901 /* use the copy made earlier */
20902 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20903 ++to->vval.v_dict->dv_refcount;
20904 }
20905 else
20906 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20907 if (to->vval.v_dict == NULL)
20908 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020909 break;
20910 default:
20911 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020912 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020913 }
20914 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020915 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020916}
20917
20918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 * ":echo expr1 ..." print each argument separated with a space, add a
20920 * newline at the end.
20921 * ":echon expr1 ..." print each argument plain.
20922 */
20923 void
20924ex_echo(eap)
20925 exarg_T *eap;
20926{
20927 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020928 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020929 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 char_u *p;
20931 int needclr = TRUE;
20932 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020933 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934
20935 if (eap->skip)
20936 ++emsg_skip;
20937 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020939 /* If eval1() causes an error message the text from the command may
20940 * still need to be cleared. E.g., "echo 22,44". */
20941 need_clr_eos = needclr;
20942
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 {
20946 /*
20947 * Report the invalid expression unless the expression evaluation
20948 * has been cancelled due to an aborting error, an interrupt, or an
20949 * exception.
20950 */
20951 if (!aborting())
20952 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020953 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 break;
20955 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020956 need_clr_eos = FALSE;
20957
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 if (!eap->skip)
20959 {
20960 if (atstart)
20961 {
20962 atstart = FALSE;
20963 /* Call msg_start() after eval1(), evaluating the expression
20964 * may cause a message to appear. */
20965 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020966 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020967 /* Mark the saved text as finishing the line, so that what
20968 * follows is displayed on a new line when scrolling back
20969 * at the more prompt. */
20970 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 }
20974 else if (eap->cmdidx == CMD_echo)
20975 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020976 current_copyID += COPYID_INC;
20977 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020978 if (p != NULL)
20979 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020981 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020983 if (*p != TAB && needclr)
20984 {
20985 /* remove any text still there from the command */
20986 msg_clr_eos();
20987 needclr = FALSE;
20988 }
20989 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 }
20991 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020992 {
20993#ifdef FEAT_MBYTE
20994 if (has_mbyte)
20995 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020996 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020997
20998 (void)msg_outtrans_len_attr(p, i, echo_attr);
20999 p += i - 1;
21000 }
21001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021003 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021006 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021008 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 arg = skipwhite(arg);
21010 }
21011 eap->nextcmd = check_nextcmd(arg);
21012
21013 if (eap->skip)
21014 --emsg_skip;
21015 else
21016 {
21017 /* remove text that may still be there from the command */
21018 if (needclr)
21019 msg_clr_eos();
21020 if (eap->cmdidx == CMD_echo)
21021 msg_end();
21022 }
21023}
21024
21025/*
21026 * ":echohl {name}".
21027 */
21028 void
21029ex_echohl(eap)
21030 exarg_T *eap;
21031{
21032 int id;
21033
21034 id = syn_name2id(eap->arg);
21035 if (id == 0)
21036 echo_attr = 0;
21037 else
21038 echo_attr = syn_id2attr(id);
21039}
21040
21041/*
21042 * ":execute expr1 ..." execute the result of an expression.
21043 * ":echomsg expr1 ..." Print a message
21044 * ":echoerr expr1 ..." Print an error
21045 * Each gets spaces around each argument and a newline at the end for
21046 * echo commands
21047 */
21048 void
21049ex_execute(eap)
21050 exarg_T *eap;
21051{
21052 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021053 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 int ret = OK;
21055 char_u *p;
21056 garray_T ga;
21057 int len;
21058 int save_did_emsg;
21059
21060 ga_init2(&ga, 1, 80);
21061
21062 if (eap->skip)
21063 ++emsg_skip;
21064 while (*arg != NUL && *arg != '|' && *arg != '\n')
21065 {
21066 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021067 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 {
21069 /*
21070 * Report the invalid expression unless the expression evaluation
21071 * has been cancelled due to an aborting error, an interrupt, or an
21072 * exception.
21073 */
21074 if (!aborting())
21075 EMSG2(_(e_invexpr2), p);
21076 ret = FAIL;
21077 break;
21078 }
21079
21080 if (!eap->skip)
21081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 len = (int)STRLEN(p);
21084 if (ga_grow(&ga, len + 2) == FAIL)
21085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 ret = FAIL;
21088 break;
21089 }
21090 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 ga.ga_len += len;
21094 }
21095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021096 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 arg = skipwhite(arg);
21098 }
21099
21100 if (ret != FAIL && ga.ga_data != NULL)
21101 {
21102 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021105 out_flush();
21106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 else if (eap->cmdidx == CMD_echoerr)
21108 {
21109 /* We don't want to abort following commands, restore did_emsg. */
21110 save_did_emsg = did_emsg;
21111 EMSG((char_u *)ga.ga_data);
21112 if (!force_abort)
21113 did_emsg = save_did_emsg;
21114 }
21115 else if (eap->cmdidx == CMD_execute)
21116 do_cmdline((char_u *)ga.ga_data,
21117 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21118 }
21119
21120 ga_clear(&ga);
21121
21122 if (eap->skip)
21123 --emsg_skip;
21124
21125 eap->nextcmd = check_nextcmd(arg);
21126}
21127
21128/*
21129 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21130 * "arg" points to the "&" or '+' when called, to "option" when returning.
21131 * Returns NULL when no option name found. Otherwise pointer to the char
21132 * after the option name.
21133 */
21134 static char_u *
21135find_option_end(arg, opt_flags)
21136 char_u **arg;
21137 int *opt_flags;
21138{
21139 char_u *p = *arg;
21140
21141 ++p;
21142 if (*p == 'g' && p[1] == ':')
21143 {
21144 *opt_flags = OPT_GLOBAL;
21145 p += 2;
21146 }
21147 else if (*p == 'l' && p[1] == ':')
21148 {
21149 *opt_flags = OPT_LOCAL;
21150 p += 2;
21151 }
21152 else
21153 *opt_flags = 0;
21154
21155 if (!ASCII_ISALPHA(*p))
21156 return NULL;
21157 *arg = p;
21158
21159 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21160 p += 4; /* termcap option */
21161 else
21162 while (ASCII_ISALPHA(*p))
21163 ++p;
21164 return p;
21165}
21166
21167/*
21168 * ":function"
21169 */
21170 void
21171ex_function(eap)
21172 exarg_T *eap;
21173{
21174 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021175 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 int j;
21177 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021179 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 char_u *name = NULL;
21181 char_u *p;
21182 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021183 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 garray_T newargs;
21185 garray_T newlines;
21186 int varargs = FALSE;
21187 int mustend = FALSE;
21188 int flags = 0;
21189 ufunc_T *fp;
21190 int indent;
21191 int nesting;
21192 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021193 dictitem_T *v;
21194 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021195 static int func_nr = 0; /* number for nameless function */
21196 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021197 hashtab_T *ht;
21198 int todo;
21199 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021200 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201
21202 /*
21203 * ":function" without argument: list functions.
21204 */
21205 if (ends_excmd(*eap->arg))
21206 {
21207 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021208 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021209 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021210 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021211 {
21212 if (!HASHITEM_EMPTY(hi))
21213 {
21214 --todo;
21215 fp = HI2UF(hi);
21216 if (!isdigit(*fp->uf_name))
21217 list_func_head(fp, FALSE);
21218 }
21219 }
21220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 eap->nextcmd = check_nextcmd(eap->arg);
21222 return;
21223 }
21224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021225 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021226 * ":function /pat": list functions matching pattern.
21227 */
21228 if (*eap->arg == '/')
21229 {
21230 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21231 if (!eap->skip)
21232 {
21233 regmatch_T regmatch;
21234
21235 c = *p;
21236 *p = NUL;
21237 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21238 *p = c;
21239 if (regmatch.regprog != NULL)
21240 {
21241 regmatch.rm_ic = p_ic;
21242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021243 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021244 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21245 {
21246 if (!HASHITEM_EMPTY(hi))
21247 {
21248 --todo;
21249 fp = HI2UF(hi);
21250 if (!isdigit(*fp->uf_name)
21251 && vim_regexec(&regmatch, fp->uf_name, 0))
21252 list_func_head(fp, FALSE);
21253 }
21254 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021255 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021256 }
21257 }
21258 if (*p == '/')
21259 ++p;
21260 eap->nextcmd = check_nextcmd(p);
21261 return;
21262 }
21263
21264 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021265 * Get the function name. There are these situations:
21266 * func normal function name
21267 * "name" == func, "fudi.fd_dict" == NULL
21268 * dict.func new dictionary entry
21269 * "name" == NULL, "fudi.fd_dict" set,
21270 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21271 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021272 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021273 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21274 * dict.func existing dict entry that's not a Funcref
21275 * "name" == NULL, "fudi.fd_dict" set,
21276 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021279 name = trans_function_name(&p, eap->skip, 0, &fudi);
21280 paren = (vim_strchr(p, '(') != NULL);
21281 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 {
21283 /*
21284 * Return on an invalid expression in braces, unless the expression
21285 * evaluation has been cancelled due to an aborting error, an
21286 * interrupt, or an exception.
21287 */
21288 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021290 if (!eap->skip && fudi.fd_newkey != NULL)
21291 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021292 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 else
21296 eap->skip = TRUE;
21297 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021298
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 /* An error in a function call during evaluation of an expression in magic
21300 * braces should not cause the function not to be defined. */
21301 saved_did_emsg = did_emsg;
21302 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303
21304 /*
21305 * ":function func" with only function name: list function.
21306 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 {
21309 if (!ends_excmd(*skipwhite(p)))
21310 {
21311 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021312 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 }
21314 eap->nextcmd = check_nextcmd(p);
21315 if (eap->nextcmd != NULL)
21316 *p = NUL;
21317 if (!eap->skip && !got_int)
21318 {
21319 fp = find_func(name);
21320 if (fp != NULL)
21321 {
21322 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021323 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021325 if (FUNCLINE(fp, j) == NULL)
21326 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 msg_putchar('\n');
21328 msg_outnum((long)(j + 1));
21329 if (j < 9)
21330 msg_putchar(' ');
21331 if (j < 99)
21332 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021333 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 out_flush(); /* show a line at a time */
21335 ui_breakcheck();
21336 }
21337 if (!got_int)
21338 {
21339 msg_putchar('\n');
21340 msg_puts((char_u *)" endfunction");
21341 }
21342 }
21343 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021344 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021346 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 }
21348
21349 /*
21350 * ":function name(arg1, arg2)" Define function.
21351 */
21352 p = skipwhite(p);
21353 if (*p != '(')
21354 {
21355 if (!eap->skip)
21356 {
21357 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021358 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
21360 /* attempt to continue by skipping some text */
21361 if (vim_strchr(p, '(') != NULL)
21362 p = vim_strchr(p, '(');
21363 }
21364 p = skipwhite(p + 1);
21365
21366 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21367 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21368
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021369 if (!eap->skip)
21370 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021371 /* Check the name of the function. Unless it's a dictionary function
21372 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021373 if (name != NULL)
21374 arg = name;
21375 else
21376 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021377 if (arg != NULL && (fudi.fd_di == NULL
21378 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021379 {
21380 if (*arg == K_SPECIAL)
21381 j = 3;
21382 else
21383 j = 0;
21384 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21385 : eval_isnamec(arg[j])))
21386 ++j;
21387 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021388 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021389 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021390 /* Disallow using the g: dict. */
21391 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21392 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021393 }
21394
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 /*
21396 * Isolate the arguments: "arg1, arg2, ...)"
21397 */
21398 while (*p != ')')
21399 {
21400 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21401 {
21402 varargs = TRUE;
21403 p += 3;
21404 mustend = TRUE;
21405 }
21406 else
21407 {
21408 arg = p;
21409 while (ASCII_ISALNUM(*p) || *p == '_')
21410 ++p;
21411 if (arg == p || isdigit(*arg)
21412 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21413 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21414 {
21415 if (!eap->skip)
21416 EMSG2(_("E125: Illegal argument: %s"), arg);
21417 break;
21418 }
21419 if (ga_grow(&newargs, 1) == FAIL)
21420 goto erret;
21421 c = *p;
21422 *p = NUL;
21423 arg = vim_strsave(arg);
21424 if (arg == NULL)
21425 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021426
21427 /* Check for duplicate argument name. */
21428 for (i = 0; i < newargs.ga_len; ++i)
21429 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21430 {
21431 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21432 goto erret;
21433 }
21434
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21436 *p = c;
21437 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 if (*p == ',')
21439 ++p;
21440 else
21441 mustend = TRUE;
21442 }
21443 p = skipwhite(p);
21444 if (mustend && *p != ')')
21445 {
21446 if (!eap->skip)
21447 EMSG2(_(e_invarg2), eap->arg);
21448 break;
21449 }
21450 }
21451 ++p; /* skip the ')' */
21452
Bram Moolenaare9a41262005-01-15 22:18:47 +000021453 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 for (;;)
21455 {
21456 p = skipwhite(p);
21457 if (STRNCMP(p, "range", 5) == 0)
21458 {
21459 flags |= FC_RANGE;
21460 p += 5;
21461 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021462 else if (STRNCMP(p, "dict", 4) == 0)
21463 {
21464 flags |= FC_DICT;
21465 p += 4;
21466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 else if (STRNCMP(p, "abort", 5) == 0)
21468 {
21469 flags |= FC_ABORT;
21470 p += 5;
21471 }
21472 else
21473 break;
21474 }
21475
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021476 /* When there is a line break use what follows for the function body.
21477 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21478 if (*p == '\n')
21479 line_arg = p + 1;
21480 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 EMSG(_(e_trailing));
21482
21483 /*
21484 * Read the body of the function, until ":endfunction" is found.
21485 */
21486 if (KeyTyped)
21487 {
21488 /* Check if the function already exists, don't let the user type the
21489 * whole function before telling him it doesn't work! For a script we
21490 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021491 if (!eap->skip && !eap->forceit)
21492 {
21493 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21494 EMSG(_(e_funcdict));
21495 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021496 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021499 if (!eap->skip && did_emsg)
21500 goto erret;
21501
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 msg_putchar('\n'); /* don't overwrite the function name */
21503 cmdline_row = msg_row;
21504 }
21505
21506 indent = 2;
21507 nesting = 0;
21508 for (;;)
21509 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021510 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021511 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021512 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021513 saved_wait_return = FALSE;
21514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021516 sourcing_lnum_off = sourcing_lnum;
21517
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021518 if (line_arg != NULL)
21519 {
21520 /* Use eap->arg, split up in parts by line breaks. */
21521 theline = line_arg;
21522 p = vim_strchr(theline, '\n');
21523 if (p == NULL)
21524 line_arg += STRLEN(line_arg);
21525 else
21526 {
21527 *p = NUL;
21528 line_arg = p + 1;
21529 }
21530 }
21531 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 theline = getcmdline(':', 0L, indent);
21533 else
21534 theline = eap->getline(':', eap->cookie, indent);
21535 if (KeyTyped)
21536 lines_left = Rows - 1;
21537 if (theline == NULL)
21538 {
21539 EMSG(_("E126: Missing :endfunction"));
21540 goto erret;
21541 }
21542
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021543 /* Detect line continuation: sourcing_lnum increased more than one. */
21544 if (sourcing_lnum > sourcing_lnum_off + 1)
21545 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21546 else
21547 sourcing_lnum_off = 0;
21548
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 if (skip_until != NULL)
21550 {
21551 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21552 * don't check for ":endfunc". */
21553 if (STRCMP(theline, skip_until) == 0)
21554 {
21555 vim_free(skip_until);
21556 skip_until = NULL;
21557 }
21558 }
21559 else
21560 {
21561 /* skip ':' and blanks*/
21562 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21563 ;
21564
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021565 /* Check for "endfunction". */
21566 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021568 if (line_arg == NULL)
21569 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 break;
21571 }
21572
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021573 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 * at "end". */
21575 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21576 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021577 else if (STRNCMP(p, "if", 2) == 0
21578 || STRNCMP(p, "wh", 2) == 0
21579 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 || STRNCMP(p, "try", 3) == 0)
21581 indent += 2;
21582
21583 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021584 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021586 if (*p == '!')
21587 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 p += eval_fname_script(p);
21589 if (ASCII_ISALPHA(*p))
21590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 if (*skipwhite(p) == '(')
21593 {
21594 ++nesting;
21595 indent += 2;
21596 }
21597 }
21598 }
21599
21600 /* Check for ":append" or ":insert". */
21601 p = skip_range(p, NULL);
21602 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21603 || (p[0] == 'i'
21604 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21605 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21606 skip_until = vim_strsave((char_u *)".");
21607
21608 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21609 arg = skipwhite(skiptowhite(p));
21610 if (arg[0] == '<' && arg[1] =='<'
21611 && ((p[0] == 'p' && p[1] == 'y'
21612 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21613 || (p[0] == 'p' && p[1] == 'e'
21614 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21615 || (p[0] == 't' && p[1] == 'c'
21616 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021617 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21618 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21620 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021621 || (p[0] == 'm' && p[1] == 'z'
21622 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 ))
21624 {
21625 /* ":python <<" continues until a dot, like ":append" */
21626 p = skipwhite(arg + 2);
21627 if (*p == NUL)
21628 skip_until = vim_strsave((char_u *)".");
21629 else
21630 skip_until = vim_strsave(p);
21631 }
21632 }
21633
21634 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021635 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021636 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021637 if (line_arg == NULL)
21638 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021640 }
21641
21642 /* Copy the line to newly allocated memory. get_one_sourceline()
21643 * allocates 250 bytes per line, this saves 80% on average. The cost
21644 * is an extra alloc/free. */
21645 p = vim_strsave(theline);
21646 if (p != NULL)
21647 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021648 if (line_arg == NULL)
21649 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021650 theline = p;
21651 }
21652
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021653 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21654
21655 /* Add NULL lines for continuation lines, so that the line count is
21656 * equal to the index in the growarray. */
21657 while (sourcing_lnum_off-- > 0)
21658 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021659
21660 /* Check for end of eap->arg. */
21661 if (line_arg != NULL && *line_arg == NUL)
21662 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 }
21664
21665 /* Don't define the function when skipping commands or when an error was
21666 * detected. */
21667 if (eap->skip || did_emsg)
21668 goto erret;
21669
21670 /*
21671 * If there are no errors, add the function
21672 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021675 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021678 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021679 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021680 goto erret;
21681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021682
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021683 fp = find_func(name);
21684 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021686 if (!eap->forceit)
21687 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021688 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021689 goto erret;
21690 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021691 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021692 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021693 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 name);
21695 goto erret;
21696 }
21697 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021698 ga_clear_strings(&(fp->uf_args));
21699 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700 vim_free(name);
21701 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 }
21704 else
21705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 char numbuf[20];
21707
21708 fp = NULL;
21709 if (fudi.fd_newkey == NULL && !eap->forceit)
21710 {
21711 EMSG(_(e_funcdict));
21712 goto erret;
21713 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021714 if (fudi.fd_di == NULL)
21715 {
21716 /* Can't add a function to a locked dictionary */
21717 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21718 goto erret;
21719 }
21720 /* Can't change an existing function if it is locked */
21721 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21722 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021723
21724 /* Give the function a sequential number. Can only be used with a
21725 * Funcref! */
21726 vim_free(name);
21727 sprintf(numbuf, "%d", ++func_nr);
21728 name = vim_strsave((char_u *)numbuf);
21729 if (name == NULL)
21730 goto erret;
21731 }
21732
21733 if (fp == NULL)
21734 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021735 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021736 {
21737 int slen, plen;
21738 char_u *scriptname;
21739
21740 /* Check that the autoload name matches the script name. */
21741 j = FAIL;
21742 if (sourcing_name != NULL)
21743 {
21744 scriptname = autoload_name(name);
21745 if (scriptname != NULL)
21746 {
21747 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021748 plen = (int)STRLEN(p);
21749 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021750 if (slen > plen && fnamecmp(p,
21751 sourcing_name + slen - plen) == 0)
21752 j = OK;
21753 vim_free(scriptname);
21754 }
21755 }
21756 if (j == FAIL)
21757 {
21758 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21759 goto erret;
21760 }
21761 }
21762
21763 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 if (fp == NULL)
21765 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021766
21767 if (fudi.fd_dict != NULL)
21768 {
21769 if (fudi.fd_di == NULL)
21770 {
21771 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021772 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021773 if (fudi.fd_di == NULL)
21774 {
21775 vim_free(fp);
21776 goto erret;
21777 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021778 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21779 {
21780 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021781 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021782 goto erret;
21783 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021784 }
21785 else
21786 /* overwrite existing dict entry */
21787 clear_tv(&fudi.fd_di->di_tv);
21788 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021789 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021790 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021791 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021792
21793 /* behave like "dict" was used */
21794 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 }
21796
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021798 STRCPY(fp->uf_name, name);
21799 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021801 fp->uf_args = newargs;
21802 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021803#ifdef FEAT_PROFILE
21804 fp->uf_tml_count = NULL;
21805 fp->uf_tml_total = NULL;
21806 fp->uf_tml_self = NULL;
21807 fp->uf_profiling = FALSE;
21808 if (prof_def_func())
21809 func_do_profile(fp);
21810#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021811 fp->uf_varargs = varargs;
21812 fp->uf_flags = flags;
21813 fp->uf_calls = 0;
21814 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021815 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816
21817erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 ga_clear_strings(&newargs);
21819 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021820ret_free:
21821 vim_free(skip_until);
21822 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021825 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826}
21827
21828/*
21829 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021830 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021832 * flags:
21833 * TFN_INT: internal function name OK
21834 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 * Advances "pp" to just after the function name (if no error).
21836 */
21837 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021838trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 char_u **pp;
21840 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021841 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021844 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 char_u *start;
21846 char_u *end;
21847 int lead;
21848 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021850 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021851
21852 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021855
21856 /* Check for hard coded <SNR>: already translated function ID (from a user
21857 * command). */
21858 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21859 && (*pp)[2] == (int)KE_SNR)
21860 {
21861 *pp += 3;
21862 len = get_id_len(pp) + 3;
21863 return vim_strnsave(start, len);
21864 }
21865
21866 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21867 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021869 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021871
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021872 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21873 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021874 if (end == start)
21875 {
21876 if (!skip)
21877 EMSG(_("E129: Function name required"));
21878 goto theend;
21879 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021880 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021881 {
21882 /*
21883 * Report an invalid expression in braces, unless the expression
21884 * evaluation has been cancelled due to an aborting error, an
21885 * interrupt, or an exception.
21886 */
21887 if (!aborting())
21888 {
21889 if (end != NULL)
21890 EMSG2(_(e_invarg2), start);
21891 }
21892 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021893 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021894 goto theend;
21895 }
21896
21897 if (lv.ll_tv != NULL)
21898 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021899 if (fdp != NULL)
21900 {
21901 fdp->fd_dict = lv.ll_dict;
21902 fdp->fd_newkey = lv.ll_newkey;
21903 lv.ll_newkey = NULL;
21904 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021905 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021906 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21907 {
21908 name = vim_strsave(lv.ll_tv->vval.v_string);
21909 *pp = end;
21910 }
21911 else
21912 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021913 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21914 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021915 EMSG(_(e_funcref));
21916 else
21917 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021918 name = NULL;
21919 }
21920 goto theend;
21921 }
21922
21923 if (lv.ll_name == NULL)
21924 {
21925 /* Error found, but continue after the function name. */
21926 *pp = end;
21927 goto theend;
21928 }
21929
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021930 /* Check if the name is a Funcref. If so, use the value. */
21931 if (lv.ll_exp_name != NULL)
21932 {
21933 len = (int)STRLEN(lv.ll_exp_name);
21934 name = deref_func_name(lv.ll_exp_name, &len);
21935 if (name == lv.ll_exp_name)
21936 name = NULL;
21937 }
21938 else
21939 {
21940 len = (int)(end - *pp);
21941 name = deref_func_name(*pp, &len);
21942 if (name == *pp)
21943 name = NULL;
21944 }
21945 if (name != NULL)
21946 {
21947 name = vim_strsave(name);
21948 *pp = end;
21949 goto theend;
21950 }
21951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021952 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021953 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021954 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021955 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21956 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21957 {
21958 /* When there was "s:" already or the name expanded to get a
21959 * leading "s:" then remove it. */
21960 lv.ll_name += 2;
21961 len -= 2;
21962 lead = 2;
21963 }
21964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021965 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021966 {
21967 if (lead == 2) /* skip over "s:" */
21968 lv.ll_name += 2;
21969 len = (int)(end - lv.ll_name);
21970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021971
21972 /*
21973 * Copy the function name to allocated memory.
21974 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21975 * Accept <SNR>123_name() outside a script.
21976 */
21977 if (skip)
21978 lead = 0; /* do nothing */
21979 else if (lead > 0)
21980 {
21981 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021982 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21983 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021984 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021985 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021986 if (current_SID <= 0)
21987 {
21988 EMSG(_(e_usingsid));
21989 goto theend;
21990 }
21991 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21992 lead += (int)STRLEN(sid_buf);
21993 }
21994 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021995 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021996 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021997 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021998 goto theend;
21999 }
22000 name = alloc((unsigned)(len + lead + 1));
22001 if (name != NULL)
22002 {
22003 if (lead > 0)
22004 {
22005 name[0] = K_SPECIAL;
22006 name[1] = KS_EXTRA;
22007 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022008 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022009 STRCPY(name + 3, sid_buf);
22010 }
22011 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22012 name[len + lead] = NUL;
22013 }
22014 *pp = end;
22015
22016theend:
22017 clear_lval(&lv);
22018 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019}
22020
22021/*
22022 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22023 * Return 2 if "p" starts with "s:".
22024 * Return 0 otherwise.
22025 */
22026 static int
22027eval_fname_script(p)
22028 char_u *p;
22029{
22030 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22031 || STRNICMP(p + 1, "SNR>", 4) == 0))
22032 return 5;
22033 if (p[0] == 's' && p[1] == ':')
22034 return 2;
22035 return 0;
22036}
22037
22038/*
22039 * Return TRUE if "p" starts with "<SID>" or "s:".
22040 * Only works if eval_fname_script() returned non-zero for "p"!
22041 */
22042 static int
22043eval_fname_sid(p)
22044 char_u *p;
22045{
22046 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22047}
22048
22049/*
22050 * List the head of the function: "name(arg1, arg2)".
22051 */
22052 static void
22053list_func_head(fp, indent)
22054 ufunc_T *fp;
22055 int indent;
22056{
22057 int j;
22058
22059 msg_start();
22060 if (indent)
22061 MSG_PUTS(" ");
22062 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022063 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 {
22065 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022066 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 }
22068 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022069 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022071 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 {
22073 if (j)
22074 MSG_PUTS(", ");
22075 msg_puts(FUNCARG(fp, j));
22076 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022077 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 {
22079 if (j)
22080 MSG_PUTS(", ");
22081 MSG_PUTS("...");
22082 }
22083 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022084 if (fp->uf_flags & FC_ABORT)
22085 MSG_PUTS(" abort");
22086 if (fp->uf_flags & FC_RANGE)
22087 MSG_PUTS(" range");
22088 if (fp->uf_flags & FC_DICT)
22089 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022090 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022091 if (p_verbose > 0)
22092 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093}
22094
22095/*
22096 * Find a function by name, return pointer to it in ufuncs.
22097 * Return NULL for unknown function.
22098 */
22099 static ufunc_T *
22100find_func(name)
22101 char_u *name;
22102{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022103 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022105 hi = hash_find(&func_hashtab, name);
22106 if (!HASHITEM_EMPTY(hi))
22107 return HI2UF(hi);
22108 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109}
22110
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022111#if defined(EXITFREE) || defined(PROTO)
22112 void
22113free_all_functions()
22114{
22115 hashitem_T *hi;
22116
22117 /* Need to start all over every time, because func_free() may change the
22118 * hash table. */
22119 while (func_hashtab.ht_used > 0)
22120 for (hi = func_hashtab.ht_array; ; ++hi)
22121 if (!HASHITEM_EMPTY(hi))
22122 {
22123 func_free(HI2UF(hi));
22124 break;
22125 }
22126}
22127#endif
22128
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022129 int
22130translated_function_exists(name)
22131 char_u *name;
22132{
22133 if (builtin_function(name))
22134 return find_internal_func(name) >= 0;
22135 return find_func(name) != NULL;
22136}
22137
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022138/*
22139 * Return TRUE if a function "name" exists.
22140 */
22141 static int
22142function_exists(name)
22143 char_u *name;
22144{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022145 char_u *nm = name;
22146 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022147 int n = FALSE;
22148
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022149 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022150 nm = skipwhite(nm);
22151
22152 /* Only accept "funcname", "funcname ", "funcname (..." and
22153 * "funcname(...", not "funcname!...". */
22154 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022155 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022156 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 return n;
22158}
22159
Bram Moolenaara1544c02013-05-30 12:35:52 +020022160 char_u *
22161get_expanded_name(name, check)
22162 char_u *name;
22163 int check;
22164{
22165 char_u *nm = name;
22166 char_u *p;
22167
22168 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22169
22170 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022171 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022172 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022173
Bram Moolenaara1544c02013-05-30 12:35:52 +020022174 vim_free(p);
22175 return NULL;
22176}
22177
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022178/*
22179 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022180 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022181 */
22182 static int
22183builtin_function(name)
22184 char_u *name;
22185{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022186 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22187 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022188}
22189
Bram Moolenaar05159a02005-02-26 23:04:13 +000022190#if defined(FEAT_PROFILE) || defined(PROTO)
22191/*
22192 * Start profiling function "fp".
22193 */
22194 static void
22195func_do_profile(fp)
22196 ufunc_T *fp;
22197{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022198 int len = fp->uf_lines.ga_len;
22199
22200 if (len == 0)
22201 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022202 fp->uf_tm_count = 0;
22203 profile_zero(&fp->uf_tm_self);
22204 profile_zero(&fp->uf_tm_total);
22205 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022206 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022207 if (fp->uf_tml_total == NULL)
22208 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022209 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022210 if (fp->uf_tml_self == NULL)
22211 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022212 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022213 fp->uf_tml_idx = -1;
22214 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22215 || fp->uf_tml_self == NULL)
22216 return; /* out of memory */
22217
22218 fp->uf_profiling = TRUE;
22219}
22220
22221/*
22222 * Dump the profiling results for all functions in file "fd".
22223 */
22224 void
22225func_dump_profile(fd)
22226 FILE *fd;
22227{
22228 hashitem_T *hi;
22229 int todo;
22230 ufunc_T *fp;
22231 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022232 ufunc_T **sorttab;
22233 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022234
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022235 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022236 if (todo == 0)
22237 return; /* nothing to dump */
22238
Bram Moolenaar73830342005-02-28 22:48:19 +000022239 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22240
Bram Moolenaar05159a02005-02-26 23:04:13 +000022241 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22242 {
22243 if (!HASHITEM_EMPTY(hi))
22244 {
22245 --todo;
22246 fp = HI2UF(hi);
22247 if (fp->uf_profiling)
22248 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022249 if (sorttab != NULL)
22250 sorttab[st_len++] = fp;
22251
Bram Moolenaar05159a02005-02-26 23:04:13 +000022252 if (fp->uf_name[0] == K_SPECIAL)
22253 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22254 else
22255 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22256 if (fp->uf_tm_count == 1)
22257 fprintf(fd, "Called 1 time\n");
22258 else
22259 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22260 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22261 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22262 fprintf(fd, "\n");
22263 fprintf(fd, "count total (s) self (s)\n");
22264
22265 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22266 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022267 if (FUNCLINE(fp, i) == NULL)
22268 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022269 prof_func_line(fd, fp->uf_tml_count[i],
22270 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022271 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22272 }
22273 fprintf(fd, "\n");
22274 }
22275 }
22276 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022277
22278 if (sorttab != NULL && st_len > 0)
22279 {
22280 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22281 prof_total_cmp);
22282 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22283 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22284 prof_self_cmp);
22285 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22286 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022287
22288 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022289}
Bram Moolenaar73830342005-02-28 22:48:19 +000022290
22291 static void
22292prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22293 FILE *fd;
22294 ufunc_T **sorttab;
22295 int st_len;
22296 char *title;
22297 int prefer_self; /* when equal print only self time */
22298{
22299 int i;
22300 ufunc_T *fp;
22301
22302 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22303 fprintf(fd, "count total (s) self (s) function\n");
22304 for (i = 0; i < 20 && i < st_len; ++i)
22305 {
22306 fp = sorttab[i];
22307 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22308 prefer_self);
22309 if (fp->uf_name[0] == K_SPECIAL)
22310 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22311 else
22312 fprintf(fd, " %s()\n", fp->uf_name);
22313 }
22314 fprintf(fd, "\n");
22315}
22316
22317/*
22318 * Print the count and times for one function or function line.
22319 */
22320 static void
22321prof_func_line(fd, count, total, self, prefer_self)
22322 FILE *fd;
22323 int count;
22324 proftime_T *total;
22325 proftime_T *self;
22326 int prefer_self; /* when equal print only self time */
22327{
22328 if (count > 0)
22329 {
22330 fprintf(fd, "%5d ", count);
22331 if (prefer_self && profile_equal(total, self))
22332 fprintf(fd, " ");
22333 else
22334 fprintf(fd, "%s ", profile_msg(total));
22335 if (!prefer_self && profile_equal(total, self))
22336 fprintf(fd, " ");
22337 else
22338 fprintf(fd, "%s ", profile_msg(self));
22339 }
22340 else
22341 fprintf(fd, " ");
22342}
22343
22344/*
22345 * Compare function for total time sorting.
22346 */
22347 static int
22348#ifdef __BORLANDC__
22349_RTLENTRYF
22350#endif
22351prof_total_cmp(s1, s2)
22352 const void *s1;
22353 const void *s2;
22354{
22355 ufunc_T *p1, *p2;
22356
22357 p1 = *(ufunc_T **)s1;
22358 p2 = *(ufunc_T **)s2;
22359 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22360}
22361
22362/*
22363 * Compare function for self time sorting.
22364 */
22365 static int
22366#ifdef __BORLANDC__
22367_RTLENTRYF
22368#endif
22369prof_self_cmp(s1, s2)
22370 const void *s1;
22371 const void *s2;
22372{
22373 ufunc_T *p1, *p2;
22374
22375 p1 = *(ufunc_T **)s1;
22376 p2 = *(ufunc_T **)s2;
22377 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22378}
22379
Bram Moolenaar05159a02005-02-26 23:04:13 +000022380#endif
22381
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022382/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022383 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022384 * Return TRUE if a package was loaded.
22385 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022386 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022387script_autoload(name, reload)
22388 char_u *name;
22389 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022390{
22391 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022392 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022393 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022394 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022395
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022396 /* Return quickly when autoload disabled. */
22397 if (no_autoload)
22398 return FALSE;
22399
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022400 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022401 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022402 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022403 return FALSE;
22404
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022405 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022406
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022407 /* Find the name in the list of previously loaded package names. Skip
22408 * "autoload/", it's always the same. */
22409 for (i = 0; i < ga_loaded.ga_len; ++i)
22410 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22411 break;
22412 if (!reload && i < ga_loaded.ga_len)
22413 ret = FALSE; /* was loaded already */
22414 else
22415 {
22416 /* Remember the name if it wasn't loaded already. */
22417 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22418 {
22419 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22420 tofree = NULL;
22421 }
22422
22423 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022424 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022425 ret = TRUE;
22426 }
22427
22428 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022429 return ret;
22430}
22431
22432/*
22433 * Return the autoload script name for a function or variable name.
22434 * Returns NULL when out of memory.
22435 */
22436 static char_u *
22437autoload_name(name)
22438 char_u *name;
22439{
22440 char_u *p;
22441 char_u *scriptname;
22442
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022443 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022444 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22445 if (scriptname == NULL)
22446 return FALSE;
22447 STRCPY(scriptname, "autoload/");
22448 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022449 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022450 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022451 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022452 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022453 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022454}
22455
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22457
22458/*
22459 * Function given to ExpandGeneric() to obtain the list of user defined
22460 * function names.
22461 */
22462 char_u *
22463get_user_func_name(xp, idx)
22464 expand_T *xp;
22465 int idx;
22466{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022467 static long_u done;
22468 static hashitem_T *hi;
22469 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470
22471 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022473 done = 0;
22474 hi = func_hashtab.ht_array;
22475 }
22476 if (done < func_hashtab.ht_used)
22477 {
22478 if (done++ > 0)
22479 ++hi;
22480 while (HASHITEM_EMPTY(hi))
22481 ++hi;
22482 fp = HI2UF(hi);
22483
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022484 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022485 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022486
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022487 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22488 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489
22490 cat_func_name(IObuff, fp);
22491 if (xp->xp_context != EXPAND_USER_FUNC)
22492 {
22493 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 STRCAT(IObuff, ")");
22496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 return IObuff;
22498 }
22499 return NULL;
22500}
22501
22502#endif /* FEAT_CMDL_COMPL */
22503
22504/*
22505 * Copy the function name of "fp" to buffer "buf".
22506 * "buf" must be able to hold the function name plus three bytes.
22507 * Takes care of script-local function names.
22508 */
22509 static void
22510cat_func_name(buf, fp)
22511 char_u *buf;
22512 ufunc_T *fp;
22513{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 {
22516 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022517 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 }
22519 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022520 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521}
22522
22523/*
22524 * ":delfunction {name}"
22525 */
22526 void
22527ex_delfunction(eap)
22528 exarg_T *eap;
22529{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022530 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 char_u *p;
22532 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534
22535 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022536 name = trans_function_name(&p, eap->skip, 0, &fudi);
22537 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539 {
22540 if (fudi.fd_dict != NULL && !eap->skip)
22541 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 if (!ends_excmd(*skipwhite(p)))
22545 {
22546 vim_free(name);
22547 EMSG(_(e_trailing));
22548 return;
22549 }
22550 eap->nextcmd = check_nextcmd(p);
22551 if (eap->nextcmd != NULL)
22552 *p = NUL;
22553
22554 if (!eap->skip)
22555 fp = find_func(name);
22556 vim_free(name);
22557
22558 if (!eap->skip)
22559 {
22560 if (fp == NULL)
22561 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022562 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 return;
22564 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022565 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 {
22567 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22568 return;
22569 }
22570
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022571 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022573 /* Delete the dict item that refers to the function, it will
22574 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022575 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022577 else
22578 func_free(fp);
22579 }
22580}
22581
22582/*
22583 * Free a function and remove it from the list of functions.
22584 */
22585 static void
22586func_free(fp)
22587 ufunc_T *fp;
22588{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022589 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022590
22591 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022592 ga_clear_strings(&(fp->uf_args));
22593 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022594#ifdef FEAT_PROFILE
22595 vim_free(fp->uf_tml_count);
22596 vim_free(fp->uf_tml_total);
22597 vim_free(fp->uf_tml_self);
22598#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022599
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 /* remove the function from the function hashtable */
22601 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22602 if (HASHITEM_EMPTY(hi))
22603 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022605 hash_remove(&func_hashtab, hi);
22606
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022607 vim_free(fp);
22608}
22609
22610/*
22611 * Unreference a Function: decrement the reference count and free it when it
22612 * becomes zero. Only for numbered functions.
22613 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022614 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615func_unref(name)
22616 char_u *name;
22617{
22618 ufunc_T *fp;
22619
22620 if (name != NULL && isdigit(*name))
22621 {
22622 fp = find_func(name);
22623 if (fp == NULL)
22624 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022625 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022626 {
22627 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022628 * when "uf_calls" becomes zero. */
22629 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 func_free(fp);
22631 }
22632 }
22633}
22634
22635/*
22636 * Count a reference to a Function.
22637 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022638 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022639func_ref(name)
22640 char_u *name;
22641{
22642 ufunc_T *fp;
22643
22644 if (name != NULL && isdigit(*name))
22645 {
22646 fp = find_func(name);
22647 if (fp == NULL)
22648 EMSG2(_(e_intern2), "func_ref()");
22649 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022650 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 }
22652}
22653
22654/*
22655 * Call a user function.
22656 */
22657 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022658call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 ufunc_T *fp; /* pointer to function */
22660 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 typval_T *argvars; /* arguments */
22662 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 linenr_T firstline; /* first line of range */
22664 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666{
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 char_u *save_sourcing_name;
22668 linenr_T save_sourcing_lnum;
22669 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022670 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 int save_did_emsg;
22672 static int depth = 0;
22673 dictitem_T *v;
22674 int fixvar_idx = 0; /* index in fixvar[] */
22675 int i;
22676 int ai;
22677 char_u numbuf[NUMBUFLEN];
22678 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022679#ifdef FEAT_PROFILE
22680 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022681 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683
22684 /* If depth of calling is getting too high, don't execute the function */
22685 if (depth >= p_mfd)
22686 {
22687 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022688 rettv->v_type = VAR_NUMBER;
22689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 return;
22691 }
22692 ++depth;
22693
22694 line_breakcheck(); /* check for CTRL-C hit */
22695
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022696 fc = (funccall_T *)alloc(sizeof(funccall_T));
22697 fc->caller = current_funccal;
22698 current_funccal = fc;
22699 fc->func = fp;
22700 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022701 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022702 fc->linenr = 0;
22703 fc->returned = FALSE;
22704 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022706 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22707 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022710 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22712 * each argument variable and saves a lot of time.
22713 */
22714 /*
22715 * Init l: variables.
22716 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022717 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022718 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022719 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022720 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22721 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022722 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022723 name = v->di_key;
22724 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022725 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022726 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022728 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 v->di_tv.vval.v_dict = selfdict;
22730 ++selfdict->dv_refcount;
22731 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022732
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 /*
22734 * Init a: variables.
22735 * Set a:0 to "argcount".
22736 * Set a:000 to a list with room for the "..." arguments.
22737 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022738 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022739 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022740 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022741 /* Use "name" to avoid a warning from some compiler that checks the
22742 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022743 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022744 name = v->di_key;
22745 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022747 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022749 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022750 v->di_tv.vval.v_list = &fc->l_varlist;
22751 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22752 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22753 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022754
22755 /*
22756 * Set a:firstline to "firstline" and a:lastline to "lastline".
22757 * Set a:name to named arguments.
22758 * Set a:N to the "..." arguments.
22759 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022760 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022762 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 (varnumber_T)lastline);
22764 for (i = 0; i < argcount; ++i)
22765 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022766 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 if (ai < 0)
22768 /* named argument a:name */
22769 name = FUNCARG(fp, i);
22770 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022771 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 /* "..." argument a:1, a:2, etc. */
22773 sprintf((char *)numbuf, "%d", ai + 1);
22774 name = numbuf;
22775 }
22776 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22777 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022778 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22780 }
22781 else
22782 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022783 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22784 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 if (v == NULL)
22786 break;
22787 v->di_flags = DI_FLAGS_RO;
22788 }
22789 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022790 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022791
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022792 /* Note: the values are copied directly to avoid alloc/free.
22793 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022795 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022796
22797 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22798 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022799 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22800 fc->l_listitems[ai].li_tv = argvars[i];
22801 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022802 }
22803 }
22804
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 /* Don't redraw while executing the function. */
22806 ++RedrawingDisabled;
22807 save_sourcing_name = sourcing_name;
22808 save_sourcing_lnum = sourcing_lnum;
22809 sourcing_lnum = 1;
22810 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 if (sourcing_name != NULL)
22813 {
22814 if (save_sourcing_name != NULL
22815 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22816 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22817 else
22818 STRCPY(sourcing_name, "function ");
22819 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22820
22821 if (p_verbose >= 12)
22822 {
22823 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022824 verbose_enter_scroll();
22825
Bram Moolenaar555b2802005-05-19 21:08:39 +000022826 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 if (p_verbose >= 14)
22828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022830 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022831 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022832 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833
22834 msg_puts((char_u *)"(");
22835 for (i = 0; i < argcount; ++i)
22836 {
22837 if (i > 0)
22838 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022839 if (argvars[i].v_type == VAR_NUMBER)
22840 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 else
22842 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022843 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22844 if (s != NULL)
22845 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022846 if (vim_strsize(s) > MSG_BUF_CLEN)
22847 {
22848 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22849 s = buf;
22850 }
22851 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022852 vim_free(tofree);
22853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 }
22855 }
22856 msg_puts((char_u *)")");
22857 }
22858 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022859
22860 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 --no_wait_return;
22862 }
22863 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022864#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022865 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022866 {
22867 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22868 func_do_profile(fp);
22869 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022870 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022871 {
22872 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022873 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022874 profile_zero(&fp->uf_tm_children);
22875 }
22876 script_prof_save(&wait_start);
22877 }
22878#endif
22879
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022881 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 save_did_emsg = did_emsg;
22883 did_emsg = FALSE;
22884
22885 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022886 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22888
22889 --RedrawingDisabled;
22890
22891 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022892 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022894 clear_tv(rettv);
22895 rettv->v_type = VAR_NUMBER;
22896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 }
22898
Bram Moolenaar05159a02005-02-26 23:04:13 +000022899#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022900 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022901 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022902 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022903 profile_end(&call_start);
22904 profile_sub_wait(&wait_start, &call_start);
22905 profile_add(&fp->uf_tm_total, &call_start);
22906 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022907 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022908 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022909 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22910 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022911 }
22912 }
22913#endif
22914
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 /* when being verbose, mention the return value */
22916 if (p_verbose >= 12)
22917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022919 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022922 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022923 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022924 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022928 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022929 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022930 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022931 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022932
Bram Moolenaar555b2802005-05-19 21:08:39 +000022933 /* The value may be very long. Skip the middle part, so that we
22934 * have some idea how it starts and ends. smsg() would always
22935 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022936 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022937 if (s != NULL)
22938 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022939 if (vim_strsize(s) > MSG_BUF_CLEN)
22940 {
22941 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22942 s = buf;
22943 }
22944 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022945 vim_free(tofree);
22946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 }
22948 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022949
22950 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 --no_wait_return;
22952 }
22953
22954 vim_free(sourcing_name);
22955 sourcing_name = save_sourcing_name;
22956 sourcing_lnum = save_sourcing_lnum;
22957 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022958#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022959 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022960 script_prof_restore(&wait_start);
22961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962
22963 if (p_verbose >= 12 && sourcing_name != NULL)
22964 {
22965 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022966 verbose_enter_scroll();
22967
Bram Moolenaar555b2802005-05-19 21:08:39 +000022968 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022970
22971 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 --no_wait_return;
22973 }
22974
22975 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022976 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022978
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022979 /* If the a:000 list and the l: and a: dicts are not referenced we can
22980 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022981 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22982 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22983 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22984 {
22985 free_funccal(fc, FALSE);
22986 }
22987 else
22988 {
22989 hashitem_T *hi;
22990 listitem_T *li;
22991 int todo;
22992
22993 /* "fc" is still in use. This can happen when returning "a:000" or
22994 * assigning "l:" to a global variable.
22995 * Link "fc" in the list for garbage collection later. */
22996 fc->caller = previous_funccal;
22997 previous_funccal = fc;
22998
22999 /* Make a copy of the a: variables, since we didn't do that above. */
23000 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23001 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23002 {
23003 if (!HASHITEM_EMPTY(hi))
23004 {
23005 --todo;
23006 v = HI2DI(hi);
23007 copy_tv(&v->di_tv, &v->di_tv);
23008 }
23009 }
23010
23011 /* Make a copy of the a:000 items, since we didn't do that above. */
23012 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23013 copy_tv(&li->li_tv, &li->li_tv);
23014 }
23015}
23016
23017/*
23018 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023019 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023020 */
23021 static int
23022can_free_funccal(fc, copyID)
23023 funccall_T *fc;
23024 int copyID;
23025{
23026 return (fc->l_varlist.lv_copyID != copyID
23027 && fc->l_vars.dv_copyID != copyID
23028 && fc->l_avars.dv_copyID != copyID);
23029}
23030
23031/*
23032 * Free "fc" and what it contains.
23033 */
23034 static void
23035free_funccal(fc, free_val)
23036 funccall_T *fc;
23037 int free_val; /* a: vars were allocated */
23038{
23039 listitem_T *li;
23040
23041 /* The a: variables typevals may not have been allocated, only free the
23042 * allocated variables. */
23043 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23044
23045 /* free all l: variables */
23046 vars_clear(&fc->l_vars.dv_hashtab);
23047
23048 /* Free the a:000 variables if they were allocated. */
23049 if (free_val)
23050 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23051 clear_tv(&li->li_tv);
23052
23053 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054}
23055
23056/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023057 * Add a number variable "name" to dict "dp" with value "nr".
23058 */
23059 static void
23060add_nr_var(dp, v, name, nr)
23061 dict_T *dp;
23062 dictitem_T *v;
23063 char *name;
23064 varnumber_T nr;
23065{
23066 STRCPY(v->di_key, name);
23067 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23068 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23069 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023070 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 v->di_tv.vval.v_number = nr;
23072}
23073
23074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 * ":return [expr]"
23076 */
23077 void
23078ex_return(eap)
23079 exarg_T *eap;
23080{
23081 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023082 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 int returning = FALSE;
23084
23085 if (current_funccal == NULL)
23086 {
23087 EMSG(_("E133: :return not inside a function"));
23088 return;
23089 }
23090
23091 if (eap->skip)
23092 ++emsg_skip;
23093
23094 eap->nextcmd = NULL;
23095 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023096 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 {
23098 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023099 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
23103 /* It's safer to return also on error. */
23104 else if (!eap->skip)
23105 {
23106 /*
23107 * Return unless the expression evaluation has been cancelled due to an
23108 * aborting error, an interrupt, or an exception.
23109 */
23110 if (!aborting())
23111 returning = do_return(eap, FALSE, TRUE, NULL);
23112 }
23113
23114 /* When skipping or the return gets pending, advance to the next command
23115 * in this line (!returning). Otherwise, ignore the rest of the line.
23116 * Following lines will be ignored by get_func_line(). */
23117 if (returning)
23118 eap->nextcmd = NULL;
23119 else if (eap->nextcmd == NULL) /* no argument */
23120 eap->nextcmd = check_nextcmd(arg);
23121
23122 if (eap->skip)
23123 --emsg_skip;
23124}
23125
23126/*
23127 * Return from a function. Possibly makes the return pending. Also called
23128 * for a pending return at the ":endtry" or after returning from an extra
23129 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023130 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023131 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 * FALSE when the return gets pending.
23133 */
23134 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023135do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 exarg_T *eap;
23137 int reanimate;
23138 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023139 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140{
23141 int idx;
23142 struct condstack *cstack = eap->cstack;
23143
23144 if (reanimate)
23145 /* Undo the return. */
23146 current_funccal->returned = FALSE;
23147
23148 /*
23149 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23150 * not in its finally clause (which then is to be executed next) is found.
23151 * In this case, make the ":return" pending for execution at the ":endtry".
23152 * Otherwise, return normally.
23153 */
23154 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23155 if (idx >= 0)
23156 {
23157 cstack->cs_pending[idx] = CSTP_RETURN;
23158
23159 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023160 /* A pending return again gets pending. "rettv" points to an
23161 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023163 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 else
23165 {
23166 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023167 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023169 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023171 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 {
23173 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023174 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023175 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 else
23177 EMSG(_(e_outofmem));
23178 }
23179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023180 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181
23182 if (reanimate)
23183 {
23184 /* The pending return value could be overwritten by a ":return"
23185 * without argument in a finally clause; reset the default
23186 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 current_funccal->rettv->v_type = VAR_NUMBER;
23188 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 }
23190 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023191 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192 }
23193 else
23194 {
23195 current_funccal->returned = TRUE;
23196
23197 /* If the return is carried out now, store the return value. For
23198 * a return immediately after reanimation, the value is already
23199 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023200 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023202 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023205 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 }
23207 }
23208
23209 return idx < 0;
23210}
23211
23212/*
23213 * Free the variable with a pending return value.
23214 */
23215 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023216discard_pending_return(rettv)
23217 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218{
Bram Moolenaar33570922005-01-25 22:26:29 +000023219 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220}
23221
23222/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023223 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 * is an allocated string. Used by report_pending() for verbose messages.
23225 */
23226 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023227get_return_cmd(rettv)
23228 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023230 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023231 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023232 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023234 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023235 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023236 if (s == NULL)
23237 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023238
23239 STRCPY(IObuff, ":return ");
23240 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23241 if (STRLEN(s) + 8 >= IOSIZE)
23242 STRCPY(IObuff + IOSIZE - 4, "...");
23243 vim_free(tofree);
23244 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245}
23246
23247/*
23248 * Get next function line.
23249 * Called by do_cmdline() to get the next line.
23250 * Returns allocated string, or NULL for end of function.
23251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 char_u *
23253get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023254 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023256 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257{
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259 ufunc_T *fp = fcp->func;
23260 char_u *retval;
23261 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262
23263 /* If breakpoints have been added/deleted need to check for it. */
23264 if (fcp->dbg_tick != debug_tick)
23265 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023266 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 sourcing_lnum);
23268 fcp->dbg_tick = debug_tick;
23269 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023270#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023271 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023272 func_line_end(cookie);
23273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274
Bram Moolenaar05159a02005-02-26 23:04:13 +000023275 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023276 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23277 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 retval = NULL;
23279 else
23280 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023281 /* Skip NULL lines (continuation lines). */
23282 while (fcp->linenr < gap->ga_len
23283 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23284 ++fcp->linenr;
23285 if (fcp->linenr >= gap->ga_len)
23286 retval = NULL;
23287 else
23288 {
23289 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23290 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023291#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023292 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023293 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023294#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 }
23297
23298 /* Did we encounter a breakpoint? */
23299 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23300 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023301 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023303 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 sourcing_lnum);
23305 fcp->dbg_tick = debug_tick;
23306 }
23307
23308 return retval;
23309}
23310
Bram Moolenaar05159a02005-02-26 23:04:13 +000023311#if defined(FEAT_PROFILE) || defined(PROTO)
23312/*
23313 * Called when starting to read a function line.
23314 * "sourcing_lnum" must be correct!
23315 * When skipping lines it may not actually be executed, but we won't find out
23316 * until later and we need to store the time now.
23317 */
23318 void
23319func_line_start(cookie)
23320 void *cookie;
23321{
23322 funccall_T *fcp = (funccall_T *)cookie;
23323 ufunc_T *fp = fcp->func;
23324
23325 if (fp->uf_profiling && sourcing_lnum >= 1
23326 && sourcing_lnum <= fp->uf_lines.ga_len)
23327 {
23328 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023329 /* Skip continuation lines. */
23330 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23331 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023332 fp->uf_tml_execed = FALSE;
23333 profile_start(&fp->uf_tml_start);
23334 profile_zero(&fp->uf_tml_children);
23335 profile_get_wait(&fp->uf_tml_wait);
23336 }
23337}
23338
23339/*
23340 * Called when actually executing a function line.
23341 */
23342 void
23343func_line_exec(cookie)
23344 void *cookie;
23345{
23346 funccall_T *fcp = (funccall_T *)cookie;
23347 ufunc_T *fp = fcp->func;
23348
23349 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23350 fp->uf_tml_execed = TRUE;
23351}
23352
23353/*
23354 * Called when done with a function line.
23355 */
23356 void
23357func_line_end(cookie)
23358 void *cookie;
23359{
23360 funccall_T *fcp = (funccall_T *)cookie;
23361 ufunc_T *fp = fcp->func;
23362
23363 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23364 {
23365 if (fp->uf_tml_execed)
23366 {
23367 ++fp->uf_tml_count[fp->uf_tml_idx];
23368 profile_end(&fp->uf_tml_start);
23369 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023370 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023371 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23372 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023373 }
23374 fp->uf_tml_idx = -1;
23375 }
23376}
23377#endif
23378
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379/*
23380 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023381 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 */
23383 int
23384func_has_ended(cookie)
23385 void *cookie;
23386{
Bram Moolenaar33570922005-01-25 22:26:29 +000023387 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388
23389 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23390 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023391 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 || fcp->returned);
23393}
23394
23395/*
23396 * return TRUE if cookie indicates a function which "abort"s on errors.
23397 */
23398 int
23399func_has_abort(cookie)
23400 void *cookie;
23401{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023402 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403}
23404
23405#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23406typedef enum
23407{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023408 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23409 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23410 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411} var_flavour_T;
23412
23413static var_flavour_T var_flavour __ARGS((char_u *varname));
23414
23415 static var_flavour_T
23416var_flavour(varname)
23417 char_u *varname;
23418{
23419 char_u *p = varname;
23420
23421 if (ASCII_ISUPPER(*p))
23422 {
23423 while (*(++p))
23424 if (ASCII_ISLOWER(*p))
23425 return VAR_FLAVOUR_SESSION;
23426 return VAR_FLAVOUR_VIMINFO;
23427 }
23428 else
23429 return VAR_FLAVOUR_DEFAULT;
23430}
23431#endif
23432
23433#if defined(FEAT_VIMINFO) || defined(PROTO)
23434/*
23435 * Restore global vars that start with a capital from the viminfo file
23436 */
23437 int
23438read_viminfo_varlist(virp, writing)
23439 vir_T *virp;
23440 int writing;
23441{
23442 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023443 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023444 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445
23446 if (!writing && (find_viminfo_parameter('!') != NULL))
23447 {
23448 tab = vim_strchr(virp->vir_line + 1, '\t');
23449 if (tab != NULL)
23450 {
23451 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023452 switch (*tab)
23453 {
23454 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023455#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023456 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023457#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023458 case 'D': type = VAR_DICT; break;
23459 case 'L': type = VAR_LIST; break;
23460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461
23462 tab = vim_strchr(tab, '\t');
23463 if (tab != NULL)
23464 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023465 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023466 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023467 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023469#ifdef FEAT_FLOAT
23470 else if (type == VAR_FLOAT)
23471 (void)string2float(tab + 1, &tv.vval.v_float);
23472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023474 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023475 if (type == VAR_DICT || type == VAR_LIST)
23476 {
23477 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23478
23479 if (etv == NULL)
23480 /* Failed to parse back the dict or list, use it as a
23481 * string. */
23482 tv.v_type = VAR_STRING;
23483 else
23484 {
23485 vim_free(tv.vval.v_string);
23486 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023487 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023488 }
23489 }
23490
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023491 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023492
23493 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023494 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023495 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23496 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 }
23498 }
23499 }
23500
23501 return viminfo_readline(virp);
23502}
23503
23504/*
23505 * Write global vars that start with a capital to the viminfo file
23506 */
23507 void
23508write_viminfo_varlist(fp)
23509 FILE *fp;
23510{
Bram Moolenaar33570922005-01-25 22:26:29 +000023511 hashitem_T *hi;
23512 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023513 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023514 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023515 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023516 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023517 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518
23519 if (find_viminfo_parameter('!') == NULL)
23520 return;
23521
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023522 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023523
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023524 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023525 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023527 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023529 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023530 this_var = HI2DI(hi);
23531 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023532 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023533 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023534 {
23535 case VAR_STRING: s = "STR"; break;
23536 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023537#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023538 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023539#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023540 case VAR_DICT: s = "DIC"; break;
23541 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023542 default: continue;
23543 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023544 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023545 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023546 if (p != NULL)
23547 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023548 vim_free(tofree);
23549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 }
23551 }
23552}
23553#endif
23554
23555#if defined(FEAT_SESSION) || defined(PROTO)
23556 int
23557store_session_globals(fd)
23558 FILE *fd;
23559{
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 hashitem_T *hi;
23561 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023562 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 char_u *p, *t;
23564
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023565 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023568 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023570 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023571 this_var = HI2DI(hi);
23572 if ((this_var->di_tv.v_type == VAR_NUMBER
23573 || this_var->di_tv.v_type == VAR_STRING)
23574 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023575 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023576 /* Escape special characters with a backslash. Turn a LF and
23577 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023579 (char_u *)"\\\"\n\r");
23580 if (p == NULL) /* out of memory */
23581 break;
23582 for (t = p; *t != NUL; ++t)
23583 if (*t == '\n')
23584 *t = 'n';
23585 else if (*t == '\r')
23586 *t = 'r';
23587 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023588 this_var->di_key,
23589 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23590 : ' ',
23591 p,
23592 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23593 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023594 || put_eol(fd) == FAIL)
23595 {
23596 vim_free(p);
23597 return FAIL;
23598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 vim_free(p);
23600 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023601#ifdef FEAT_FLOAT
23602 else if (this_var->di_tv.v_type == VAR_FLOAT
23603 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23604 {
23605 float_T f = this_var->di_tv.vval.v_float;
23606 int sign = ' ';
23607
23608 if (f < 0)
23609 {
23610 f = -f;
23611 sign = '-';
23612 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023613 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023614 this_var->di_key, sign, f) < 0)
23615 || put_eol(fd) == FAIL)
23616 return FAIL;
23617 }
23618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 }
23620 }
23621 return OK;
23622}
23623#endif
23624
Bram Moolenaar661b1822005-07-28 22:36:45 +000023625/*
23626 * Display script name where an item was last set.
23627 * Should only be invoked when 'verbose' is non-zero.
23628 */
23629 void
23630last_set_msg(scriptID)
23631 scid_T scriptID;
23632{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023633 char_u *p;
23634
Bram Moolenaar661b1822005-07-28 22:36:45 +000023635 if (scriptID != 0)
23636 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023637 p = home_replace_save(NULL, get_scriptname(scriptID));
23638 if (p != NULL)
23639 {
23640 verbose_enter();
23641 MSG_PUTS(_("\n\tLast set from "));
23642 MSG_PUTS(p);
23643 vim_free(p);
23644 verbose_leave();
23645 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023646 }
23647}
23648
Bram Moolenaard812df62008-11-09 12:46:09 +000023649/*
23650 * List v:oldfiles in a nice way.
23651 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023652 void
23653ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023654 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023655{
23656 list_T *l = vimvars[VV_OLDFILES].vv_list;
23657 listitem_T *li;
23658 int nr = 0;
23659
23660 if (l == NULL)
23661 msg((char_u *)_("No old files"));
23662 else
23663 {
23664 msg_start();
23665 msg_scroll = TRUE;
23666 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23667 {
23668 msg_outnum((long)++nr);
23669 MSG_PUTS(": ");
23670 msg_outtrans(get_tv_string(&li->li_tv));
23671 msg_putchar('\n');
23672 out_flush(); /* output one line at a time */
23673 ui_breakcheck();
23674 }
23675 /* Assume "got_int" was set to truncate the listing. */
23676 got_int = FALSE;
23677
23678#ifdef FEAT_BROWSE_CMD
23679 if (cmdmod.browse)
23680 {
23681 quit_more = FALSE;
23682 nr = prompt_for_number(FALSE);
23683 msg_starthere();
23684 if (nr > 0)
23685 {
23686 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23687 (long)nr);
23688
23689 if (p != NULL)
23690 {
23691 p = expand_env_save(p);
23692 eap->arg = p;
23693 eap->cmdidx = CMD_edit;
23694 cmdmod.browse = FALSE;
23695 do_exedit(eap, NULL);
23696 vim_free(p);
23697 }
23698 }
23699 }
23700#endif
23701 }
23702}
23703
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704#endif /* FEAT_EVAL */
23705
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023707#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708
23709#ifdef WIN3264
23710/*
23711 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23712 */
23713static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23714static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23715static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23716
23717/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023718 * Get the short path (8.3) for the filename in "fnamep".
23719 * Only works for a valid file name.
23720 * When the path gets longer "fnamep" is changed and the allocated buffer
23721 * is put in "bufp".
23722 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23723 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 */
23725 static int
23726get_short_pathname(fnamep, bufp, fnamelen)
23727 char_u **fnamep;
23728 char_u **bufp;
23729 int *fnamelen;
23730{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023731 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 char_u *newbuf;
23733
23734 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 l = GetShortPathName(*fnamep, *fnamep, len);
23736 if (l > len - 1)
23737 {
23738 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 newbuf = vim_strnsave(*fnamep, l);
23741 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023742 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743
23744 vim_free(*bufp);
23745 *fnamep = *bufp = newbuf;
23746
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023747 /* Really should always succeed, as the buffer is big enough. */
23748 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 }
23750
23751 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023752 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753}
23754
23755/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 * Get the short path (8.3) for the filename in "fname". The converted
23757 * path is returned in "bufp".
23758 *
23759 * Some of the directories specified in "fname" may not exist. This function
23760 * will shorten the existing directories at the beginning of the path and then
23761 * append the remaining non-existing path.
23762 *
23763 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023764 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023765 * bufp - Pointer to an allocated buffer for the filename.
23766 * fnamelen - Length of the filename pointed to by fname
23767 *
23768 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 */
23770 static int
23771shortpath_for_invalid_fname(fname, bufp, fnamelen)
23772 char_u **fname;
23773 char_u **bufp;
23774 int *fnamelen;
23775{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023776 char_u *short_fname, *save_fname, *pbuf_unused;
23777 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023779 int old_len, len;
23780 int new_len, sfx_len;
23781 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782
23783 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023784 old_len = *fnamelen;
23785 save_fname = vim_strnsave(*fname, old_len);
23786 pbuf_unused = NULL;
23787 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023789 endp = save_fname + old_len - 1; /* Find the end of the copy */
23790 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023792 /*
23793 * Try shortening the supplied path till it succeeds by removing one
23794 * directory at a time from the tail of the path.
23795 */
23796 len = 0;
23797 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023799 /* go back one path-separator */
23800 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23801 --endp;
23802 if (endp <= save_fname)
23803 break; /* processed the complete path */
23804
23805 /*
23806 * Replace the path separator with a NUL and try to shorten the
23807 * resulting path.
23808 */
23809 ch = *endp;
23810 *endp = 0;
23811 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023812 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023813 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23814 {
23815 retval = FAIL;
23816 goto theend;
23817 }
23818 *endp = ch; /* preserve the string */
23819
23820 if (len > 0)
23821 break; /* successfully shortened the path */
23822
23823 /* failed to shorten the path. Skip the path separator */
23824 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 }
23826
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023827 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023829 /*
23830 * Succeeded in shortening the path. Now concatenate the shortened
23831 * path with the remaining path at the tail.
23832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023834 /* Compute the length of the new path. */
23835 sfx_len = (int)(save_endp - endp) + 1;
23836 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023838 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023840 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023842 /* There is not enough space in the currently allocated string,
23843 * copy it to a buffer big enough. */
23844 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023846 {
23847 retval = FAIL;
23848 goto theend;
23849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 }
23851 else
23852 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023853 /* Transfer short_fname to the main buffer (it's big enough),
23854 * unless get_short_pathname() did its work in-place. */
23855 *fname = *bufp = save_fname;
23856 if (short_fname != save_fname)
23857 vim_strncpy(save_fname, short_fname, len);
23858 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023860
23861 /* concat the not-shortened part of the path */
23862 vim_strncpy(*fname + len, endp, sfx_len);
23863 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023865
23866theend:
23867 vim_free(pbuf_unused);
23868 vim_free(save_fname);
23869
23870 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871}
23872
23873/*
23874 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023875 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 */
23877 static int
23878shortpath_for_partial(fnamep, bufp, fnamelen)
23879 char_u **fnamep;
23880 char_u **bufp;
23881 int *fnamelen;
23882{
23883 int sepcount, len, tflen;
23884 char_u *p;
23885 char_u *pbuf, *tfname;
23886 int hasTilde;
23887
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023888 /* Count up the path separators from the RHS.. so we know which part
23889 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023891 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 if (vim_ispathsep(*p))
23893 ++sepcount;
23894
23895 /* Need full path first (use expand_env() to remove a "~/") */
23896 hasTilde = (**fnamep == '~');
23897 if (hasTilde)
23898 pbuf = tfname = expand_env_save(*fnamep);
23899 else
23900 pbuf = tfname = FullName_save(*fnamep, FALSE);
23901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023902 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023904 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23905 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906
23907 if (len == 0)
23908 {
23909 /* Don't have a valid filename, so shorten the rest of the
23910 * path if we can. This CAN give us invalid 8.3 filenames, but
23911 * there's not a lot of point in guessing what it might be.
23912 */
23913 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023914 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23915 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 }
23917
23918 /* Count the paths backward to find the beginning of the desired string. */
23919 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023920 {
23921#ifdef FEAT_MBYTE
23922 if (has_mbyte)
23923 p -= mb_head_off(tfname, p);
23924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 if (vim_ispathsep(*p))
23926 {
23927 if (sepcount == 0 || (hasTilde && sepcount == 1))
23928 break;
23929 else
23930 sepcount --;
23931 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 if (hasTilde)
23934 {
23935 --p;
23936 if (p >= tfname)
23937 *p = '~';
23938 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023939 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 }
23941 else
23942 ++p;
23943
23944 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23945 vim_free(*bufp);
23946 *fnamelen = (int)STRLEN(p);
23947 *bufp = pbuf;
23948 *fnamep = p;
23949
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023950 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951}
23952#endif /* WIN3264 */
23953
23954/*
23955 * Adjust a filename, according to a string of modifiers.
23956 * *fnamep must be NUL terminated when called. When returning, the length is
23957 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023958 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 * When there is an error, *fnamep is set to NULL.
23960 */
23961 int
23962modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23963 char_u *src; /* string with modifiers */
23964 int *usedlen; /* characters after src that are used */
23965 char_u **fnamep; /* file name so far */
23966 char_u **bufp; /* buffer for allocated file name or NULL */
23967 int *fnamelen; /* length of fnamep */
23968{
23969 int valid = 0;
23970 char_u *tail;
23971 char_u *s, *p, *pbuf;
23972 char_u dirname[MAXPATHL];
23973 int c;
23974 int has_fullname = 0;
23975#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023976 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 int has_shortname = 0;
23978#endif
23979
23980repeat:
23981 /* ":p" - full path/file_name */
23982 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23983 {
23984 has_fullname = 1;
23985
23986 valid |= VALID_PATH;
23987 *usedlen += 2;
23988
23989 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23990 if ((*fnamep)[0] == '~'
23991#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23992 && ((*fnamep)[1] == '/'
23993# ifdef BACKSLASH_IN_FILENAME
23994 || (*fnamep)[1] == '\\'
23995# endif
23996 || (*fnamep)[1] == NUL)
23997
23998#endif
23999 )
24000 {
24001 *fnamep = expand_env_save(*fnamep);
24002 vim_free(*bufp); /* free any allocated file name */
24003 *bufp = *fnamep;
24004 if (*fnamep == NULL)
24005 return -1;
24006 }
24007
24008 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024009 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 {
24011 if (vim_ispathsep(*p)
24012 && p[1] == '.'
24013 && (p[2] == NUL
24014 || vim_ispathsep(p[2])
24015 || (p[2] == '.'
24016 && (p[3] == NUL || vim_ispathsep(p[3])))))
24017 break;
24018 }
24019
24020 /* FullName_save() is slow, don't use it when not needed. */
24021 if (*p != NUL || !vim_isAbsName(*fnamep))
24022 {
24023 *fnamep = FullName_save(*fnamep, *p != NUL);
24024 vim_free(*bufp); /* free any allocated file name */
24025 *bufp = *fnamep;
24026 if (*fnamep == NULL)
24027 return -1;
24028 }
24029
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024030#ifdef WIN3264
24031# if _WIN32_WINNT >= 0x0500
24032 if (vim_strchr(*fnamep, '~') != NULL)
24033 {
24034 /* Expand 8.3 filename to full path. Needed to make sure the same
24035 * file does not have two different names.
24036 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24037 p = alloc(_MAX_PATH + 1);
24038 if (p != NULL)
24039 {
24040 if (GetLongPathName(*fnamep, p, MAXPATHL))
24041 {
24042 vim_free(*bufp);
24043 *bufp = *fnamep = p;
24044 }
24045 else
24046 vim_free(p);
24047 }
24048 }
24049# endif
24050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 /* Append a path separator to a directory. */
24052 if (mch_isdir(*fnamep))
24053 {
24054 /* Make room for one or two extra characters. */
24055 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24056 vim_free(*bufp); /* free any allocated file name */
24057 *bufp = *fnamep;
24058 if (*fnamep == NULL)
24059 return -1;
24060 add_pathsep(*fnamep);
24061 }
24062 }
24063
24064 /* ":." - path relative to the current directory */
24065 /* ":~" - path relative to the home directory */
24066 /* ":8" - shortname path - postponed till after */
24067 while (src[*usedlen] == ':'
24068 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24069 {
24070 *usedlen += 2;
24071 if (c == '8')
24072 {
24073#ifdef WIN3264
24074 has_shortname = 1; /* Postpone this. */
24075#endif
24076 continue;
24077 }
24078 pbuf = NULL;
24079 /* Need full path first (use expand_env() to remove a "~/") */
24080 if (!has_fullname)
24081 {
24082 if (c == '.' && **fnamep == '~')
24083 p = pbuf = expand_env_save(*fnamep);
24084 else
24085 p = pbuf = FullName_save(*fnamep, FALSE);
24086 }
24087 else
24088 p = *fnamep;
24089
24090 has_fullname = 0;
24091
24092 if (p != NULL)
24093 {
24094 if (c == '.')
24095 {
24096 mch_dirname(dirname, MAXPATHL);
24097 s = shorten_fname(p, dirname);
24098 if (s != NULL)
24099 {
24100 *fnamep = s;
24101 if (pbuf != NULL)
24102 {
24103 vim_free(*bufp); /* free any allocated file name */
24104 *bufp = pbuf;
24105 pbuf = NULL;
24106 }
24107 }
24108 }
24109 else
24110 {
24111 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24112 /* Only replace it when it starts with '~' */
24113 if (*dirname == '~')
24114 {
24115 s = vim_strsave(dirname);
24116 if (s != NULL)
24117 {
24118 *fnamep = s;
24119 vim_free(*bufp);
24120 *bufp = s;
24121 }
24122 }
24123 }
24124 vim_free(pbuf);
24125 }
24126 }
24127
24128 tail = gettail(*fnamep);
24129 *fnamelen = (int)STRLEN(*fnamep);
24130
24131 /* ":h" - head, remove "/file_name", can be repeated */
24132 /* Don't remove the first "/" or "c:\" */
24133 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24134 {
24135 valid |= VALID_HEAD;
24136 *usedlen += 2;
24137 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024138 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024139 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 *fnamelen = (int)(tail - *fnamep);
24141#ifdef VMS
24142 if (*fnamelen > 0)
24143 *fnamelen += 1; /* the path separator is part of the path */
24144#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024145 if (*fnamelen == 0)
24146 {
24147 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24148 p = vim_strsave((char_u *)".");
24149 if (p == NULL)
24150 return -1;
24151 vim_free(*bufp);
24152 *bufp = *fnamep = tail = p;
24153 *fnamelen = 1;
24154 }
24155 else
24156 {
24157 while (tail > s && !after_pathsep(s, tail))
24158 mb_ptr_back(*fnamep, tail);
24159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 }
24161
24162 /* ":8" - shortname */
24163 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24164 {
24165 *usedlen += 2;
24166#ifdef WIN3264
24167 has_shortname = 1;
24168#endif
24169 }
24170
24171#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024172 /*
24173 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 */
24175 if (has_shortname)
24176 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024177 /* Copy the string if it is shortened by :h and when it wasn't copied
24178 * yet, because we are going to change it in place. Avoids changing
24179 * the buffer name for "%:8". */
24180 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 {
24182 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024183 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 return -1;
24185 vim_free(*bufp);
24186 *bufp = *fnamep = p;
24187 }
24188
24189 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024190 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 if (!has_fullname && !vim_isAbsName(*fnamep))
24192 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024193 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 return -1;
24195 }
24196 else
24197 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024198 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199
Bram Moolenaardc935552011-08-17 15:23:23 +020024200 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024202 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 return -1;
24204
24205 if (l == 0)
24206 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024207 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024209 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210 return -1;
24211 }
24212 *fnamelen = l;
24213 }
24214 }
24215#endif /* WIN3264 */
24216
24217 /* ":t" - tail, just the basename */
24218 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24219 {
24220 *usedlen += 2;
24221 *fnamelen -= (int)(tail - *fnamep);
24222 *fnamep = tail;
24223 }
24224
24225 /* ":e" - extension, can be repeated */
24226 /* ":r" - root, without extension, can be repeated */
24227 while (src[*usedlen] == ':'
24228 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24229 {
24230 /* find a '.' in the tail:
24231 * - for second :e: before the current fname
24232 * - otherwise: The last '.'
24233 */
24234 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24235 s = *fnamep - 2;
24236 else
24237 s = *fnamep + *fnamelen - 1;
24238 for ( ; s > tail; --s)
24239 if (s[0] == '.')
24240 break;
24241 if (src[*usedlen + 1] == 'e') /* :e */
24242 {
24243 if (s > tail)
24244 {
24245 *fnamelen += (int)(*fnamep - (s + 1));
24246 *fnamep = s + 1;
24247#ifdef VMS
24248 /* cut version from the extension */
24249 s = *fnamep + *fnamelen - 1;
24250 for ( ; s > *fnamep; --s)
24251 if (s[0] == ';')
24252 break;
24253 if (s > *fnamep)
24254 *fnamelen = s - *fnamep;
24255#endif
24256 }
24257 else if (*fnamep <= tail)
24258 *fnamelen = 0;
24259 }
24260 else /* :r */
24261 {
24262 if (s > tail) /* remove one extension */
24263 *fnamelen = (int)(s - *fnamep);
24264 }
24265 *usedlen += 2;
24266 }
24267
24268 /* ":s?pat?foo?" - substitute */
24269 /* ":gs?pat?foo?" - global substitute */
24270 if (src[*usedlen] == ':'
24271 && (src[*usedlen + 1] == 's'
24272 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24273 {
24274 char_u *str;
24275 char_u *pat;
24276 char_u *sub;
24277 int sep;
24278 char_u *flags;
24279 int didit = FALSE;
24280
24281 flags = (char_u *)"";
24282 s = src + *usedlen + 2;
24283 if (src[*usedlen + 1] == 'g')
24284 {
24285 flags = (char_u *)"g";
24286 ++s;
24287 }
24288
24289 sep = *s++;
24290 if (sep)
24291 {
24292 /* find end of pattern */
24293 p = vim_strchr(s, sep);
24294 if (p != NULL)
24295 {
24296 pat = vim_strnsave(s, (int)(p - s));
24297 if (pat != NULL)
24298 {
24299 s = p + 1;
24300 /* find end of substitution */
24301 p = vim_strchr(s, sep);
24302 if (p != NULL)
24303 {
24304 sub = vim_strnsave(s, (int)(p - s));
24305 str = vim_strnsave(*fnamep, *fnamelen);
24306 if (sub != NULL && str != NULL)
24307 {
24308 *usedlen = (int)(p + 1 - src);
24309 s = do_string_sub(str, pat, sub, flags);
24310 if (s != NULL)
24311 {
24312 *fnamep = s;
24313 *fnamelen = (int)STRLEN(s);
24314 vim_free(*bufp);
24315 *bufp = s;
24316 didit = TRUE;
24317 }
24318 }
24319 vim_free(sub);
24320 vim_free(str);
24321 }
24322 vim_free(pat);
24323 }
24324 }
24325 /* after using ":s", repeat all the modifiers */
24326 if (didit)
24327 goto repeat;
24328 }
24329 }
24330
24331 return valid;
24332}
24333
24334/*
24335 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24336 * "flags" can be "g" to do a global substitute.
24337 * Returns an allocated string, NULL for error.
24338 */
24339 char_u *
24340do_string_sub(str, pat, sub, flags)
24341 char_u *str;
24342 char_u *pat;
24343 char_u *sub;
24344 char_u *flags;
24345{
24346 int sublen;
24347 regmatch_T regmatch;
24348 int i;
24349 int do_all;
24350 char_u *tail;
24351 garray_T ga;
24352 char_u *ret;
24353 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024354 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355
24356 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24357 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024358 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359
24360 ga_init2(&ga, 1, 200);
24361
24362 do_all = (flags[0] == 'g');
24363
24364 regmatch.rm_ic = p_ic;
24365 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24366 if (regmatch.regprog != NULL)
24367 {
24368 tail = str;
24369 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24370 {
24371 /*
24372 * Get some space for a temporary buffer to do the substitution
24373 * into. It will contain:
24374 * - The text up to where the match is.
24375 * - The substituted text.
24376 * - The text after the match.
24377 */
24378 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24379 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24380 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24381 {
24382 ga_clear(&ga);
24383 break;
24384 }
24385
24386 /* copy the text up to where the match is */
24387 i = (int)(regmatch.startp[0] - tail);
24388 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24389 /* add the substituted text */
24390 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24391 + ga.ga_len + i, TRUE, TRUE, FALSE);
24392 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024393 zero_width = (tail == regmatch.endp[0]
24394 || regmatch.startp[0] == regmatch.endp[0]);
24395 tail = regmatch.endp[0];
24396 if (*tail == NUL)
24397 break;
24398 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024400 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24402 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 if (!do_all)
24405 break;
24406 }
24407
24408 if (ga.ga_data != NULL)
24409 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24410
Bram Moolenaar473de612013-06-08 18:19:48 +020024411 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 }
24413
24414 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24415 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024416 if (p_cpo == empty_option)
24417 p_cpo = save_cpo;
24418 else
24419 /* Darn, evaluating {sub} expression changed the value. */
24420 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421
24422 return ret;
24423}
24424
24425#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */