blob: 5cac7b4adf0d3dd7d2b7086dd9d1d967dc20a228 [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 Moolenaard812df62008-11-09 12:46:09 +0000359 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200360 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000372#define vimvarht vimvardict.dv_hashtab
373
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
375static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
377static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
378static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
380static void list_glob_vars __ARGS((int *first));
381static void list_buf_vars __ARGS((int *first));
382static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_vim_vars __ARGS((int *first));
387static void list_script_vars __ARGS((int *first));
388static void list_func_vars __ARGS((int *first));
389static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
391static int check_changedtick __ARGS((char_u *arg));
392static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
393static void clear_lval __ARGS((lval_T *lp));
394static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
395static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
397static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
398static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
399static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
400static void item_lock __ARGS((typval_T *tv, int deep, int lock));
401static int tv_islocked __ARGS((typval_T *tv));
402
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
404static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000409static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
410static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000412static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000417static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static 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 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
443static int string2float __ARGS((char_u *text, float_T *value));
444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static 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 +0200449static 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 +0000450static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000451static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200455static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100458static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000482static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000485static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000488static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#ifdef FEAT_FLOAT
495static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200496static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
501static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
513static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000517static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
524static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200526static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000528static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000537static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000539static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000545static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000553static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000554static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000555static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200559static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000560static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000568static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000582static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100587static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000589static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200602static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605#ifdef FEAT_LUA
606static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100624#ifdef FEAT_MZSCHEME
625static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100629static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631#ifdef FEAT_FLOAT
632static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000635static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000636static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200637#ifdef FEAT_PYTHON3
638static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
640#ifdef FEAT_PYTHON
641static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000645static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200660static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100662static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000665static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000667static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000674static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000675static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000676static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000677static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200679static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100682#ifdef FEAT_CRYPT
683static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
684#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200686static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
689static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200690static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000693static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000701static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200702static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200712static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200713static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000719static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200720static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100745static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100756static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000758static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000759static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static int get_env_len __ARGS((char_u **arg));
761static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000763static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
764#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
765#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
766 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000767static 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 +0000768static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000769static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
771static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static typval_T *alloc_tv __ARGS((void));
773static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void init_tv __ARGS((typval_T *varp));
775static long get_tv_number __ARGS((typval_T *varp));
776static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000777static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static char_u *get_tv_string __ARGS((typval_T *varp));
779static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200782static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
784static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
785static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000786static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
787static 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 +0000788static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
789static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000790static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200791static int var_check_func_name __ARGS((char_u *name, int new_var));
792static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000793static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000794static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
796static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
797static int eval_fname_script __ARGS((char_u *p));
798static int eval_fname_sid __ARGS((char_u *p));
799static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static ufunc_T *find_func __ARGS((char_u *name));
801static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000802static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
804static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000805static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
806static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_total_cmp __ARGS((const void *s1, const void *s2));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200818static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000820static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static 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 +0000823static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
824static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000826static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
827static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000828static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200832
833#ifdef EBCDIC
834static int compare_func_name __ARGS((const void *s1, const void *s2));
835static void sortFunctions __ARGS(());
836#endif
837
Bram Moolenaar33570922005-01-25 22:26:29 +0000838/*
839 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 */
841 void
842eval_init()
843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 int i;
845 struct vimvar *p;
846
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
848 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200849 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000850 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 STRCPY(p->vv_di.di_key, p->vv_name);
857 if (p->vv_flags & VV_RO)
858 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 else if (p->vv_flags & VV_RO_SBX)
860 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
861 else
862 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000863
864 /* add to v: scope dict, unless the value is not always available */
865 if (p->vv_type != VAR_UNKNOWN)
866 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 /* add to compat scope dict */
869 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000871 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200872 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100876 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200909 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001560 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001566 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001591 if (str_arg_only)
1592 len = 0;
1593 else
1594 /* Recognize a number argument, the others must be strings. */
1595 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (len != 0 && len == (int)STRLEN(argv[i]))
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_NUMBER;
1599 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else
1602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001603 argvars[i].v_type = VAR_STRING;
1604 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 }
1607
1608 if (safe)
1609 {
1610 save_funccalp = save_funccal();
1611 ++sandbox;
1612 }
1613
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1615 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (safe)
1619 {
1620 --sandbox;
1621 restore_funccal(save_funccalp);
1622 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 vim_free(argvars);
1624
1625 if (ret == FAIL)
1626 clear_tv(rettv);
1627
1628 return ret;
1629}
1630
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001631/*
1632 * Call vimL function "func" and return the result as a number.
1633 * Returns -1 when calling the function fails.
1634 * Uses argv[argc] for the function arguments.
1635 */
1636 long
1637call_func_retnr(func, argc, argv, safe)
1638 char_u *func;
1639 int argc;
1640 char_u **argv;
1641 int safe; /* use the sandbox */
1642{
1643 typval_T rettv;
1644 long retval;
1645
1646 /* All arguments are passed as strings, no conversion to number. */
1647 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1648 return -1;
1649
1650 retval = get_tv_number_chk(&rettv, NULL);
1651 clear_tv(&rettv);
1652 return retval;
1653}
1654
1655#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1656 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a string.
1661 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 * Uses argv[argc] for the function arguments.
1663 */
1664 void *
1665call_func_retstr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return NULL;
1677
1678 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001698 /* All arguments are passed as strings, no conversion to number. */
1699 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 return NULL;
1701
1702 if (rettv.v_type != VAR_LIST)
1703 {
1704 clear_tv(&rettv);
1705 return NULL;
1706 }
1707
1708 return rettv.vval.v_list;
1709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
1711
1712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar429fa852013-04-15 12:27:36 +02002121 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002136 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 /* When assigning to a scope dictionary check that a function and
2730 * variable name is valid (only variable name unless it is l: or
2731 * g: dictionary). Disallow overwriting a builtin function. */
2732 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002733 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002734 int prevval;
2735 int wrong;
2736
2737 if (len != -1)
2738 {
2739 prevval = key[len];
2740 key[len] = NUL;
2741 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002742 else
2743 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2745 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 || !valid_varname(key);
2748 if (len != -1)
2749 key[len] = prevval;
2750 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 return NULL;
2752 }
2753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 /* Can't add "v:" variable. */
2757 if (lp->ll_dict == &vimvardict)
2758 {
2759 EMSG2(_(e_illvar), name);
2760 return NULL;
2761 }
2762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (len == -1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (len == -1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 p = NULL;
2780 break;
2781 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 /* existing variable, need to check if it can be changed */
2783 else if (var_check_ro(lp->ll_di->di_flags, name))
2784 return NULL;
2785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 else
2791 {
2792 /*
2793 * Get the number and item for the only or first index of the List.
2794 */
2795 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 else
2798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002799 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 clear_tv(&var1);
2801 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_list = lp->ll_tv->vval.v_list;
2804 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2805 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002807 if (lp->ll_n1 < 0)
2808 {
2809 lp->ll_n1 = 0;
2810 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2811 }
2812 }
2813 if (lp->ll_li == NULL)
2814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821
2822 /*
2823 * May need to find the item or absolute index for the second
2824 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 * When no index given: "lp->ll_empty2" is TRUE.
2826 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 {
2837 if (!quiet)
2838 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002840 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2845 if (lp->ll_n1 < 0)
2846 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2847 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return p;
2860}
2861
2862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 */
2865 static void
2866clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868{
2869 vim_free(lp->ll_exp_name);
2870 vim_free(lp->ll_newkey);
2871}
2872
2873/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 */
2878 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885{
2886 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 listitem_T *ri;
2888 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889
2890 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 cc = *endp;
2895 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 if (op != NULL && *op != '=')
2897 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002901 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002902 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 {
2904 if (tv_op(&tv, rettv, op) == OK)
2905 set_var(lp->ll_name, &tv, FALSE);
2906 clear_tv(&tv);
2907 }
2908 }
2909 else
2910 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 else if (tv_check_lock(lp->ll_newkey == NULL
2915 ? lp->ll_tv->v_lock
2916 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2917 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
2920 /*
2921 * Assign the List values to the list items.
2922 */
2923 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2927 else
2928 {
2929 clear_tv(&lp->ll_li->li_tv);
2930 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 ri = ri->li_next;
2933 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2934 break;
2935 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002938 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ri = NULL;
2941 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 lp->ll_li = lp->ll_li->li_next;
2945 ++lp->ll_n1;
2946 }
2947 if (ri != NULL)
2948 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 else if (lp->ll_empty2
2950 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 : lp->ll_n1 != lp->ll_n2)
2952 EMSG(_("E711: List value has not enough items"));
2953 }
2954 else
2955 {
2956 /*
2957 * Assign to a List or Dictionary item.
2958 */
2959 if (lp->ll_newkey != NULL)
2960 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 if (op != NULL && *op != '=')
2962 {
2963 EMSG2(_(e_letwrong), op);
2964 return;
2965 }
2966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (di == NULL)
2970 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2972 {
2973 vim_free(di);
2974 return;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 else if (op != NULL && *op != '=')
2979 {
2980 tv_op(lp->ll_tv, rettv, op);
2981 return;
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the value to the variable or list item.
2988 */
2989 if (copy)
2990 copy_tv(rettv, lp->ll_tv);
2991 else
2992 {
2993 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002994 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 }
2997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998}
2999
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3002 * Returns OK or FAIL.
3003 */
3004 static int
3005tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 typval_T *tv1;
3007 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 char_u *op;
3009{
3010 long n;
3011 char_u numbuf[NUMBUFLEN];
3012 char_u *s;
3013
3014 /* Can't do anything with a Funcref or a Dict on the right. */
3015 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3016 {
3017 switch (tv1->v_type)
3018 {
3019 case VAR_DICT:
3020 case VAR_FUNC:
3021 break;
3022
3023 case VAR_LIST:
3024 if (*op != '+' || tv2->v_type != VAR_LIST)
3025 break;
3026 /* List += List */
3027 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3028 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3029 return OK;
3030
3031 case VAR_NUMBER:
3032 case VAR_STRING:
3033 if (tv2->v_type == VAR_LIST)
3034 break;
3035 if (*op == '+' || *op == '-')
3036 {
3037 /* nr += nr or nr -= nr*/
3038 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 if (tv2->v_type == VAR_FLOAT)
3041 {
3042 float_T f = n;
3043
3044 if (*op == '+')
3045 f += tv2->vval.v_float;
3046 else
3047 f -= tv2->vval.v_float;
3048 clear_tv(tv1);
3049 tv1->v_type = VAR_FLOAT;
3050 tv1->vval.v_float = f;
3051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053#endif
3054 {
3055 if (*op == '+')
3056 n += get_tv_number(tv2);
3057 else
3058 n -= get_tv_number(tv2);
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_NUMBER;
3061 tv1->vval.v_number = n;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 }
3064 else
3065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 if (tv2->v_type == VAR_FLOAT)
3067 break;
3068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 /* str .= str */
3070 s = get_tv_string(tv1);
3071 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3072 clear_tv(tv1);
3073 tv1->v_type = VAR_STRING;
3074 tv1->vval.v_string = s;
3075 }
3076 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077
3078#ifdef FEAT_FLOAT
3079 case VAR_FLOAT:
3080 {
3081 float_T f;
3082
3083 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3084 && tv2->v_type != VAR_NUMBER
3085 && tv2->v_type != VAR_STRING))
3086 break;
3087 if (tv2->v_type == VAR_FLOAT)
3088 f = tv2->vval.v_float;
3089 else
3090 f = get_tv_number(tv2);
3091 if (*op == '+')
3092 tv1->vval.v_float += f;
3093 else
3094 tv1->vval.v_float -= f;
3095 }
3096 return OK;
3097#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 }
3099 }
3100
3101 EMSG2(_(e_letwrong), op);
3102 return FAIL;
3103}
3104
3105/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 * Add a watcher to a list.
3107 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003108 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 list_T *l;
3111 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112{
3113 lw->lw_next = l->lv_watch;
3114 l->lv_watch = lw;
3115}
3116
3117/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003118 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 * No warning when it isn't found...
3120 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003121 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 list_T *l;
3124 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125{
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127
3128 lwp = &l->lv_watch;
3129 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3130 {
3131 if (lw == lwrem)
3132 {
3133 *lwp = lw->lw_next;
3134 break;
3135 }
3136 lwp = &lw->lw_next;
3137 }
3138}
3139
3140/*
3141 * Just before removing an item from a list: advance watchers to the next
3142 * item.
3143 */
3144 static void
3145list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 list_T *l;
3147 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150
3151 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3152 if (lw->lw_item == item)
3153 lw->lw_item = item->li_next;
3154}
3155
3156/*
3157 * Evaluate the expression used in a ":for var in expr" command.
3158 * "arg" points to "var".
3159 * Set "*errp" to TRUE for an error, FALSE otherwise;
3160 * Return a pointer that holds the info. Null when there is an error.
3161 */
3162 void *
3163eval_for_line(arg, errp, nextcmdp, skip)
3164 char_u *arg;
3165 int *errp;
3166 char_u **nextcmdp;
3167 int skip;
3168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 typval_T tv;
3172 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 *errp = TRUE; /* default: there is an error */
3175
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 if (fi == NULL)
3178 return NULL;
3179
3180 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3181 if (expr == NULL)
3182 return fi;
3183
3184 expr = skipwhite(expr);
3185 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003187 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 return fi;
3189 }
3190
3191 if (skip)
3192 ++emsg_skip;
3193 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3194 {
3195 *errp = FALSE;
3196 if (!skip)
3197 {
3198 l = tv.vval.v_list;
3199 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 clear_tv(&tv);
3203 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 else
3205 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003206 /* No need to increment the refcount, it's already set for the
3207 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 fi->fi_list = l;
3209 list_add_watch(l, &fi->fi_lw);
3210 fi->fi_lw.lw_item = l->lv_first;
3211 }
3212 }
3213 }
3214 if (skip)
3215 --emsg_skip;
3216
3217 return fi;
3218}
3219
3220/*
3221 * Use the first item in a ":for" list. Advance to the next.
3222 * Assign the values to the variable (list). "arg" points to the first one.
3223 * Return TRUE when a valid item was found, FALSE when at end of list or
3224 * something wrong.
3225 */
3226 int
3227next_for_item(fi_void, arg)
3228 void *fi_void;
3229 char_u *arg;
3230{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 item = fi->fi_lw.lw_item;
3236 if (item == NULL)
3237 result = FALSE;
3238 else
3239 {
3240 fi->fi_lw.lw_item = item->li_next;
3241 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3242 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3243 }
3244 return result;
3245}
3246
3247/*
3248 * Free the structure used to store info used by ":for".
3249 */
3250 void
3251free_for_info(fi_void)
3252 void *fi_void;
3253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003256 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 list_unref(fi->fi_list);
3260 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 vim_free(fi);
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3265
3266 void
3267set_context_for_expression(xp, arg, cmdidx)
3268 expand_T *xp;
3269 char_u *arg;
3270 cmdidx_T cmdidx;
3271{
3272 int got_eq = FALSE;
3273 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 if (cmdidx == CMD_let)
3277 {
3278 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003282 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 {
3284 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (vim_iswhite(*p))
3287 break;
3288 }
3289 return;
3290 }
3291 }
3292 else
3293 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3294 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 while ((xp->xp_pattern = vim_strpbrk(arg,
3296 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3297 {
3298 c = *xp->xp_pattern;
3299 if (c == '&')
3300 {
3301 c = xp->xp_pattern[1];
3302 if (c == '&')
3303 {
3304 ++xp->xp_pattern;
3305 xp->xp_context = cmdidx != CMD_let || got_eq
3306 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3307 }
3308 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003311 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3312 xp->xp_pattern += 2;
3313
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316 else if (c == '$')
3317 {
3318 /* environment variable */
3319 xp->xp_context = EXPAND_ENV_VARS;
3320 }
3321 else if (c == '=')
3322 {
3323 got_eq = TRUE;
3324 xp->xp_context = EXPAND_EXPRESSION;
3325 }
3326 else if (c == '<'
3327 && xp->xp_context == EXPAND_FUNCTIONS
3328 && vim_strchr(xp->xp_pattern, '(') == NULL)
3329 {
3330 /* Function name can start with "<SNR>" */
3331 break;
3332 }
3333 else if (cmdidx != CMD_let || got_eq)
3334 {
3335 if (c == '"') /* string */
3336 {
3337 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3338 if (c == '\\' && xp->xp_pattern[1] != NUL)
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_NOTHING;
3341 }
3342 else if (c == '\'') /* literal string */
3343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3346 /* skip */ ;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '|')
3350 {
3351 if (xp->xp_pattern[1] == '|')
3352 {
3353 ++xp->xp_pattern;
3354 xp->xp_context = EXPAND_EXPRESSION;
3355 }
3356 else
3357 xp->xp_context = EXPAND_COMMANDS;
3358 }
3359 else
3360 xp->xp_context = EXPAND_EXPRESSION;
3361 }
3362 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 /* Doesn't look like something valid, expand as an expression
3364 * anyway. */
3365 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 arg = xp->xp_pattern;
3367 if (*arg != NUL)
3368 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3369 /* skip */ ;
3370 }
3371 xp->xp_pattern = arg;
3372}
3373
3374#endif /* FEAT_CMDL_COMPL */
3375
3376/*
3377 * ":1,25call func(arg1, arg2)" function call.
3378 */
3379 void
3380ex_call(eap)
3381 exarg_T *eap;
3382{
3383 char_u *arg = eap->arg;
3384 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003386 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 linenr_T lnum;
3390 int doesrange;
3391 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003394 if (eap->skip)
3395 {
3396 /* trans_function_name() doesn't work well when skipping, use eval0()
3397 * instead to skip to any following command, e.g. for:
3398 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003399 ++emsg_skip;
3400 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3401 clear_tv(&rettv);
3402 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003403 return;
3404 }
3405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003407 if (fudi.fd_newkey != NULL)
3408 {
3409 /* Still need to give an error message for missing key. */
3410 EMSG2(_(e_dictkey), fudi.fd_newkey);
3411 vim_free(fudi.fd_newkey);
3412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 if (tofree == NULL)
3414 return;
3415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003416 /* Increase refcount on dictionary, it could get deleted when evaluating
3417 * the arguments. */
3418 if (fudi.fd_dict != NULL)
3419 ++fudi.fd_dict->dv_refcount;
3420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003422 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar532c7802005-01-27 14:44:31 +00003425 /* Skip white space to allow ":call func ()". Not good, but required for
3426 * backward compatibility. */
3427 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 if (*startarg != '(')
3431 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003432 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 goto end;
3434 }
3435
3436 /*
3437 * When skipping, evaluate the function once, to find the end of the
3438 * arguments.
3439 * When the function takes a range, this is discovered after the first
3440 * call, and the loop is broken.
3441 */
3442 if (eap->skip)
3443 {
3444 ++emsg_skip;
3445 lnum = eap->line2; /* do it once, also with an invalid range */
3446 }
3447 else
3448 lnum = eap->line1;
3449 for ( ; lnum <= eap->line2; ++lnum)
3450 {
3451 if (!eap->skip && eap->addr_count > 0)
3452 {
3453 curwin->w_cursor.lnum = lnum;
3454 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003455#ifdef FEAT_VIRTUALEDIT
3456 curwin->w_cursor.coladd = 0;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 eap->line1, eap->line2, &doesrange,
3462 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 failed = TRUE;
3465 break;
3466 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003467
3468 /* Handle a function returning a Funcref, Dictionary or List. */
3469 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3470 {
3471 failed = TRUE;
3472 break;
3473 }
3474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (doesrange || eap->skip)
3477 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (aborting())
3484 break;
3485 }
3486 if (eap->skip)
3487 --emsg_skip;
3488
3489 if (!failed)
3490 {
3491 /* Check for trailing illegal characters and a following command. */
3492 if (!ends_excmd(*arg))
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 else
3498 eap->nextcmd = check_nextcmd(arg);
3499 }
3500
3501end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003502 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * ":unlet[!] var1 ... " command.
3508 */
3509 void
3510ex_unlet(eap)
3511 exarg_T *eap;
3512{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 ex_unletlock(eap, eap->arg, 0);
3514}
3515
3516/*
3517 * ":lockvar" and ":unlockvar" commands
3518 */
3519 void
3520ex_lockvar(eap)
3521 exarg_T *eap;
3522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int deep = 2;
3525
3526 if (eap->forceit)
3527 deep = -1;
3528 else if (vim_isdigit(*arg))
3529 {
3530 deep = getdigits(&arg);
3531 arg = skipwhite(arg);
3532 }
3533
3534 ex_unletlock(eap, arg, deep);
3535}
3536
3537/*
3538 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3539 */
3540 static void
3541ex_unletlock(eap, argstart, deep)
3542 exarg_T *eap;
3543 char_u *argstart;
3544 int deep;
3545{
3546 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 do
3552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3555 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (lv.ll_name == NULL)
3557 error = TRUE; /* error but continue parsing */
3558 if (name_end == NULL || (!vim_iswhite(*name_end)
3559 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (name_end != NULL)
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 if (!(eap->skip || error))
3567 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569 }
3570
3571 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 {
3573 if (eap->cmdidx == CMD_unlet)
3574 {
3575 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3576 error = TRUE;
3577 }
3578 else
3579 {
3580 if (do_lock_var(&lv, name_end, deep,
3581 eap->cmdidx == CMD_lockvar) == FAIL)
3582 error = TRUE;
3583 }
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 if (!eap->skip)
3587 clear_lval(&lv);
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 arg = skipwhite(name_end);
3590 } while (!ends_excmd(*arg));
3591
3592 eap->nextcmd = check_nextcmd(arg);
3593}
3594
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 int forceit;
3600{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 int ret = OK;
3602 int cc;
3603
3604 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 cc = *name_end;
3607 *name_end = NUL;
3608
3609 /* Normal name or expanded name. */
3610 if (check_changedtick(lp->ll_name))
3611 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3617 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 else if (lp->ll_range)
3619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003620 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621
3622 /* Delete a range of List items. */
3623 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3624 {
3625 li = lp->ll_li->li_next;
3626 listitem_remove(lp->ll_list, lp->ll_li);
3627 lp->ll_li = li;
3628 ++lp->ll_n1;
3629 }
3630 }
3631 else
3632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a List item. */
3635 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003638 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 }
3640
3641 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644/*
3645 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 hashtab_T *ht;
3654 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003656 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 ht = find_var_ht(name, &varname);
3659 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 hi = hash_find(ht, varname);
3662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003664 di = HI2DI(hi);
3665 if (var_check_fixed(di->di_flags, name)
3666 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 return FAIL;
3668 delete_var(ht, hi);
3669 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 if (forceit)
3673 return OK;
3674 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676}
3677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678/*
3679 * Lock or unlock variable indicated by "lp".
3680 * "deep" is the levels to go (-1 for unlimited);
3681 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3682 */
3683 static int
3684do_lock_var(lp, name_end, deep, lock)
3685 lval_T *lp;
3686 char_u *name_end;
3687 int deep;
3688 int lock;
3689{
3690 int ret = OK;
3691 int cc;
3692 dictitem_T *di;
3693
3694 if (deep == 0) /* nothing to do */
3695 return OK;
3696
3697 if (lp->ll_tv == NULL)
3698 {
3699 cc = *name_end;
3700 *name_end = NUL;
3701
3702 /* Normal name or expanded name. */
3703 if (check_changedtick(lp->ll_name))
3704 ret = FAIL;
3705 else
3706 {
3707 di = find_var(lp->ll_name, NULL);
3708 if (di == NULL)
3709 ret = FAIL;
3710 else
3711 {
3712 if (lock)
3713 di->di_flags |= DI_FLAGS_LOCK;
3714 else
3715 di->di_flags &= ~DI_FLAGS_LOCK;
3716 item_lock(&di->di_tv, deep, lock);
3717 }
3718 }
3719 *name_end = cc;
3720 }
3721 else if (lp->ll_range)
3722 {
3723 listitem_T *li = lp->ll_li;
3724
3725 /* (un)lock a range of List items. */
3726 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 item_lock(&li->li_tv, deep, lock);
3729 li = li->li_next;
3730 ++lp->ll_n1;
3731 }
3732 }
3733 else if (lp->ll_list != NULL)
3734 /* (un)lock a List item. */
3735 item_lock(&lp->ll_li->li_tv, deep, lock);
3736 else
3737 /* un(lock) a Dictionary item. */
3738 item_lock(&lp->ll_di->di_tv, deep, lock);
3739
3740 return ret;
3741}
3742
3743/*
3744 * Lock or unlock an item. "deep" is nr of levels to go.
3745 */
3746 static void
3747item_lock(tv, deep, lock)
3748 typval_T *tv;
3749 int deep;
3750 int lock;
3751{
3752 static int recurse = 0;
3753 list_T *l;
3754 listitem_T *li;
3755 dict_T *d;
3756 hashitem_T *hi;
3757 int todo;
3758
3759 if (recurse >= DICT_MAXNEST)
3760 {
3761 EMSG(_("E743: variable nested too deep for (un)lock"));
3762 return;
3763 }
3764 if (deep == 0)
3765 return;
3766 ++recurse;
3767
3768 /* lock/unlock the item itself */
3769 if (lock)
3770 tv->v_lock |= VAR_LOCKED;
3771 else
3772 tv->v_lock &= ~VAR_LOCKED;
3773
3774 switch (tv->v_type)
3775 {
3776 case VAR_LIST:
3777 if ((l = tv->vval.v_list) != NULL)
3778 {
3779 if (lock)
3780 l->lv_lock |= VAR_LOCKED;
3781 else
3782 l->lv_lock &= ~VAR_LOCKED;
3783 if (deep < 0 || deep > 1)
3784 /* recursive: lock/unlock the items the List contains */
3785 for (li = l->lv_first; li != NULL; li = li->li_next)
3786 item_lock(&li->li_tv, deep - 1, lock);
3787 }
3788 break;
3789 case VAR_DICT:
3790 if ((d = tv->vval.v_dict) != NULL)
3791 {
3792 if (lock)
3793 d->dv_lock |= VAR_LOCKED;
3794 else
3795 d->dv_lock &= ~VAR_LOCKED;
3796 if (deep < 0 || deep > 1)
3797 {
3798 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003799 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3801 {
3802 if (!HASHITEM_EMPTY(hi))
3803 {
3804 --todo;
3805 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3806 }
3807 }
3808 }
3809 }
3810 }
3811 --recurse;
3812}
3813
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003815 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3816 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003817 */
3818 static int
3819tv_islocked(tv)
3820 typval_T *tv;
3821{
3822 return (tv->v_lock & VAR_LOCKED)
3823 || (tv->v_type == VAR_LIST
3824 && tv->vval.v_list != NULL
3825 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3826 || (tv->v_type == VAR_DICT
3827 && tv->vval.v_dict != NULL
3828 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3829}
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3832/*
3833 * Delete all "menutrans_" variables.
3834 */
3835 void
3836del_menutrans_vars()
3837{
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003842 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 {
3845 if (!HASHITEM_EMPTY(hi))
3846 {
3847 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3849 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 }
3851 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853}
3854#endif
3855
3856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3857
3858/*
3859 * Local string buffer for the next two functions to store a variable name
3860 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3861 * get_user_var_name().
3862 */
3863
3864static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3865
3866static char_u *varnamebuf = NULL;
3867static int varnamebuflen = 0;
3868
3869/*
3870 * Function to concatenate a prefix and a variable name.
3871 */
3872 static char_u *
3873cat_prefix_varname(prefix, name)
3874 int prefix;
3875 char_u *name;
3876{
3877 int len;
3878
3879 len = (int)STRLEN(name) + 3;
3880 if (len > varnamebuflen)
3881 {
3882 vim_free(varnamebuf);
3883 len += 10; /* some additional space */
3884 varnamebuf = alloc(len);
3885 if (varnamebuf == NULL)
3886 {
3887 varnamebuflen = 0;
3888 return NULL;
3889 }
3890 varnamebuflen = len;
3891 }
3892 *varnamebuf = prefix;
3893 varnamebuf[1] = ':';
3894 STRCPY(varnamebuf + 2, name);
3895 return varnamebuf;
3896}
3897
3898/*
3899 * Function given to ExpandGeneric() to obtain the list of user defined
3900 * (global/buffer/window/built-in) variable names.
3901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u *
3903get_user_var_name(xp, idx)
3904 expand_T *xp;
3905 int idx;
3906{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003907 static long_u gdone;
3908 static long_u bdone;
3909 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910#ifdef FEAT_WINDOWS
3911 static long_u tdone;
3912#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003913 static int vidx;
3914 static hashitem_T *hi;
3915 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003919 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920#ifdef FEAT_WINDOWS
3921 tdone = 0;
3922#endif
3923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* Global variables */
3926 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3935 return cat_prefix_varname('g', hi->hi_key);
3936 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003938
3939 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003940 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003945 else
3946 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 while (HASHITEM_EMPTY(hi))
3948 ++hi;
3949 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return (char_u *)"b:changedtick";
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003961 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970#ifdef FEAT_WINDOWS
3971 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003972 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 if (tdone < ht->ht_used)
3974 {
3975 if (tdone++ == 0)
3976 hi = ht->ht_array;
3977 else
3978 ++hi;
3979 while (HASHITEM_EMPTY(hi))
3980 ++hi;
3981 return cat_prefix_varname('t', hi->hi_key);
3982 }
3983#endif
3984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 /* v: variables */
3986 if (vidx < VV_LEN)
3987 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 vim_free(varnamebuf);
3990 varnamebuf = NULL;
3991 varnamebuflen = 0;
3992 return NULL;
3993}
3994
3995#endif /* FEAT_CMDL_COMPL */
3996
3997/*
3998 * types for expressions.
3999 */
4000typedef enum
4001{
4002 TYPE_UNKNOWN = 0
4003 , TYPE_EQUAL /* == */
4004 , TYPE_NEQUAL /* != */
4005 , TYPE_GREATER /* > */
4006 , TYPE_GEQUAL /* >= */
4007 , TYPE_SMALLER /* < */
4008 , TYPE_SEQUAL /* <= */
4009 , TYPE_MATCH /* =~ */
4010 , TYPE_NOMATCH /* !~ */
4011} exptype_T;
4012
4013/*
4014 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4017 */
4018
4019/*
4020 * Handle zero level expression.
4021 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004023 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u **nextcmd;
4031 int evaluate;
4032{
4033 int ret;
4034 char_u *p;
4035
4036 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (ret == FAIL || !ends_excmd(*p))
4039 {
4040 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Report the invalid expression unless the expression evaluation has
4044 * been cancelled due to an aborting error, an interrupt, or an
4045 * exception.
4046 */
4047 if (!aborting())
4048 EMSG2(_(e_invexpr2), arg);
4049 ret = FAIL;
4050 }
4051 if (nextcmd != NULL)
4052 *nextcmd = check_nextcmd(p);
4053
4054 return ret;
4055}
4056
4057/*
4058 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004059 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 *
4061 * "arg" must point to the first non-white of the expression.
4062 * "arg" is advanced to the next non-white after the recognized expression.
4063 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004064 * Note: "rettv.v_lock" is not set.
4065 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
4074 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Get the first variable.
4079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return FAIL;
4082
4083 if ((*arg)[0] == '?')
4084 {
4085 result = FALSE;
4086 if (evaluate)
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
4089
4090 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 /*
4105 * Check for the ":".
4106 */
4107 if ((*arg)[0] != ':')
4108 {
4109 EMSG(_("E109: Missing ':' after '?'"));
4110 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113 }
4114
4115 /*
4116 * Get the third variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
4119 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4120 {
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128
4129 return OK;
4130}
4131
4132/*
4133 * Handle first level expression:
4134 * expr2 || expr2 || expr2 logical OR
4135 *
4136 * "arg" must point to the first non-white of the expression.
4137 * "arg" is advanced to the next non-white after the recognized expression.
4138 *
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int evaluate;
4146{
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 long result;
4149 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004150 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat until there is no following "||".
4160 */
4161 first = TRUE;
4162 result = FALSE;
4163 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4164 {
4165 if (evaluate && first)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (error)
4171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 first = FALSE;
4173 }
4174
4175 /*
4176 * Get the second variable.
4177 */
4178 *arg = skipwhite(*arg + 2);
4179 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4180 return FAIL;
4181
4182 /*
4183 * Compute the result.
4184 */
4185 if (evaluate && !result)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 if (evaluate)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle second level expression:
4205 * expr3 && expr3 && expr3 logical AND
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "&&".
4231 */
4232 first = TRUE;
4233 result = TRUE;
4234 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval4(arg, &var2, evaluate && result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle third level expression:
4276 * var1 == var2
4277 * var1 =~ var2
4278 * var1 != var2
4279 * var1 !~ var2
4280 * var1 > var2
4281 * var1 >= var2
4282 * var1 < var2
4283 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 * var1 is var2
4285 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *
4287 * "arg" must point to the first non-white of the expression.
4288 * "arg" is advanced to the next non-white after the recognized expression.
4289 *
4290 * Return OK or FAIL.
4291 */
4292 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int evaluate;
4297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u *p;
4300 int i;
4301 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int len = 2;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 regmatch_T regmatch;
4308 int ic;
4309 char_u *save_cpo;
4310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 p = *arg;
4318 switch (p[0])
4319 {
4320 case '=': if (p[1] == '=')
4321 type = TYPE_EQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_MATCH;
4324 break;
4325 case '!': if (p[1] == '=')
4326 type = TYPE_NEQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_NOMATCH;
4329 break;
4330 case '>': if (p[1] != '=')
4331 {
4332 type = TYPE_GREATER;
4333 len = 1;
4334 }
4335 else
4336 type = TYPE_GEQUAL;
4337 break;
4338 case '<': if (p[1] != '=')
4339 {
4340 type = TYPE_SMALLER;
4341 len = 1;
4342 }
4343 else
4344 type = TYPE_SEQUAL;
4345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 case 'i': if (p[1] == 's')
4347 {
4348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4349 len = 5;
4350 if (!vim_isIDc(p[len]))
4351 {
4352 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4353 type_is = TRUE;
4354 }
4355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358
4359 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 if (type != TYPE_UNKNOWN)
4363 {
4364 /* extra question mark appended: ignore case */
4365 if (p[len] == '?')
4366 {
4367 ic = TRUE;
4368 ++len;
4369 }
4370 /* extra '#' appended: match case */
4371 else if (p[len] == '#')
4372 {
4373 ic = FALSE;
4374 ++len;
4375 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004376 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
4378 ic = p_ic;
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(p + len);
4384 if (eval5(arg, &var2, evaluate) == FAIL)
4385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 return FAIL;
4388 }
4389
4390 if (evaluate)
4391 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 if (type_is && rettv->v_type != var2.v_type)
4393 {
4394 /* For "is" a different type always means FALSE, for "notis"
4395 * it means TRUE. */
4396 n1 = (type == TYPE_NEQUAL);
4397 }
4398 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4399 {
4400 if (type_is)
4401 {
4402 n1 = (rettv->v_type == var2.v_type
4403 && rettv->vval.v_list == var2.vval.v_list);
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 else if (rettv->v_type != var2.v_type
4408 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4409 {
4410 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004411 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004413 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 clear_tv(rettv);
4415 clear_tv(&var2);
4416 return FAIL;
4417 }
4418 else
4419 {
4420 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004421 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4422 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 }
4427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004428 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4429 {
4430 if (type_is)
4431 {
4432 n1 = (rettv->v_type == var2.v_type
4433 && rettv->vval.v_dict == var2.vval.v_dict);
4434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 else if (rettv->v_type != var2.v_type
4438 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4439 {
4440 if (rettv->v_type != var2.v_type)
4441 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4442 else
4443 EMSG(_("E736: Invalid operation for Dictionary"));
4444 clear_tv(rettv);
4445 clear_tv(&var2);
4446 return FAIL;
4447 }
4448 else
4449 {
4450 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004451 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4452 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 if (type == TYPE_NEQUAL)
4454 n1 = !n1;
4455 }
4456 }
4457
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004458 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4459 {
4460 if (rettv->v_type != var2.v_type
4461 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4462 {
4463 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004466 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 clear_tv(rettv);
4468 clear_tv(&var2);
4469 return FAIL;
4470 }
4471 else
4472 {
4473 /* Compare two Funcrefs for being equal or unequal. */
4474 if (rettv->vval.v_string == NULL
4475 || var2.vval.v_string == NULL)
4476 n1 = FALSE;
4477 else
4478 n1 = STRCMP(rettv->vval.v_string,
4479 var2.vval.v_string) == 0;
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004485#ifdef FEAT_FLOAT
4486 /*
4487 * If one of the two variables is a float, compare as a float.
4488 * When using "=~" or "!~", always compare as string.
4489 */
4490 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4491 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4492 {
4493 float_T f1, f2;
4494
4495 if (rettv->v_type == VAR_FLOAT)
4496 f1 = rettv->vval.v_float;
4497 else
4498 f1 = get_tv_number(rettv);
4499 if (var2.v_type == VAR_FLOAT)
4500 f2 = var2.vval.v_float;
4501 else
4502 f2 = get_tv_number(&var2);
4503 n1 = FALSE;
4504 switch (type)
4505 {
4506 case TYPE_EQUAL: n1 = (f1 == f2); break;
4507 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4508 case TYPE_GREATER: n1 = (f1 > f2); break;
4509 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4510 case TYPE_SMALLER: n1 = (f1 < f2); break;
4511 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4512 case TYPE_UNKNOWN:
4513 case TYPE_MATCH:
4514 case TYPE_NOMATCH: break; /* avoid gcc warning */
4515 }
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * If one of the two variables is a number, compare as a number.
4521 * When using "=~" or "!~", always compare as string.
4522 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 n1 = get_tv_number(rettv);
4527 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 switch (type)
4529 {
4530 case TYPE_EQUAL: n1 = (n1 == n2); break;
4531 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4532 case TYPE_GREATER: n1 = (n1 > n2); break;
4533 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4534 case TYPE_SMALLER: n1 = (n1 < n2); break;
4535 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4536 case TYPE_UNKNOWN:
4537 case TYPE_MATCH:
4538 case TYPE_NOMATCH: break; /* avoid gcc warning */
4539 }
4540 }
4541 else
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 s1 = get_tv_string_buf(rettv, buf1);
4544 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4547 else
4548 i = 0;
4549 n1 = FALSE;
4550 switch (type)
4551 {
4552 case TYPE_EQUAL: n1 = (i == 0); break;
4553 case TYPE_NEQUAL: n1 = (i != 0); break;
4554 case TYPE_GREATER: n1 = (i > 0); break;
4555 case TYPE_GEQUAL: n1 = (i >= 0); break;
4556 case TYPE_SMALLER: n1 = (i < 0); break;
4557 case TYPE_SEQUAL: n1 = (i <= 0); break;
4558
4559 case TYPE_MATCH:
4560 case TYPE_NOMATCH:
4561 /* avoid 'l' flag in 'cpoptions' */
4562 save_cpo = p_cpo;
4563 p_cpo = (char_u *)"";
4564 regmatch.regprog = vim_regcomp(s2,
4565 RE_MAGIC + RE_STRING);
4566 regmatch.rm_ic = ic;
4567 if (regmatch.regprog != NULL)
4568 {
4569 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (type == TYPE_NOMATCH)
4572 n1 = !n1;
4573 }
4574 p_cpo = save_cpo;
4575 break;
4576
4577 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4578 }
4579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 rettv->v_type = VAR_NUMBER;
4583 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 }
4586
4587 return OK;
4588}
4589
4590/*
4591 * Handle fourth level expression:
4592 * + number addition
4593 * - number subtraction
4594 * . string concatenation
4595 *
4596 * "arg" must point to the first non-white of the expression.
4597 * "arg" is advanced to the next non-white after the recognized expression.
4598 *
4599 * Return OK or FAIL.
4600 */
4601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int evaluate;
4606{
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T var2;
4608 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int op;
4610 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611#ifdef FEAT_FLOAT
4612 float_T f1 = 0, f2 = 0;
4613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *s1, *s2;
4615 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4616 char_u *p;
4617
4618 /*
4619 * Get the first variable.
4620 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004621 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
4623
4624 /*
4625 * Repeat computing, until no '+', '-' or '.' is following.
4626 */
4627 for (;;)
4628 {
4629 op = **arg;
4630 if (op != '+' && op != '-' && op != '.')
4631 break;
4632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 if ((op != '+' || rettv->v_type != VAR_LIST)
4634#ifdef FEAT_FLOAT
4635 && (op == '.' || rettv->v_type != VAR_FLOAT)
4636#endif
4637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
4639 /* For "list + ...", an illegal use of the first operand as
4640 * a number cannot be determined before evaluating the 2nd
4641 * operand: if this is also a list, all is ok.
4642 * For "something . ...", "something - ..." or "non-list + ...",
4643 * we know that the first operand needs to be a string or number
4644 * without evaluating the 2nd operand. So check before to avoid
4645 * side effects after an error. */
4646 if (evaluate && get_tv_string_chk(rettv) == NULL)
4647 {
4648 clear_tv(rettv);
4649 return FAIL;
4650 }
4651 }
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /*
4654 * Get the second variable.
4655 */
4656 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004657 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 }
4662
4663 if (evaluate)
4664 {
4665 /*
4666 * Compute the result.
4667 */
4668 if (op == '.')
4669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4671 s2 = get_tv_string_buf_chk(&var2, buf2);
4672 if (s2 == NULL) /* type error ? */
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004678 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004683 else if (op == '+' && rettv->v_type == VAR_LIST
4684 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004685 {
4686 /* concatenate Lists */
4687 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4688 &var3) == FAIL)
4689 {
4690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 return FAIL;
4693 }
4694 clear_tv(rettv);
4695 *rettv = var3;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
4700
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 f1 = rettv->vval.v_float;
4705 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 else
4708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 n1 = get_tv_number_chk(rettv, &error);
4711 if (error)
4712 {
4713 /* This can only happen for "list + non-list". For
4714 * "non-list + ..." or "something - ...", we returned
4715 * before evaluating the 2nd operand. */
4716 clear_tv(rettv);
4717 return FAIL;
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 f1 = n1;
4722#endif
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 {
4727 f2 = var2.vval.v_float;
4728 n2 = 0;
4729 }
4730 else
4731#endif
4732 {
4733 n2 = get_tv_number_chk(&var2, &error);
4734 if (error)
4735 {
4736 clear_tv(rettv);
4737 clear_tv(&var2);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (rettv->v_type == VAR_FLOAT)
4742 f2 = n2;
4743#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746
4747#ifdef FEAT_FLOAT
4748 /* If there is a float on either side the result is a float. */
4749 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4750 {
4751 if (op == '+')
4752 f1 = f1 + f2;
4753 else
4754 f1 = f1 - f2;
4755 rettv->v_type = VAR_FLOAT;
4756 rettv->vval.v_float = f1;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759#endif
4760 {
4761 if (op == '+')
4762 n1 = n1 + n2;
4763 else
4764 n1 = n1 - n2;
4765 rettv->v_type = VAR_NUMBER;
4766 rettv->vval.v_number = n1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 }
4772 return OK;
4773}
4774
4775/*
4776 * Handle fifth level expression:
4777 * * number multiplication
4778 * / number division
4779 * % number modulo
4780 *
4781 * "arg" must point to the first non-white of the expression.
4782 * "arg" is advanced to the next non-white after the recognized expression.
4783 *
4784 * Return OK or FAIL.
4785 */
4786 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int op;
4795 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 int use_float = FALSE;
4798 float_T f1 = 0, f2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Get the first variable.
4804 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
4807
4808 /*
4809 * Repeat computing, until no '*', '/' or '%' is following.
4810 */
4811 for (;;)
4812 {
4813 op = **arg;
4814 if (op != '*' && op != '/' && op != '%')
4815 break;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 {
4822 f1 = rettv->vval.v_float;
4823 use_float = TRUE;
4824 n1 = 0;
4825 }
4826 else
4827#endif
4828 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (error)
4831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 else
4834 n1 = 0;
4835
4836 /*
4837 * Get the second variable.
4838 */
4839 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004840 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return FAIL;
4842
4843 if (evaluate)
4844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 if (!use_float)
4849 {
4850 f1 = n1;
4851 use_float = TRUE;
4852 }
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 clear_tv(&var2);
4861 if (error)
4862 return FAIL;
4863#ifdef FEAT_FLOAT
4864 if (use_float)
4865 f2 = n2;
4866#endif
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 /*
4870 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 if (op == '*')
4877 f1 = f1 * f2;
4878 else if (op == '/')
4879 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004880# ifdef VMS
4881 /* VMS crashes on divide by zero, work around it */
4882 if (f2 == 0.0)
4883 {
4884 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004887 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004888 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004889 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890 }
4891 else
4892 f1 = f1 / f2;
4893# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 /* We rely on the floating point library to handle divide
4895 * by zero to result in "inf" and not a crash. */
4896 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004901 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 return FAIL;
4903 }
4904 rettv->v_type = VAR_FLOAT;
4905 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 if (op == '*')
4911 n1 = n1 * n2;
4912 else if (op == '/')
4913 {
4914 if (n2 == 0) /* give an error message? */
4915 {
4916 if (n1 == 0)
4917 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4918 else if (n1 < 0)
4919 n1 = -0x7fffffffL;
4920 else
4921 n1 = 0x7fffffffL;
4922 }
4923 else
4924 n1 = n1 / n2;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 {
4928 if (n2 == 0) /* give an error message? */
4929 n1 = 0;
4930 else
4931 n1 = n1 % n2;
4932 }
4933 rettv->v_type = VAR_NUMBER;
4934 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938
4939 return OK;
4940}
4941
4942/*
4943 * Handle sixth level expression:
4944 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004945 * "string" string constant
4946 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * &option-name option value
4948 * @r register contents
4949 * identifier variable value
4950 * function() function call
4951 * $VAR environment variable
4952 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004953 * [expr, expr] List
4954 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *
4956 * Also handle:
4957 * ! in front logical NOT
4958 * - in front unary minus
4959 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 * trailing [] subscript in String or List
4961 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * "arg" must point to the first non-white of the expression.
4964 * "arg" is advanced to the next non-white after the recognized expression.
4965 *
4966 * Return OK or FAIL.
4967 */
4968 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004969eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004973 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 long n;
4976 int len;
4977 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *start_leader, *end_leader;
4979 int ret = OK;
4980 char_u *alias;
4981
4982 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * Skip '!' and '-' characters. They are handled later.
4990 */
4991 start_leader = *arg;
4992 while (**arg == '!' || **arg == '-' || **arg == '+')
4993 *arg = skipwhite(*arg + 1);
4994 end_leader = *arg;
4995
4996 switch (**arg)
4997 {
4998 /*
4999 * Number constant.
5000 */
5001 case '0':
5002 case '1':
5003 case '2':
5004 case '3':
5005 case '4':
5006 case '5':
5007 case '6':
5008 case '7':
5009 case '8':
5010 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012#ifdef FEAT_FLOAT
5013 char_u *p = skipdigits(*arg + 1);
5014 int get_float = FALSE;
5015
5016 /* We accept a float when the format matches
5017 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005018 * strict to avoid backwards compatibility problems.
5019 * Don't look for a float after the "." operator, so that
5020 * ":let vers = 1.2.3" doesn't fail. */
5021 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 get_float = TRUE;
5024 p = skipdigits(p + 2);
5025 if (*p == 'e' || *p == 'E')
5026 {
5027 ++p;
5028 if (*p == '-' || *p == '+')
5029 ++p;
5030 if (!vim_isdigit(*p))
5031 get_float = FALSE;
5032 else
5033 p = skipdigits(p + 1);
5034 }
5035 if (ASCII_ISALPHA(*p) || *p == '.')
5036 get_float = FALSE;
5037 }
5038 if (get_float)
5039 {
5040 float_T f;
5041
5042 *arg += string2float(*arg, &f);
5043 if (evaluate)
5044 {
5045 rettv->v_type = VAR_FLOAT;
5046 rettv->vval.v_float = f;
5047 }
5048 }
5049 else
5050#endif
5051 {
5052 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5053 *arg += len;
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_NUMBER;
5057 rettv->vval.v_number = n;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 /*
5064 * String constant: "string".
5065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068
5069 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 break;
5074
5075 /*
5076 * List: [expr, expr]
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Dictionary: {key: val, key: val}
5083 */
5084 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5085 break;
5086
5087 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
5094 * Environment variable: $VAR.
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
5100 * Register contents: @r.
5101 */
5102 case '@': ++*arg;
5103 if (evaluate)
5104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005106 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 if (**arg != NUL)
5109 ++*arg;
5110 break;
5111
5112 /*
5113 * nested expression: (expression).
5114 */
5115 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (**arg == ')')
5118 ++*arg;
5119 else if (ret == OK)
5120 {
5121 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ret = FAIL;
5124 }
5125 break;
5126
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130
5131 if (ret == NOTDONE)
5132 {
5133 /*
5134 * Must be a variable or function name.
5135 * Can also be a curly-braces kind of name: {expr}.
5136 */
5137 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (alias != NULL)
5140 s = alias;
5141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 ret = FAIL;
5144 else
5145 {
5146 if (**arg == '(') /* recursive! */
5147 {
5148 /* If "s" is the name of a variable of type VAR_FUNC
5149 * use its contents. */
5150 s = deref_func_name(s, &len);
5151
5152 /* Invoke the function. */
5153 ret = get_func_tv(s, len, rettv, arg,
5154 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005156
5157 /* If evaluate is FALSE rettv->v_type was not set in
5158 * get_func_tv, but it's needed in handle_subscript() to parse
5159 * what follows. So set it here. */
5160 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5161 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005162 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005163 rettv->v_type = VAR_FUNC;
5164 }
5165
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /* Stop the expression evaluation when immediately
5167 * aborting on error, or when an interrupt occurred or
5168 * an exception was thrown but not caught. */
5169 if (aborting())
5170 {
5171 if (ret == OK)
5172 clear_tv(rettv);
5173 ret = FAIL;
5174 }
5175 }
5176 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005178 else
5179 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005181 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *arg = skipwhite(*arg);
5185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5187 * expr(expr). */
5188 if (ret == OK)
5189 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 /*
5192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5193 */
5194 if (ret == OK && evaluate && end_leader > start_leader)
5195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 int val = 0;
5198#ifdef FEAT_FLOAT
5199 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 if (rettv->v_type == VAR_FLOAT)
5202 f = rettv->vval.v_float;
5203 else
5204#endif
5205 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 clear_tv(rettv);
5209 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else
5212 {
5213 while (end_leader > start_leader)
5214 {
5215 --end_leader;
5216 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 {
5218#ifdef FEAT_FLOAT
5219 if (rettv->v_type == VAR_FLOAT)
5220 f = !f;
5221 else
5222#endif
5223 val = !val;
5224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 {
5227#ifdef FEAT_FLOAT
5228 if (rettv->v_type == VAR_FLOAT)
5229 f = -f;
5230 else
5231#endif
5232 val = -val;
5233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 {
5238 clear_tv(rettv);
5239 rettv->vval.v_float = f;
5240 }
5241 else
5242#endif
5243 {
5244 clear_tv(rettv);
5245 rettv->v_type = VAR_NUMBER;
5246 rettv->vval.v_number = val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return ret;
5252}
5253
5254/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5256 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5258 */
5259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265{
5266 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 long len = -1;
5270 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005274 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (verbose)
5277 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 return FAIL;
5279 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005280#ifdef FEAT_FLOAT
5281 else if (rettv->v_type == VAR_FLOAT)
5282 {
5283 if (verbose)
5284 EMSG(_(e_float_as_string));
5285 return FAIL;
5286 }
5287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 /*
5292 * dict.name
5293 */
5294 key = *arg + 1;
5295 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5296 ;
5297 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * something[idx]
5305 *
5306 * Get the (first) variable from inside the [].
5307 */
5308 *arg = skipwhite(*arg + 1);
5309 if (**arg == ':')
5310 empty1 = TRUE;
5311 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5312 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5314 {
5315 /* not a number or string */
5316 clear_tv(&var1);
5317 return FAIL;
5318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319
5320 /*
5321 * Get the second variable from inside the [:].
5322 */
5323 if (**arg == ':')
5324 {
5325 range = TRUE;
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ']')
5328 empty2 = TRUE;
5329 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 if (!empty1)
5332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5336 {
5337 /* not a number or string */
5338 if (!empty1)
5339 clear_tv(&var1);
5340 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 return FAIL;
5342 }
5343 }
5344
5345 /* Check for the ']'. */
5346 if (**arg != ']')
5347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 clear_tv(&var1);
5351 if (range)
5352 clear_tv(&var2);
5353 return FAIL;
5354 }
5355 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357
5358 if (evaluate)
5359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 n1 = 0;
5361 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n1 = get_tv_number(&var1);
5364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 if (range)
5367 {
5368 if (empty2)
5369 n2 = -1;
5370 else
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 n2 = get_tv_number(&var2);
5373 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 }
5376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
5379 case VAR_NUMBER:
5380 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 len = (long)STRLEN(s);
5383 if (range)
5384 {
5385 /* The resulting variable is a substring. If the indexes
5386 * are out of range the result is empty. */
5387 if (n1 < 0)
5388 {
5389 n1 = len + n1;
5390 if (n1 < 0)
5391 n1 = 0;
5392 }
5393 if (n2 < 0)
5394 n2 = len + n2;
5395 else if (n2 >= len)
5396 n2 = len;
5397 if (n1 >= len || n2 < 0 || n1 > n2)
5398 s = NULL;
5399 else
5400 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5401 }
5402 else
5403 {
5404 /* The resulting variable is a string of a single
5405 * character. If the index is too big or negative the
5406 * result is empty. */
5407 if (n1 >= len || n1 < 0)
5408 s = NULL;
5409 else
5410 s = vim_strnsave(s + n1, 1);
5411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(rettv);
5413 rettv->v_type = VAR_STRING;
5414 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 break;
5416
5417 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 if (n1 < 0)
5420 n1 = len + n1;
5421 if (!empty1 && (n1 < 0 || n1 >= len))
5422 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005423 /* For a range we allow invalid values and return an empty
5424 * list. A list index out of range is an error. */
5425 if (!range)
5426 {
5427 if (verbose)
5428 EMSGN(_(e_listidx), n1);
5429 return FAIL;
5430 }
5431 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 if (range)
5434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 list_T *l;
5436 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437
5438 if (n2 < 0)
5439 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 else if (n2 >= len)
5441 n2 = len - 1;
5442 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005443 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 l = list_alloc();
5445 if (l == NULL)
5446 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 n1 <= n2; ++n1)
5449 {
5450 if (list_append_tv(l, &item->li_tv) == FAIL)
5451 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005452 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 return FAIL;
5454 }
5455 item = item->li_next;
5456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 clear_tv(rettv);
5458 rettv->v_type = VAR_LIST;
5459 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005460 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 else
5463 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005464 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 clear_tv(rettv);
5466 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469
5470 case VAR_DICT:
5471 if (range)
5472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473 if (verbose)
5474 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 if (len == -1)
5476 clear_tv(&var1);
5477 return FAIL;
5478 }
5479 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 if (len == -1)
5483 {
5484 key = get_tv_string(&var1);
5485 if (*key == NUL)
5486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (verbose)
5488 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 }
5493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005497 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (len == -1)
5499 clear_tv(&var1);
5500 if (item == NULL)
5501 return FAIL;
5502
5503 copy_tv(&item->di_tv, &var1);
5504 clear_tv(rettv);
5505 *rettv = var1;
5506 }
5507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
5509 }
5510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 return OK;
5512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * Get an option value.
5516 * "arg" points to the '&' or '+' before the option name.
5517 * "arg" is advanced to character after the option name.
5518 * Return OK or FAIL.
5519 */
5520 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int evaluate;
5525{
5526 char_u *option_end;
5527 long numval;
5528 char_u *stringval;
5529 int opt_type;
5530 int c;
5531 int working = (**arg == '+'); /* has("+option") */
5532 int ret = OK;
5533 int opt_flags;
5534
5535 /*
5536 * Isolate the option name and find its value.
5537 */
5538 option_end = find_option_end(arg, &opt_flags);
5539 if (option_end == NULL)
5540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 EMSG2(_("E112: Option name missing: %s"), *arg);
5543 return FAIL;
5544 }
5545
5546 if (!evaluate)
5547 {
5548 *arg = option_end;
5549 return OK;
5550 }
5551
5552 c = *option_end;
5553 *option_end = NUL;
5554 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 if (opt_type == -3) /* invalid name */
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E113: Unknown option: %s"), *arg);
5561 ret = FAIL;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 if (opt_type == -2) /* hidden string option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_STRING;
5568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else if (opt_type == -1) /* hidden number option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_NUMBER;
5573 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else if (opt_type == 1) /* number option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_NUMBER;
5578 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 else /* string option */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->v_type = VAR_STRING;
5583 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 }
5586 else if (working && (opt_type == -2 || opt_type == -1))
5587 ret = FAIL;
5588
5589 *option_end = c; /* put back for error messages */
5590 *arg = option_end;
5591
5592 return ret;
5593}
5594
5595/*
5596 * Allocate a variable for a string constant.
5597 * Return OK or FAIL.
5598 */
5599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int evaluate;
5604{
5605 char_u *p;
5606 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int extra = 0;
5608
5609 /*
5610 * Find the end of the string, skipping backslashed characters.
5611 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 if (*p == '\\' && p[1] != NUL)
5615 {
5616 ++p;
5617 /* A "\<x>" form occupies at least 4 characters, and produces up
5618 * to 6 characters: reserve space for 2 extra */
5619 if (*p == '<')
5620 extra += 2;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623
5624 if (*p != '"')
5625 {
5626 EMSG2(_("E114: Missing quote: %s"), *arg);
5627 return FAIL;
5628 }
5629
5630 /* If only parsing, set *arg and return here */
5631 if (!evaluate)
5632 {
5633 *arg = p + 1;
5634 return OK;
5635 }
5636
5637 /*
5638 * Copy the string into allocated memory, handling backslashed
5639 * characters.
5640 */
5641 name = alloc((unsigned)(p - *arg + extra));
5642 if (name == NULL)
5643 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 if (*p == '\\')
5650 {
5651 switch (*++p)
5652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 case 'b': *name++ = BS; ++p; break;
5654 case 'e': *name++ = ESC; ++p; break;
5655 case 'f': *name++ = FF; ++p; break;
5656 case 'n': *name++ = NL; ++p; break;
5657 case 'r': *name++ = CAR; ++p; break;
5658 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 case 'X': /* hex: "\x1", "\x12" */
5661 case 'x':
5662 case 'u': /* Unicode: "\u0023" */
5663 case 'U':
5664 if (vim_isxdigit(p[1]))
5665 {
5666 int n, nr;
5667 int c = toupper(*p);
5668
5669 if (c == 'X')
5670 n = 2;
5671 else
5672 n = 4;
5673 nr = 0;
5674 while (--n >= 0 && vim_isxdigit(p[1]))
5675 {
5676 ++p;
5677 nr = (nr << 4) + hex2nr(*p);
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_MBYTE
5681 /* For "\u" store the number according to
5682 * 'encoding'. */
5683 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690
5691 /* octal: "\1", "\12", "\123" */
5692 case '0':
5693 case '1':
5694 case '2':
5695 case '3':
5696 case '4':
5697 case '5':
5698 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 case '7': *name = *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 *name = (*name << 3) + *p++ - '0';
5703 if (*p >= '0' && *p <= '7')
5704 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (extra != 0)
5712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715 }
5716 /* FALLTHROUGH */
5717
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 }
5721 }
5722 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 *arg = p + 1;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 return OK;
5730}
5731
5732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005737get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 int evaluate;
5741{
5742 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 int reduce = 0;
5745
5746 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 */
5749 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 break;
5755 ++reduce;
5756 ++p;
5757 }
5758 }
5759
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 return FAIL;
5764 }
5765
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 if (!evaluate)
5768 {
5769 *arg = p + 1;
5770 return OK;
5771 }
5772
5773 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 */
5776 str = alloc((unsigned)((p - *arg) - reduce));
5777 if (str == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 break;
5788 ++p;
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 *arg = p + 1;
5794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 return OK;
5796}
5797
5798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 * Allocate a variable for a List and fill it from "*arg".
5800 * Return OK or FAIL.
5801 */
5802 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005803get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 int evaluate;
5807{
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 list_T *l = NULL;
5809 typval_T tv;
5810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811
5812 if (evaluate)
5813 {
5814 l = list_alloc();
5815 if (l == NULL)
5816 return FAIL;
5817 }
5818
5819 *arg = skipwhite(*arg + 1);
5820 while (**arg != ']' && **arg != NUL)
5821 {
5822 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5823 goto failret;
5824 if (evaluate)
5825 {
5826 item = listitem_alloc();
5827 if (item != NULL)
5828 {
5829 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005830 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 list_append(l, item);
5832 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005833 else
5834 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 }
5836
5837 if (**arg == ']')
5838 break;
5839 if (**arg != ',')
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 goto failret;
5843 }
5844 *arg = skipwhite(*arg + 1);
5845 }
5846
5847 if (**arg != ']')
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850failret:
5851 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005852 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 return FAIL;
5854 }
5855
5856 *arg = skipwhite(*arg + 1);
5857 if (evaluate)
5858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 rettv->v_type = VAR_LIST;
5860 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 ++l->lv_refcount;
5862 }
5863
5864 return OK;
5865}
5866
5867/*
5868 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005869 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005871 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872list_alloc()
5873{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005874 list_T *l;
5875
5876 l = (list_T *)alloc_clear(sizeof(list_T));
5877 if (l != NULL)
5878 {
5879 /* Prepend the list to the list of lists for garbage collection. */
5880 if (first_list != NULL)
5881 first_list->lv_used_prev = l;
5882 l->lv_used_prev = NULL;
5883 l->lv_used_next = first_list;
5884 first_list = l;
5885 }
5886 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887}
5888
5889/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005890 * Allocate an empty list for a return value.
5891 * Returns OK or FAIL.
5892 */
5893 static int
5894rettv_list_alloc(rettv)
5895 typval_T *rettv;
5896{
5897 list_T *l = list_alloc();
5898
5899 if (l == NULL)
5900 return FAIL;
5901
5902 rettv->vval.v_list = l;
5903 rettv->v_type = VAR_LIST;
5904 ++l->lv_refcount;
5905 return OK;
5906}
5907
5908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 * Unreference a list: decrement the reference count and free it when it
5910 * becomes zero.
5911 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005912 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916 if (l != NULL && --l->lv_refcount <= 0)
5917 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
5921 * Free a list, including all items it points to.
5922 * Ignores the reference count.
5923 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005924 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005925list_free(l, recurse)
5926 list_T *l;
5927 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Remove the list from the list of lists for garbage collection. */
5932 if (l->lv_used_prev == NULL)
5933 first_list = l->lv_used_next;
5934 else
5935 l->lv_used_prev->lv_used_next = l->lv_used_next;
5936 if (l->lv_used_next != NULL)
5937 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5938
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005941 /* Remove the item before deleting it. */
5942 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943 if (recurse || (item->li_tv.v_type != VAR_LIST
5944 && item->li_tv.v_type != VAR_DICT))
5945 clear_tv(&item->li_tv);
5946 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948 vim_free(l);
5949}
5950
5951/*
5952 * Allocate a list item.
5953 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005954 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_alloc()
5956{
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958}
5959
5960/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005961 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 vim_free(item);
5969}
5970
5971/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Remove a list item from a List and free it. Also clears the value.
5973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005978{
5979 list_remove(l, item, item);
5980 listitem_free(item);
5981}
5982
5983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Get the number of items in a list.
5985 */
5986 static long
5987list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 if (l == NULL)
5991 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Return TRUE when two lists have exactly the same values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l1;
6001 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006007 if (l1 == NULL || l2 == NULL)
6008 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006009 if (l1 == l2)
6010 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011 if (list_len(l1) != list_len(l2))
6012 return FALSE;
6013
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 for (item1 = l1->lv_first, item2 = l2->lv_first;
6015 item1 != NULL && item2 != NULL;
6016 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 return FALSE;
6019 return item1 == NULL && item2 == NULL;
6020}
6021
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006022#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6023 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024/*
6025 * Return the dictitem that an entry in a hashtable points to.
6026 */
6027 dictitem_T *
6028dict_lookup(hi)
6029 hashitem_T *hi;
6030{
6031 return HI2DI(hi);
6032}
6033#endif
6034
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 * Return TRUE when two dictionaries have exactly the same key/values.
6037 */
6038 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 dict_T *d1;
6041 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 hashitem_T *hi;
6046 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006049 if (d1 == NULL || d2 == NULL)
6050 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (d1 == d2)
6052 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 if (dict_len(d1) != dict_len(d2))
6054 return FALSE;
6055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006056 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 if (!HASHITEM_EMPTY(hi))
6060 {
6061 item2 = dict_find(d2, hi->hi_key, -1);
6062 if (item2 == NULL)
6063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 return FALSE;
6066 --todo;
6067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 }
6069 return TRUE;
6070}
6071
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072static int tv_equal_recurse_limit;
6073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006076 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 */
6079 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 typval_T *tv1;
6082 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 int ic; /* ignore case */
6084 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
6086 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006087 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006089 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 * recursiveness to a limit. We guess they are equal then.
6096 * A fixed limit has the problem of still taking an awful long time.
6097 * Reduce the limit every time running into it. That should work fine for
6098 * deeply linked structures that are not recursively linked and catch
6099 * recursiveness quickly. */
6100 if (!recursive)
6101 tv_equal_recurse_limit = 1000;
6102 if (recursive_cnt >= tv_equal_recurse_limit)
6103 {
6104 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 ++recursive_cnt;
6112 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6113 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006114 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115
6116 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 ++recursive_cnt;
6118 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6119 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006120 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121
6122 case VAR_FUNC:
6123 return (tv1->vval.v_string != NULL
6124 && tv2->vval.v_string != NULL
6125 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6126
6127 case VAR_NUMBER:
6128 return tv1->vval.v_number == tv2->vval.v_number;
6129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006130#ifdef FEAT_FLOAT
6131 case VAR_FLOAT:
6132 return tv1->vval.v_float == tv2->vval.v_float;
6133#endif
6134
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135 case VAR_STRING:
6136 s1 = get_tv_string_buf(tv1, buf1);
6137 s2 = get_tv_string_buf(tv2, buf2);
6138 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140
6141 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 return TRUE;
6143}
6144
6145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 * Locate item with index "n" in list "l" and return it.
6147 * A negative index is counted from the end; -1 is the last item.
6148 * Returns NULL when "n" is out of range.
6149 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006150 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long n;
6154{
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 long idx;
6157
6158 if (l == NULL)
6159 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160
6161 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 n = l->lv_len + n;
6164
6165 /* Check for index out of range. */
6166 if (n < 0 || n >= l->lv_len)
6167 return NULL;
6168
6169 /* When there is a cached index may start search from there. */
6170 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172 if (n < l->lv_idx / 2)
6173 {
6174 /* closest to the start of the list */
6175 item = l->lv_first;
6176 idx = 0;
6177 }
6178 else if (n > (l->lv_idx + l->lv_len) / 2)
6179 {
6180 /* closest to the end of the list */
6181 item = l->lv_last;
6182 idx = l->lv_len - 1;
6183 }
6184 else
6185 {
6186 /* closest to the cached index */
6187 item = l->lv_idx_item;
6188 idx = l->lv_idx;
6189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 }
6191 else
6192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006193 if (n < l->lv_len / 2)
6194 {
6195 /* closest to the start of the list */
6196 item = l->lv_first;
6197 idx = 0;
6198 }
6199 else
6200 {
6201 /* closest to the end of the list */
6202 item = l->lv_last;
6203 idx = l->lv_len - 1;
6204 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006206
6207 while (n > idx)
6208 {
6209 /* search forward */
6210 item = item->li_next;
6211 ++idx;
6212 }
6213 while (n < idx)
6214 {
6215 /* search backward */
6216 item = item->li_prev;
6217 --idx;
6218 }
6219
6220 /* cache the used index */
6221 l->lv_idx = idx;
6222 l->lv_idx_item = item;
6223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return item;
6225}
6226
6227/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 * Get list item "l[idx]" as a number.
6229 */
6230 static long
6231list_find_nr(l, idx, errorp)
6232 list_T *l;
6233 long idx;
6234 int *errorp; /* set to TRUE when something wrong */
6235{
6236 listitem_T *li;
6237
6238 li = list_find(l, idx);
6239 if (li == NULL)
6240 {
6241 if (errorp != NULL)
6242 *errorp = TRUE;
6243 return -1L;
6244 }
6245 return get_tv_number_chk(&li->li_tv, errorp);
6246}
6247
6248/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006249 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6250 */
6251 char_u *
6252list_find_str(l, idx)
6253 list_T *l;
6254 long idx;
6255{
6256 listitem_T *li;
6257
6258 li = list_find(l, idx - 1);
6259 if (li == NULL)
6260 {
6261 EMSGN(_(e_listidx), idx);
6262 return NULL;
6263 }
6264 return get_tv_string(&li->li_tv);
6265}
6266
6267/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268 * Locate "item" list "l" and return its index.
6269 * Returns -1 when "item" is not in the list.
6270 */
6271 static long
6272list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 list_T *l;
6274 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275{
6276 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278
6279 if (l == NULL)
6280 return -1;
6281 idx = 0;
6282 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6283 ++idx;
6284 if (li == NULL)
6285 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006286 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287}
6288
6289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 * Append item "item" to the end of list "l".
6291 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 list_T *l;
6295 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296{
6297 if (l->lv_last == NULL)
6298 {
6299 /* empty list */
6300 l->lv_first = item;
6301 l->lv_last = item;
6302 item->li_prev = NULL;
6303 }
6304 else
6305 {
6306 l->lv_last->li_next = item;
6307 item->li_prev = l->lv_last;
6308 l->lv_last = item;
6309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 item->li_next = NULL;
6312}
6313
6314/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006318 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l;
6321 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 copy_tv(tv, &li->li_tv);
6328 list_append(l, li);
6329 return OK;
6330}
6331
6332/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006333 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 * Return FAIL when out of memory.
6335 */
6336 int
6337list_append_dict(list, dict)
6338 list_T *list;
6339 dict_T *dict;
6340{
6341 listitem_T *li = listitem_alloc();
6342
6343 if (li == NULL)
6344 return FAIL;
6345 li->li_tv.v_type = VAR_DICT;
6346 li->li_tv.v_lock = 0;
6347 li->li_tv.vval.v_dict = dict;
6348 list_append(list, li);
6349 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 return OK;
6351}
6352
6353/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356 * Returns FAIL when out of memory.
6357 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006358 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360 list_T *l;
6361 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363{
6364 listitem_T *li = listitem_alloc();
6365
6366 if (li == NULL)
6367 return FAIL;
6368 list_append(l, li);
6369 li->li_tv.v_type = VAR_STRING;
6370 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 if (str == NULL)
6372 li->li_tv.vval.v_string = NULL;
6373 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006374 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375 return FAIL;
6376 return OK;
6377}
6378
6379/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 * Append "n" to list "l".
6381 * Returns FAIL when out of memory.
6382 */
6383 static int
6384list_append_number(l, n)
6385 list_T *l;
6386 varnumber_T n;
6387{
6388 listitem_T *li;
6389
6390 li = listitem_alloc();
6391 if (li == NULL)
6392 return FAIL;
6393 li->li_tv.v_type = VAR_NUMBER;
6394 li->li_tv.v_lock = 0;
6395 li->li_tv.vval.v_number = n;
6396 list_append(l, li);
6397 return OK;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * If "item" is NULL append at the end.
6403 * Return FAIL when out of memory.
6404 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l;
6408 typval_T *tv;
6409 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412
6413 if (ni == NULL)
6414 return FAIL;
6415 copy_tv(tv, &ni->li_tv);
6416 if (item == NULL)
6417 /* Append new item at end of list. */
6418 list_append(l, ni);
6419 else
6420 {
6421 /* Insert new item before existing item. */
6422 ni->li_prev = item->li_prev;
6423 ni->li_next = item;
6424 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 ++l->lv_idx;
6428 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 l->lv_idx_item = NULL;
6433 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 }
6437 return OK;
6438}
6439
6440/*
6441 * Extend "l1" with "l2".
6442 * If "bef" is NULL append at the end, otherwise insert before this item.
6443 * Returns FAIL when out of memory.
6444 */
6445 static int
6446list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 list_T *l1;
6448 list_T *l2;
6449 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450{
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006452 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006454 /* We also quit the loop when we have inserted the original item count of
6455 * the list, avoid a hang when we extend a list with itself. */
6456 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6458 return FAIL;
6459 return OK;
6460}
6461
6462/*
6463 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6464 * Return FAIL when out of memory.
6465 */
6466 static int
6467list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006474 if (l1 == NULL || l2 == NULL)
6475 return FAIL;
6476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (l == NULL)
6480 return FAIL;
6481 tv->v_type = VAR_LIST;
6482 tv->vval.v_list = l;
6483
6484 /* append all items from the second list */
6485 return list_extend(l, l2, NULL);
6486}
6487
6488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Returns NULL when out of memory.
6493 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *copy;
6501 listitem_T *item;
6502 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 if (orig == NULL)
6505 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
6507 copy = list_alloc();
6508 if (copy != NULL)
6509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (copyID != 0)
6511 {
6512 /* Do this before adding the items, because one of the items may
6513 * refer back to this list. */
6514 orig->lv_copyID = copyID;
6515 orig->lv_copylist = copy;
6516 }
6517 for (item = orig->lv_first; item != NULL && !got_int;
6518 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 {
6520 ni = listitem_alloc();
6521 if (ni == NULL)
6522 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 {
6525 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6526 {
6527 vim_free(ni);
6528 break;
6529 }
6530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 list_append(copy, ni);
6534 }
6535 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 if (item != NULL)
6537 {
6538 list_unref(copy);
6539 copy = NULL;
6540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 }
6542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 return copy;
6544}
6545
6546/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006550 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l;
6553 listitem_T *item;
6554 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 /* notify watchers */
6559 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 list_fix_watch(l, ip);
6563 if (ip == item2)
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566
6567 if (item2->li_next == NULL)
6568 l->lv_last = item->li_prev;
6569 else
6570 item2->li_next->li_prev = item->li_prev;
6571 if (item->li_prev == NULL)
6572 l->lv_first = item2->li_next;
6573 else
6574 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006575 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576}
6577
6578/*
6579 * Return an allocated string with the string representation of a list.
6580 * May return NULL.
6581 */
6582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
6587 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 if (tv->vval.v_list == NULL)
6590 return NULL;
6591 ga_init2(&ga, (int)sizeof(char), 80);
6592 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 {
6595 vim_free(ga.ga_data);
6596 return NULL;
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 ga_append(&ga, ']');
6599 ga_append(&ga, NUL);
6600 return (char_u *)ga.ga_data;
6601}
6602
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006603typedef struct join_S {
6604 char_u *s;
6605 char_u *tofree;
6606} join_T;
6607
6608 static int
6609list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6610 garray_T *gap; /* to store the result in */
6611 list_T *l;
6612 char_u *sep;
6613 int echo_style;
6614 int copyID;
6615 garray_T *join_gap; /* to keep each list item string */
6616{
6617 int i;
6618 join_T *p;
6619 int len;
6620 int sumlen = 0;
6621 int first = TRUE;
6622 char_u *tofree;
6623 char_u numbuf[NUMBUFLEN];
6624 listitem_T *item;
6625 char_u *s;
6626
6627 /* Stringify each item in the list. */
6628 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6629 {
6630 if (echo_style)
6631 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6632 else
6633 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6634 if (s == NULL)
6635 return FAIL;
6636
6637 len = (int)STRLEN(s);
6638 sumlen += len;
6639
6640 ga_grow(join_gap, 1);
6641 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6642 if (tofree != NULL || s != numbuf)
6643 {
6644 p->s = s;
6645 p->tofree = tofree;
6646 }
6647 else
6648 {
6649 p->s = vim_strnsave(s, len);
6650 p->tofree = p->s;
6651 }
6652
6653 line_breakcheck();
6654 }
6655
6656 /* Allocate result buffer with its total size, avoid re-allocation and
6657 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6658 if (join_gap->ga_len >= 2)
6659 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6660 if (ga_grow(gap, sumlen + 2) == FAIL)
6661 return FAIL;
6662
6663 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6664 {
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(gap, sep);
6669 p = ((join_T *)join_gap->ga_data) + i;
6670
6671 if (p->s != NULL)
6672 ga_concat(gap, p->s);
6673 line_breakcheck();
6674 }
6675
6676 return OK;
6677}
6678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006685list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006689 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 garray_T join_ga;
6693 int retval;
6694 join_T *p;
6695 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6698 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6699
6700 /* Dispose each item in join_ga. */
6701 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703 p = (join_T *)join_ga.ga_data;
6704 for (i = 0; i < join_ga.ga_len; ++i)
6705 {
6706 vim_free(p->tofree);
6707 ++p;
6708 }
6709 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711
6712 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713}
6714
6715/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006716 * Garbage collection for lists and dictionaries.
6717 *
6718 * We use reference counts to be able to free most items right away when they
6719 * are no longer used. But for composite items it's possible that it becomes
6720 * unused while the reference count is > 0: When there is a recursive
6721 * reference. Example:
6722 * :let l = [1, 2, 3]
6723 * :let d = {9: l}
6724 * :let l[1] = d
6725 *
6726 * Since this is quite unusual we handle this with garbage collection: every
6727 * once in a while find out which lists and dicts are not referenced from any
6728 * variable.
6729 *
6730 * Here is a good reference text about garbage collection (refers to Python
6731 * but it applies to all reference-counting mechanisms):
6732 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734
6735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 * Do garbage collection for lists and dicts.
6737 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 int
6740garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 buf_T *buf;
6744 win_T *wp;
6745 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006746 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 int did_free;
6748 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006749#ifdef FEAT_WINDOWS
6750 tabpage_T *tp;
6751#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006753 /* Only do this once. */
6754 want_garbage_collect = FALSE;
6755 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006756 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006757
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758 /* We advance by two because we add one for items referenced through
6759 * previous_funccal. */
6760 current_copyID += COPYID_INC;
6761 copyID = current_copyID;
6762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 /*
6764 * 1. Go through all accessible variables and mark all lists and dicts
6765 * with copyID.
6766 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767
6768 /* Don't free variables in the previous_funccal list unless they are only
6769 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006770 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6775 }
6776
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 /* script-local variables */
6778 for (i = 1; i <= ga_scripts.ga_len; ++i)
6779 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6780
6781 /* buffer-local variables */
6782 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006783 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
6785 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006788#ifdef FEAT_AUTOCMD
6789 if (aucmd_win != NULL)
6790 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6791#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793#ifdef FEAT_WINDOWS
6794 /* tabpage-local variables */
6795 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006796 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006797#endif
6798
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* global variables */
6800 set_ref_in_ht(&globvarht, copyID);
6801
6802 /* function-local variables */
6803 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6804 {
6805 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6806 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6807 }
6808
Bram Moolenaard812df62008-11-09 12:46:09 +00006809 /* v: vars */
6810 set_ref_in_ht(&vimvarht, copyID);
6811
Bram Moolenaar1dced572012-04-05 16:54:08 +02006812#ifdef FEAT_LUA
6813 set_ref_in_lua(copyID);
6814#endif
6815
Bram Moolenaardb913952012-06-29 12:54:53 +02006816#ifdef FEAT_PYTHON
6817 set_ref_in_python(copyID);
6818#endif
6819
6820#ifdef FEAT_PYTHON3
6821 set_ref_in_python3(copyID);
6822#endif
6823
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006824 /*
6825 * 2. Free lists and dictionaries that are not referenced.
6826 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 did_free = free_unref_items(copyID);
6828
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006829 /*
6830 * 3. Check if any funccal can be freed now.
6831 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 for (pfc = &previous_funccal; *pfc != NULL; )
6833 {
6834 if (can_free_funccal(*pfc, copyID))
6835 {
6836 fc = *pfc;
6837 *pfc = fc->caller;
6838 free_funccal(fc, TRUE);
6839 did_free = TRUE;
6840 did_free_funccal = TRUE;
6841 }
6842 else
6843 pfc = &(*pfc)->caller;
6844 }
6845 if (did_free_funccal)
6846 /* When a funccal was freed some more items might be garbage
6847 * collected, so run again. */
6848 (void)garbage_collect();
6849
6850 return did_free;
6851}
6852
6853/*
6854 * Free lists and dictionaries that are no longer referenced.
6855 */
6856 static int
6857free_unref_items(copyID)
6858 int copyID;
6859{
6860 dict_T *dd;
6861 list_T *ll;
6862 int did_free = FALSE;
6863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 */
6867 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Free the Dictionary and ordinary items it contains, but don't
6871 * recurse into Lists and Dictionaries, they will be in the list
6872 * of dicts or list of lists. */
6873 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 did_free = TRUE;
6875
6876 /* restart, next dict may also have been freed */
6877 dd = first_dict;
6878 }
6879 else
6880 dd = dd->dv_used_next;
6881
6882 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 * Go through the list of lists and free items without the copyID.
6884 * But don't free a list that has a watcher (used in a for loop), these
6885 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 */
6887 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6889 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the List and ordinary items it contains, but don't recurse
6892 * into Lists and Dictionaries, they will be in the list of dicts
6893 * or list of lists. */
6894 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006897 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 ll = first_list;
6899 }
6900 else
6901 ll = ll->lv_used_next;
6902
6903 return did_free;
6904}
6905
6906/*
6907 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6908 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006909 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910set_ref_in_ht(ht, copyID)
6911 hashtab_T *ht;
6912 int copyID;
6913{
6914 int todo;
6915 hashitem_T *hi;
6916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 for (hi = ht->ht_array; todo > 0; ++hi)
6919 if (!HASHITEM_EMPTY(hi))
6920 {
6921 --todo;
6922 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6923 }
6924}
6925
6926/*
6927 * Mark all lists and dicts referenced through list "l" with "copyID".
6928 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006929 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930set_ref_in_list(l, copyID)
6931 list_T *l;
6932 int copyID;
6933{
6934 listitem_T *li;
6935
6936 for (li = l->lv_first; li != NULL; li = li->li_next)
6937 set_ref_in_item(&li->li_tv, copyID);
6938}
6939
6940/*
6941 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006943 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944set_ref_in_item(tv, copyID)
6945 typval_T *tv;
6946 int copyID;
6947{
6948 dict_T *dd;
6949 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006950
6951 switch (tv->v_type)
6952 {
6953 case VAR_DICT:
6954 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006955 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 /* Didn't see this dict yet. */
6958 dd->dv_copyID = copyID;
6959 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962
6963 case VAR_LIST:
6964 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006965 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* Didn't see this list yet. */
6968 ll->lv_copyID = copyID;
6969 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006974}
6975
6976/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977 * Allocate an empty header for a dictionary.
6978 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980dict_alloc()
6981{
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 if (first_dict != NULL)
6989 first_dict->dv_used_prev = d;
6990 d->dv_used_next = first_dict;
6991 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006996 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001}
7002
7003/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007004 * Allocate an empty dict for a return value.
7005 * Returns OK or FAIL.
7006 */
7007 static int
7008rettv_dict_alloc(rettv)
7009 typval_T *rettv;
7010{
7011 dict_T *d = dict_alloc();
7012
7013 if (d == NULL)
7014 return FAIL;
7015
7016 rettv->vval.v_dict = d;
7017 rettv->v_type = VAR_DICT;
7018 ++d->dv_refcount;
7019 return OK;
7020}
7021
7022
7023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024 * Unreference a Dictionary: decrement the reference count and free it when it
7025 * becomes zero.
7026 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007027 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031 if (d != NULL && --d->dv_refcount <= 0)
7032 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033}
7034
7035/*
7036 * Free a Dictionary, including all items it contains.
7037 * Ignores the reference count.
7038 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007039 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007040dict_free(d, recurse)
7041 dict_T *d;
7042 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 /* Remove the dict from the list of dicts for garbage collection. */
7049 if (d->dv_used_prev == NULL)
7050 first_dict = d->dv_used_next;
7051 else
7052 d->dv_used_prev->dv_used_next = d->dv_used_next;
7053 if (d->dv_used_next != NULL)
7054 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7055
7056 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007057 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (!HASHITEM_EMPTY(hi))
7062 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007063 /* Remove the item before deleting it, just in case there is
7064 * something recursive causing trouble. */
7065 di = HI2DI(hi);
7066 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067 if (recurse || (di->di_tv.v_type != VAR_LIST
7068 && di->di_tv.v_type != VAR_DICT))
7069 clear_tv(&di->di_tv);
7070 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 --todo;
7072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 vim_free(d);
7076}
7077
7078/*
7079 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 * The "key" is copied to the new item.
7081 * Note that the value of the item "di_tv" still needs to be initialized!
7082 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007084 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085dictitem_alloc(key)
7086 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087{
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007090 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007094 di->di_flags = 0;
7095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
7099/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 * Make a copy of a Dictionary item.
7101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7109 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 if (di != NULL)
7111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 copy_tv(&org->di_tv, &di->di_tv);
7115 }
7116 return di;
7117}
7118
7119/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 * Remove item "item" from Dictionary "dict" and free it.
7121 */
7122 static void
7123dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 dict_T *dict;
7125 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 if (HASHITEM_EMPTY(hi))
7131 EMSG2(_(e_intern2), "dictitem_remove()");
7132 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 dictitem_free(item);
7135}
7136
7137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 * Free a dict item. Also clears the value.
7139 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007140 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 clear_tv(&item->di_tv);
7145 vim_free(item);
7146}
7147
7148/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7150 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Returns NULL when out of memory.
7153 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *copy;
7161 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164
7165 if (orig == NULL)
7166 return NULL;
7167
7168 copy = dict_alloc();
7169 if (copy != NULL)
7170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007171 if (copyID != 0)
7172 {
7173 orig->dv_copyID = copyID;
7174 orig->dv_copydict = copy;
7175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007176 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007177 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 --todo;
7182
7183 di = dictitem_alloc(hi->hi_key);
7184 if (di == NULL)
7185 break;
7186 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 {
7188 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7189 copyID) == FAIL)
7190 {
7191 vim_free(di);
7192 break;
7193 }
7194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 else
7196 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7197 if (dict_add(copy, di) == FAIL)
7198 {
7199 dictitem_free(di);
7200 break;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 if (todo > 0)
7207 {
7208 dict_unref(copy);
7209 copy = NULL;
7210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 }
7212
7213 return copy;
7214}
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007218 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007220 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 dict_T *d;
7223 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007229 * Add a number or string entry to dictionary "d".
7230 * When "str" is NULL use number "nr", otherwise use "str".
7231 * Returns FAIL when out of memory and when key already exists.
7232 */
7233 int
7234dict_add_nr_str(d, key, nr, str)
7235 dict_T *d;
7236 char *key;
7237 long nr;
7238 char_u *str;
7239{
7240 dictitem_T *item;
7241
7242 item = dictitem_alloc((char_u *)key);
7243 if (item == NULL)
7244 return FAIL;
7245 item->di_tv.v_lock = 0;
7246 if (str == NULL)
7247 {
7248 item->di_tv.v_type = VAR_NUMBER;
7249 item->di_tv.vval.v_number = nr;
7250 }
7251 else
7252 {
7253 item->di_tv.v_type = VAR_STRING;
7254 item->di_tv.vval.v_string = vim_strsave(str);
7255 }
7256 if (dict_add(d, item) == FAIL)
7257 {
7258 dictitem_free(item);
7259 return FAIL;
7260 }
7261 return OK;
7262}
7263
7264/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007265 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007266 * Returns FAIL when out of memory and when key already exists.
7267 */
7268 int
7269dict_add_list(d, key, list)
7270 dict_T *d;
7271 char *key;
7272 list_T *list;
7273{
7274 dictitem_T *item;
7275
7276 item = dictitem_alloc((char_u *)key);
7277 if (item == NULL)
7278 return FAIL;
7279 item->di_tv.v_lock = 0;
7280 item->di_tv.v_type = VAR_LIST;
7281 item->di_tv.vval.v_list = list;
7282 if (dict_add(d, item) == FAIL)
7283 {
7284 dictitem_free(item);
7285 return FAIL;
7286 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007287 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 return OK;
7289}
7290
7291/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 * Get the number of items in a Dictionary.
7293 */
7294 static long
7295dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (d == NULL)
7299 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301}
7302
7303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Find item "key[len]" in Dictionary "d".
7305 * If "len" is negative use strlen(key).
7306 * Returns NULL when not found.
7307 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007308 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 char_u *key;
7312 int len;
7313{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314#define AKEYLEN 200
7315 char_u buf[AKEYLEN];
7316 char_u *akey;
7317 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (len < 0)
7321 akey = key;
7322 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 tofree = akey = vim_strnsave(key, len);
7325 if (akey == NULL)
7326 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 else
7329 {
7330 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007331 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 akey = buf;
7333 }
7334
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 vim_free(tofree);
7337 if (HASHITEM_EMPTY(hi))
7338 return NULL;
7339 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 * Get a string item from a dictionary.
7344 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345 * Returns NULL if the entry doesn't exist or out of memory.
7346 */
7347 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349 dict_T *d;
7350 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352{
7353 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355
7356 di = dict_find(d, key, -1);
7357 if (di == NULL)
7358 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007359 s = get_tv_string(&di->di_tv);
7360 if (save && s != NULL)
7361 s = vim_strsave(s);
7362 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007363}
7364
7365/*
7366 * Get a number item from a dictionary.
7367 * Returns 0 if the entry doesn't exist or out of memory.
7368 */
7369 long
7370get_dict_number(d, key)
7371 dict_T *d;
7372 char_u *key;
7373{
7374 dictitem_T *di;
7375
7376 di = dict_find(d, key, -1);
7377 if (di == NULL)
7378 return 0;
7379 return get_tv_number(&di->di_tv);
7380}
7381
7382/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 * Return an allocated string with the string representation of a Dictionary.
7384 * May return NULL.
7385 */
7386 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
7391 garray_T ga;
7392 int first = TRUE;
7393 char_u *tofree;
7394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 return NULL;
7402 ga_init2(&ga, (int)sizeof(char), 80);
7403 ga_append(&ga, '{');
7404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007405 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 --todo;
7411
7412 if (first)
7413 first = FALSE;
7414 else
7415 ga_concat(&ga, (char_u *)", ");
7416
7417 tofree = string_quote(hi->hi_key, FALSE);
7418 if (tofree != NULL)
7419 {
7420 ga_concat(&ga, tofree);
7421 vim_free(tofree);
7422 }
7423 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (s != NULL)
7426 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007428 if (s == NULL)
7429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 if (todo > 0)
7433 {
7434 vim_free(ga.ga_data);
7435 return NULL;
7436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437
7438 ga_append(&ga, '}');
7439 ga_append(&ga, NUL);
7440 return (char_u *)ga.ga_data;
7441}
7442
7443/*
7444 * Allocate a variable for a Dictionary and fill it from "*arg".
7445 * Return OK or FAIL. Returns NOTDONE for {expr}.
7446 */
7447 static int
7448get_dict_tv(arg, rettv, evaluate)
7449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 int evaluate;
7452{
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 dict_T *d = NULL;
7454 typval_T tvkey;
7455 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007456 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460
7461 /*
7462 * First check if it's not a curly-braces thing: {expr}.
7463 * Must do this without evaluating, otherwise a function may be called
7464 * twice. Unfortunately this means we need to call eval1() twice for the
7465 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 if (*start != '}')
7469 {
7470 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7471 return FAIL;
7472 if (*start == '}')
7473 return NOTDONE;
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 if (evaluate)
7477 {
7478 d = dict_alloc();
7479 if (d == NULL)
7480 return FAIL;
7481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 tvkey.v_type = VAR_UNKNOWN;
7483 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484
7485 *arg = skipwhite(*arg + 1);
7486 while (**arg != '}' && **arg != NUL)
7487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 goto failret;
7490 if (**arg != ':')
7491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007492 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 goto failret;
7495 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007496 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007498 key = get_tv_string_buf_chk(&tvkey, buf);
7499 if (key == NULL || *key == NUL)
7500 {
7501 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7502 if (key != NULL)
7503 EMSG(_(e_emptykey));
7504 clear_tv(&tvkey);
7505 goto failret;
7506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 *arg = skipwhite(*arg + 1);
7510 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7511 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007512 if (evaluate)
7513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 goto failret;
7515 }
7516 if (evaluate)
7517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 if (item != NULL)
7520 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007521 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 clear_tv(&tv);
7524 goto failret;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 item = dictitem_alloc(key);
7527 clear_tv(&tvkey);
7528 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (dict_add(d, item) == FAIL)
7533 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
7535 }
7536
7537 if (**arg == '}')
7538 break;
7539 if (**arg != ',')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 goto failret;
7543 }
7544 *arg = skipwhite(*arg + 1);
7545 }
7546
7547 if (**arg != '}')
7548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550failret:
7551 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return FAIL;
7554 }
7555
7556 *arg = skipwhite(*arg + 1);
7557 if (evaluate)
7558 {
7559 rettv->v_type = VAR_DICT;
7560 rettv->vval.v_dict = d;
7561 ++d->dv_refcount;
7562 }
7563
7564 return OK;
7565}
7566
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 * Return a string with the string representation of a variable.
7569 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007573 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 */
7575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007579 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 static int recurse = 0;
7583 char_u *r = NULL;
7584
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007587 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 *tofree = NULL;
7589 return NULL;
7590 }
7591 ++recurse;
7592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 switch (tv->v_type)
7594 {
7595 case VAR_FUNC:
7596 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 r = tv->vval.v_string;
7598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 if (tv->vval.v_list == NULL)
7602 {
7603 *tofree = NULL;
7604 r = NULL;
7605 }
7606 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7607 {
7608 *tofree = NULL;
7609 r = (char_u *)"[...]";
7610 }
7611 else
7612 {
7613 tv->vval.v_list->lv_copyID = copyID;
7614 *tofree = list2string(tv, copyID);
7615 r = *tofree;
7616 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 if (tv->vval.v_dict == NULL)
7621 {
7622 *tofree = NULL;
7623 r = NULL;
7624 }
7625 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7626 {
7627 *tofree = NULL;
7628 r = (char_u *)"{...}";
7629 }
7630 else
7631 {
7632 tv->vval.v_dict->dv_copyID = copyID;
7633 *tofree = dict2string(tv, copyID);
7634 r = *tofree;
7635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 case VAR_STRING:
7639 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 *tofree = NULL;
7641 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007644#ifdef FEAT_FLOAT
7645 case VAR_FLOAT:
7646 *tofree = NULL;
7647 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7648 r = numbuf;
7649 break;
7650#endif
7651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656
7657 --recurse;
7658 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659}
7660
7661/*
7662 * Return a string with the string representation of a variable.
7663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7664 * "numbuf" is used for a number.
7665 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007666 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 */
7668 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671 char_u **tofree;
7672 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007673 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674{
7675 switch (tv->v_type)
7676 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 case VAR_FUNC:
7678 *tofree = string_quote(tv->vval.v_string, TRUE);
7679 return *tofree;
7680 case VAR_STRING:
7681 *tofree = string_quote(tv->vval.v_string, FALSE);
7682 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#ifdef FEAT_FLOAT
7684 case VAR_FLOAT:
7685 *tofree = NULL;
7686 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7687 return numbuf;
7688#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Return string "str" in ' quotes, doubling ' characters.
7701 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 */
7704 static char_u *
7705string_quote(str, function)
7706 char_u *str;
7707 int function;
7708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 char_u *p, *r, *s;
7711
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 len = (function ? 13 : 3);
7713 if (str != NULL)
7714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007715 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 for (p = str; *p != NUL; mb_ptr_adv(p))
7717 if (*p == '\'')
7718 ++len;
7719 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 s = r = alloc(len);
7721 if (r != NULL)
7722 {
7723 if (function)
7724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 r += 10;
7727 }
7728 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 if (str != NULL)
7731 for (p = str; *p != NUL; )
7732 {
7733 if (*p == '\'')
7734 *r++ = '\'';
7735 MB_COPY_CHAR(p, r);
7736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 if (function)
7739 *r++ = ')';
7740 *r++ = NUL;
7741 }
7742 return s;
7743}
7744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746/*
7747 * Convert the string "text" to a floating point number.
7748 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7749 * this always uses a decimal point.
7750 * Returns the length of the text that was consumed.
7751 */
7752 static int
7753string2float(text, value)
7754 char_u *text;
7755 float_T *value; /* result stored here */
7756{
7757 char *s = (char *)text;
7758 float_T f;
7759
7760 f = strtod(s, &s);
7761 *value = f;
7762 return (int)((char_u *)s - text);
7763}
7764#endif
7765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Get the value of an environment variable.
7768 * "arg" is pointing to the '$'. It is advanced to after the name.
7769 * If the environment variable was not set, silently assume it is empty.
7770 * Always return OK.
7771 */
7772 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 int evaluate;
7777{
7778 char_u *string = NULL;
7779 int len;
7780 int cc;
7781 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 ++*arg;
7785 name = *arg;
7786 len = get_env_len(arg);
7787 if (evaluate)
7788 {
7789 if (len != 0)
7790 {
7791 cc = name[len];
7792 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 /* first try vim_getenv(), fast for normal environment vars */
7794 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 {
7797 if (!mustfree)
7798 string = vim_strsave(string);
7799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007802 if (mustfree)
7803 vim_free(string);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* next try expanding things like $VIM and ${HOME} */
7806 string = expand_env_save(name - 1);
7807 if (string != NULL && *string == '$')
7808 {
7809 vim_free(string);
7810 string = NULL;
7811 }
7812 }
7813 name[len] = cc;
7814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->v_type = VAR_STRING;
7816 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
7819 return OK;
7820}
7821
7822/*
7823 * Array with names and number of arguments of all internal functions
7824 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7825 */
7826static struct fst
7827{
7828 char *f_name; /* function name */
7829 char f_min_argc; /* minimal number of arguments */
7830 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007832 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833} functions[] =
7834{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#ifdef FEAT_FLOAT
7836 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007839 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007840 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"append", 2, 2, f_append},
7842 {"argc", 0, 0, f_argc},
7843 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007844 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007846 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007848 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007851 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufexists", 1, 1, f_bufexists},
7853 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7854 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7855 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7856 {"buflisted", 1, 1, f_buflisted},
7857 {"bufloaded", 1, 1, f_bufloaded},
7858 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007859 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"bufwinnr", 1, 1, f_bufwinnr},
7861 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"ceil", 1, 1, f_ceil},
7866#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007867 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007868 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007872#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007873 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007874 {"complete_add", 1, 1, f_complete_add},
7875 {"complete_check", 0, 0, f_complete_check},
7876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007881 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007885 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007886 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"delete", 1, 1, f_delete},
7888 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007889 {"diff_filler", 1, 1, f_diff_filler},
7890 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007891 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"eventhandler", 0, 0, f_eventhandler},
7895 {"executable", 1, 1, f_executable},
7896 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007897#ifdef FEAT_FLOAT
7898 {"exp", 1, 1, f_exp},
7899#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007900 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007902 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7904 {"filereadable", 1, 1, f_filereadable},
7905 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007907 {"finddir", 1, 3, f_finddir},
7908 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"float2nr", 1, 1, f_float2nr},
7911 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007912 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007914 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"fnamemodify", 2, 2, f_fnamemodify},
7916 {"foldclosed", 1, 1, f_foldclosed},
7917 {"foldclosedend", 1, 1, f_foldclosedend},
7918 {"foldlevel", 1, 1, f_foldlevel},
7919 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007920 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007923 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007925 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007926 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getchar", 0, 1, f_getchar},
7928 {"getcharmod", 0, 0, f_getcharmod},
7929 {"getcmdline", 0, 0, f_getcmdline},
7930 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007931 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007933 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007934 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getfsize", 1, 1, f_getfsize},
7936 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007938 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007939 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007940 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007941 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007942 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007943 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007944 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007946 {"gettabvar", 2, 3, f_gettabvar},
7947 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getwinposx", 0, 0, f_getwinposx},
7949 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007950 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007951 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007952 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007955 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007956 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7958 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7959 {"histadd", 2, 2, f_histadd},
7960 {"histdel", 1, 2, f_histdel},
7961 {"histget", 1, 2, f_histget},
7962 {"histnr", 1, 1, f_histnr},
7963 {"hlID", 1, 1, f_hlID},
7964 {"hlexists", 1, 1, f_hlexists},
7965 {"hostname", 0, 0, f_hostname},
7966 {"iconv", 3, 3, f_iconv},
7967 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007969 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007971 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"inputrestore", 0, 0, f_inputrestore},
7973 {"inputsave", 0, 0, f_inputsave},
7974 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007976 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007978 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007983 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"libcall", 3, 3, f_libcall},
7985 {"libcallnr", 3, 3, f_libcallnr},
7986 {"line", 1, 1, f_line},
7987 {"line2byte", 1, 1, f_line2byte},
7988 {"lispindent", 1, 1, f_lispindent},
7989 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007991 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 {"log10", 1, 1, f_log10},
7993#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007994#ifdef FEAT_LUA
7995 {"luaeval", 1, 2, f_luaeval},
7996#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007998 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007999 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008000 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008002 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008005 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008006 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008007 {"max", 1, 1, f_max},
8008 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008009#ifdef vim_mkdir
8010 {"mkdir", 1, 3, f_mkdir},
8011#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008013#ifdef FEAT_MZSCHEME
8014 {"mzeval", 1, 1, f_mzeval},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008017 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008018 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008019 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"pow", 2, 2, f_pow},
8022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008024 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008025 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008026#ifdef FEAT_PYTHON3
8027 {"py3eval", 1, 1, f_py3eval},
8028#endif
8029#ifdef FEAT_PYTHON
8030 {"pyeval", 1, 1, f_pyeval},
8031#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008034 {"reltime", 0, 2, f_reltime},
8035 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"remote_expr", 2, 3, f_remote_expr},
8037 {"remote_foreground", 1, 1, f_remote_foreground},
8038 {"remote_peek", 1, 2, f_remote_peek},
8039 {"remote_read", 1, 1, f_remote_read},
8040 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008041 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008045 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
8047 {"round", 1, 1, f_round},
8048#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008049 {"screenattr", 2, 2, f_screenattr},
8050 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008051 {"screencol", 0, 0, f_screencol},
8052 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008053 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008054 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008055 {"searchpair", 3, 7, f_searchpair},
8056 {"searchpairpos", 3, 7, f_searchpairpos},
8057 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"server2client", 2, 2, f_server2client},
8059 {"serverlist", 0, 0, f_serverlist},
8060 {"setbufvar", 3, 3, f_setbufvar},
8061 {"setcmdpos", 1, 1, f_setcmdpos},
8062 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008063 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008064 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008065 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008066 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008068 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008069 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008071#ifdef FEAT_CRYPT
8072 {"sha256", 1, 1, f_sha256},
8073#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008074 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008075 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008081 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008082 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008083 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008084 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008085 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"sqrt", 1, 1, f_sqrt},
8088 {"str2float", 1, 1, f_str2float},
8089#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008090 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008092 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#ifdef HAVE_STRFTIME
8094 {"strftime", 1, 2, f_strftime},
8095#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"strlen", 1, 1, f_strlen},
8099 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008100 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008102 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"submatch", 1, 1, f_submatch},
8104 {"substitute", 4, 4, f_substitute},
8105 {"synID", 3, 3, f_synID},
8106 {"synIDattr", 2, 3, f_synIDattr},
8107 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008108 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008109 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008110 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008111 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008112 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008113 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008114 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008115 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116#ifdef FEAT_FLOAT
8117 {"tan", 1, 1, f_tan},
8118 {"tanh", 1, 1, f_tanh},
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008121 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"tolower", 1, 1, f_tolower},
8123 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008124 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008125#ifdef FEAT_FLOAT
8126 {"trunc", 1, 1, f_trunc},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008129 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008130 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"virtcol", 1, 1, f_virtcol},
8133 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008134 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"winbufnr", 1, 1, f_winbufnr},
8136 {"wincol", 0, 0, f_wincol},
8137 {"winheight", 1, 1, f_winheight},
8138 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008139 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008141 {"winrestview", 1, 1, f_winrestview},
8142 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008144 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008145 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146};
8147
8148#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8149
8150/*
8151 * Function given to ExpandGeneric() to obtain the list of internal
8152 * or user defined function names.
8153 */
8154 char_u *
8155get_function_name(xp, idx)
8156 expand_T *xp;
8157 int idx;
8158{
8159 static int intidx = -1;
8160 char_u *name;
8161
8162 if (idx == 0)
8163 intidx = -1;
8164 if (intidx < 0)
8165 {
8166 name = get_user_func_name(xp, idx);
8167 if (name != NULL)
8168 return name;
8169 }
8170 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8171 {
8172 STRCPY(IObuff, functions[intidx].f_name);
8173 STRCAT(IObuff, "(");
8174 if (functions[intidx].f_max_argc == 0)
8175 STRCAT(IObuff, ")");
8176 return IObuff;
8177 }
8178
8179 return NULL;
8180}
8181
8182/*
8183 * Function given to ExpandGeneric() to obtain the list of internal or
8184 * user defined variable or function names.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 char_u *
8187get_expr_name(xp, idx)
8188 expand_T *xp;
8189 int idx;
8190{
8191 static int intidx = -1;
8192 char_u *name;
8193
8194 if (idx == 0)
8195 intidx = -1;
8196 if (intidx < 0)
8197 {
8198 name = get_function_name(xp, idx);
8199 if (name != NULL)
8200 return name;
8201 }
8202 return get_user_var_name(xp, ++intidx);
8203}
8204
8205#endif /* FEAT_CMDL_COMPL */
8206
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008207#if defined(EBCDIC) || defined(PROTO)
8208/*
8209 * Compare struct fst by function name.
8210 */
8211 static int
8212compare_func_name(s1, s2)
8213 const void *s1;
8214 const void *s2;
8215{
8216 struct fst *p1 = (struct fst *)s1;
8217 struct fst *p2 = (struct fst *)s2;
8218
8219 return STRCMP(p1->f_name, p2->f_name);
8220}
8221
8222/*
8223 * Sort the function table by function name.
8224 * The sorting of the table above is ASCII dependant.
8225 * On machines using EBCDIC we have to sort it.
8226 */
8227 static void
8228sortFunctions()
8229{
8230 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8231
8232 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8233}
8234#endif
8235
8236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237/*
8238 * Find internal function in table above.
8239 * Return index, or -1 if not found
8240 */
8241 static int
8242find_internal_func(name)
8243 char_u *name; /* name of the function */
8244{
8245 int first = 0;
8246 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8247 int cmp;
8248 int x;
8249
8250 /*
8251 * Find the function name in the table. Binary search.
8252 */
8253 while (first <= last)
8254 {
8255 x = first + ((unsigned)(last - first) >> 1);
8256 cmp = STRCMP(name, functions[x].f_name);
8257 if (cmp < 0)
8258 last = x - 1;
8259 else if (cmp > 0)
8260 first = x + 1;
8261 else
8262 return x;
8263 }
8264 return -1;
8265}
8266
8267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8269 * name it contains, otherwise return "name".
8270 */
8271 static char_u *
8272deref_func_name(name, lenp)
8273 char_u *name;
8274 int *lenp;
8275{
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 int cc;
8278
8279 cc = name[*lenp];
8280 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008281 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 {
8287 *lenp = 0;
8288 return (char_u *)""; /* just in case */
8289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008290 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 }
8293
8294 return name;
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * Allocate a variable for the result of a function.
8299 * Return OK or FAIL.
8300 */
8301 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008302get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8303 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 char_u *name; /* name of the function */
8305 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 char_u **arg; /* argument, pointing to the '(' */
8308 linenr_T firstline; /* first line of range */
8309 linenr_T lastline; /* last line of range */
8310 int *doesrange; /* return: function handled range */
8311 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u *argp;
8315 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int argcount = 0; /* number of arguments found */
8318
8319 /*
8320 * Get the arguments.
8321 */
8322 argp = *arg;
8323 while (argcount < MAX_FUNC_ARGS)
8324 {
8325 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8326 if (*argp == ')' || *argp == ',' || *argp == NUL)
8327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8329 {
8330 ret = FAIL;
8331 break;
8332 }
8333 ++argcount;
8334 if (*argp != ',')
8335 break;
8336 }
8337 if (*argp == ')')
8338 ++argp;
8339 else
8340 ret = FAIL;
8341
8342 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 {
8347 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008348 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356 *arg = skipwhite(argp);
8357 return ret;
8358}
8359
8360
8361/*
8362 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008363 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008367call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008368 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008369 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008373 typval_T *argvars; /* vars for arguments, must have "argcount"
8374 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 linenr_T firstline; /* first line of range */
8376 linenr_T lastline; /* last line of range */
8377 int *doesrange; /* return: function handled range */
8378 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define ERROR_UNKNOWN 0
8383#define ERROR_TOOMANY 1
8384#define ERROR_TOOFEW 2
8385#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386#define ERROR_DICT 4
8387#define ERROR_NONE 5
8388#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 int error = ERROR_NONE;
8390 int i;
8391 int llen;
8392 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#define FLEN_FIXED 40
8394 char_u fname_buf[FLEN_FIXED + 1];
8395 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 char_u *name;
8397
8398 /* Make a copy of the name, if it comes from a funcref variable it could
8399 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008401 if (name == NULL)
8402 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8406 * Change <SNR>123_name() to K_SNR 123_name().
8407 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 llen = eval_fname_script(name);
8410 if (llen > 0)
8411 {
8412 fname_buf[0] = K_SPECIAL;
8413 fname_buf[1] = KS_EXTRA;
8414 fname_buf[2] = (int)KE_SNR;
8415 i = 3;
8416 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8417 {
8418 if (current_SID <= 0)
8419 error = ERROR_SCRIPT;
8420 else
8421 {
8422 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8423 i = (int)STRLEN(fname_buf);
8424 }
8425 }
8426 if (i + STRLEN(name + llen) < FLEN_FIXED)
8427 {
8428 STRCPY(fname_buf + i, name + llen);
8429 fname = fname_buf;
8430 }
8431 else
8432 {
8433 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8434 if (fname == NULL)
8435 error = ERROR_OTHER;
8436 else
8437 {
8438 mch_memmove(fname, fname_buf, (size_t)i);
8439 STRCPY(fname + i, name + llen);
8440 }
8441 }
8442 }
8443 else
8444 fname = name;
8445
8446 *doesrange = FALSE;
8447
8448
8449 /* execute the function if no errors detected and executing */
8450 if (evaluate && error == ERROR_NONE)
8451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008452 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 error = ERROR_UNKNOWN;
8455
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008456 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /*
8459 * User defined function.
8460 */
8461 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464 /* Trigger FuncUndefined event, may load the function. */
8465 if (fp == NULL
8466 && apply_autocmds(EVENT_FUNCUNDEFINED,
8467 fname, fname, TRUE, NULL)
8468 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 fp = find_func(fname);
8472 }
8473#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008475 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008476 {
8477 /* loaded a package, search for the function again */
8478 fp = find_func(fname);
8479 }
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (fp != NULL)
8482 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008485 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 else
8492 {
8493 /*
8494 * Call the user function.
8495 * Save and restore search patterns, script variables and
8496 * redo buffer.
8497 */
8498 save_search_patterns();
8499 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008500 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008503 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8504 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8505 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008506 /* Function was unreferenced while being used, free it
8507 * now. */
8508 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 restoreRedobuff();
8510 restore_search_patterns();
8511 error = ERROR_NONE;
8512 }
8513 }
8514 }
8515 else
8516 {
8517 /*
8518 * Find the function name in the table, call its implementation.
8519 */
8520 i = find_internal_func(fname);
8521 if (i >= 0)
8522 {
8523 if (argcount < functions[i].f_min_argc)
8524 error = ERROR_TOOFEW;
8525 else if (argcount > functions[i].f_max_argc)
8526 error = ERROR_TOOMANY;
8527 else
8528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_NONE;
8532 }
8533 }
8534 }
8535 /*
8536 * The function call (or "FuncUndefined" autocommand sequence) might
8537 * have been aborted by an error, an interrupt, or an explicitly thrown
8538 * exception that has not been caught so far. This situation can be
8539 * tested for by calling aborting(). For an error in an internal
8540 * function or for the "E132" error in call_user_func(), however, the
8541 * throw point at which the "force_abort" flag (temporarily reset by
8542 * emsg()) is normally updated has not been reached yet. We need to
8543 * update that flag first to make aborting() reliable.
8544 */
8545 update_force_abort();
8546 }
8547 if (error == ERROR_NONE)
8548 ret = OK;
8549
8550 /*
8551 * Report an error unless the argument evaluation or function call has been
8552 * cancelled due to an aborting error, an interrupt, or an exception.
8553 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 if (!aborting())
8555 {
8556 switch (error)
8557 {
8558 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 break;
8561 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 break;
8564 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008565 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 name);
8567 break;
8568 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008570 name);
8571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008573 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 name);
8575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 }
8577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (fname != name && fname != fname_buf)
8580 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008581 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 return ret;
8584}
8585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586/*
8587 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008588 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 */
8590 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591emsg_funcname(ermsg, name)
8592 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 char_u *name;
8594{
8595 char_u *p;
8596
8597 if (*name == K_SPECIAL)
8598 p = concat_str((char_u *)"<SNR>", name + 3);
8599 else
8600 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008601 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 if (p != name)
8603 vim_free(p);
8604}
8605
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606/*
8607 * Return TRUE for a non-zero Number and a non-empty String.
8608 */
8609 static int
8610non_zero_arg(argvars)
8611 typval_T *argvars;
8612{
8613 return ((argvars[0].v_type == VAR_NUMBER
8614 && argvars[0].vval.v_number != 0)
8615 || (argvars[0].v_type == VAR_STRING
8616 && argvars[0].vval.v_string != NULL
8617 && *argvars[0].vval.v_string != NUL));
8618}
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*********************************************
8621 * Implementation of the built-in functions
8622 */
8623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8626
8627/*
8628 * Get the float value of "argvars[0]" into "f".
8629 * Returns FAIL when the argument is not a Number or Float.
8630 */
8631 static int
8632get_float_arg(argvars, f)
8633 typval_T *argvars;
8634 float_T *f;
8635{
8636 if (argvars[0].v_type == VAR_FLOAT)
8637 {
8638 *f = argvars[0].vval.v_float;
8639 return OK;
8640 }
8641 if (argvars[0].v_type == VAR_NUMBER)
8642 {
8643 *f = (float_T)argvars[0].vval.v_number;
8644 return OK;
8645 }
8646 EMSG(_("E808: Number or Float required"));
8647 return FAIL;
8648}
8649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650/*
8651 * "abs(expr)" function
8652 */
8653 static void
8654f_abs(argvars, rettv)
8655 typval_T *argvars;
8656 typval_T *rettv;
8657{
8658 if (argvars[0].v_type == VAR_FLOAT)
8659 {
8660 rettv->v_type = VAR_FLOAT;
8661 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8662 }
8663 else
8664 {
8665 varnumber_T n;
8666 int error = FALSE;
8667
8668 n = get_tv_number_chk(&argvars[0], &error);
8669 if (error)
8670 rettv->vval.v_number = -1;
8671 else if (n > 0)
8672 rettv->vval.v_number = n;
8673 else
8674 rettv->vval.v_number = -n;
8675 }
8676}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677
8678/*
8679 * "acos()" function
8680 */
8681 static void
8682f_acos(argvars, rettv)
8683 typval_T *argvars;
8684 typval_T *rettv;
8685{
8686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = acos(f);
8691 else
8692 rettv->vval.v_float = 0.0;
8693}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008697 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 */
8699 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *argvars;
8702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008709 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008710 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 }
8714 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008715 EMSG(_(e_listreq));
8716}
8717
8718/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 * "and(expr, expr)" function
8720 */
8721 static void
8722f_and(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8727 & get_tv_number_chk(&argvars[1], NULL);
8728}
8729
8730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 * "append(lnum, string/list)" function
8732 */
8733 static void
8734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737{
8738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l = NULL;
8741 listitem_T *li = NULL;
8742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 long added = 0;
8744
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 lnum = get_tv_lnum(argvars);
8746 if (lnum >= 0
8747 && lnum <= curbuf->b_ml.ml_line_count
8748 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752 l = argvars[1].vval.v_list;
8753 if (l == NULL)
8754 return;
8755 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757 for (;;)
8758 {
8759 if (l == NULL)
8760 tv = &argvars[1]; /* append a string */
8761 else if (li == NULL)
8762 break; /* end of list */
8763 else
8764 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 line = get_tv_string_chk(tv);
8766 if (line == NULL) /* type error */
8767 {
8768 rettv->vval.v_number = 1; /* Failed */
8769 break;
8770 }
8771 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008772 ++added;
8773 if (l == NULL)
8774 break;
8775 li = li->li_next;
8776 }
8777
8778 appended_lines_mark(lnum, added);
8779 if (curwin->w_cursor.lnum > lnum)
8780 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 else
8783 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "argc()" function
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "argidx()" function
8799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808/*
8809 * "argv(nr)" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 int idx;
8817
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008818 if (argvars[0].v_type != VAR_UNKNOWN)
8819 {
8820 idx = get_tv_number_chk(&argvars[0], NULL);
8821 if (idx >= 0 && idx < ARGCOUNT)
8822 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8823 else
8824 rettv->vval.v_string = NULL;
8825 rettv->v_type = VAR_STRING;
8826 }
8827 else if (rettv_list_alloc(rettv) == OK)
8828 for (idx = 0; idx < ARGCOUNT; ++idx)
8829 list_append_string(rettv->vval.v_list,
8830 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831}
8832
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008833#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837 static void
8838f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008839 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008840 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008841{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = asin(f);
8847 else
8848 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849}
8850
8851/*
8852 * "atan()" function
8853 */
8854 static void
8855f_atan(argvars, rettv)
8856 typval_T *argvars;
8857 typval_T *rettv;
8858{
8859 float_T f;
8860
8861 rettv->v_type = VAR_FLOAT;
8862 if (get_float_arg(argvars, &f) == OK)
8863 rettv->vval.v_float = atan(f);
8864 else
8865 rettv->vval.v_float = 0.0;
8866}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008867
8868/*
8869 * "atan2()" function
8870 */
8871 static void
8872f_atan2(argvars, rettv)
8873 typval_T *argvars;
8874 typval_T *rettv;
8875{
8876 float_T fx, fy;
8877
8878 rettv->v_type = VAR_FLOAT;
8879 if (get_float_arg(argvars, &fx) == OK
8880 && get_float_arg(&argvars[1], &fy) == OK)
8881 rettv->vval.v_float = atan2(fx, fy);
8882 else
8883 rettv->vval.v_float = 0.0;
8884}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885#endif
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * "browse(save, title, initdir, default)" function
8889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008892 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895#ifdef FEAT_BROWSE
8896 int save;
8897 char_u *title;
8898 char_u *initdir;
8899 char_u *defname;
8900 char_u buf[NUMBUFLEN];
8901 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 save = get_tv_number_chk(&argvars[0], &error);
8905 title = get_tv_string_chk(&argvars[1]);
8906 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8907 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 if (error || title == NULL || initdir == NULL || defname == NULL)
8910 rettv->vval.v_string = NULL;
8911 else
8912 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913 do_browse(save ? BROWSE_SAVE : 0,
8914 title, defname, NULL, initdir, NULL, curbuf);
8915#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919}
8920
8921/*
8922 * "browsedir(title, initdir)" function
8923 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008928{
8929#ifdef FEAT_BROWSE
8930 char_u *title;
8931 char_u *initdir;
8932 char_u buf[NUMBUFLEN];
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 title = get_tv_string_chk(&argvars[0]);
8935 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 if (title == NULL || initdir == NULL)
8938 rettv->vval.v_string = NULL;
8939 else
8940 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008941 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946}
8947
Bram Moolenaar33570922005-01-25 22:26:29 +00008948static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950/*
8951 * Find a buffer by number or exact name.
8952 */
8953 static buf_T *
8954find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008959 if (avar->v_type == VAR_NUMBER)
8960 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008961 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008964 if (buf == NULL)
8965 {
8966 /* No full path name match, try a match with a URL or a "nofile"
8967 * buffer, these don't use the full path. */
8968 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8969 if (buf->b_fname != NULL
8970 && (path_with_url(buf->b_fname)
8971#ifdef FEAT_QUICKFIX
8972 || bt_nofile(buf)
8973#endif
8974 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008975 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008976 break;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 }
8979 return buf;
8980}
8981
8982/*
8983 * "bufexists(expr)" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991}
8992
8993/*
8994 * "buflisted(expr)" function
8995 */
8996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 buf_T *buf;
9002
9003 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005}
9006
9007/*
9008 * "bufloaded(expr)" function
9009 */
9010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_bufloaded(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_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019}
9020
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009021static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023/*
9024 * Get buffer by number or pattern.
9025 */
9026 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009027get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009028 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009029 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 int save_magic;
9033 char_u *save_cpo;
9034 buf_T *buf;
9035
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 if (tv->v_type == VAR_NUMBER)
9037 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009038 if (tv->v_type != VAR_STRING)
9039 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 if (name == NULL || *name == NUL)
9041 return curbuf;
9042 if (name[0] == '$' && name[1] == NUL)
9043 return lastbuf;
9044
9045 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9046 save_magic = p_magic;
9047 p_magic = TRUE;
9048 save_cpo = p_cpo;
9049 p_cpo = (char_u *)"";
9050
9051 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009052 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 p_magic = save_magic;
9055 p_cpo = save_cpo;
9056
9057 /* If not found, try expanding the name, like done for bufexists(). */
9058 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 return buf;
9062}
9063
9064/*
9065 * "bufname(expr)" function
9066 */
9067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *argvars;
9070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072 buf_T *buf;
9073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009076 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 --emsg_off;
9083}
9084
9085/*
9086 * "bufnr(expr)" function
9087 */
9088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 typval_T *argvars;
9091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009094 int error = FALSE;
9095 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009097 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009099 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009100 --emsg_off;
9101
9102 /* If the buffer isn't found and the second argument is not zero create a
9103 * new buffer. */
9104 if (buf == NULL
9105 && argvars[1].v_type != VAR_UNKNOWN
9106 && get_tv_number_chk(&argvars[1], &error) != 0
9107 && !error
9108 && (name = get_tv_string_chk(&argvars[0])) != NULL
9109 && !error)
9110 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "bufwinnr(nr)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 typval_T *argvars;
9124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126#ifdef FEAT_WINDOWS
9127 win_T *wp;
9128 int winnr = 0;
9129#endif
9130 buf_T *buf;
9131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009134 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#ifdef FEAT_WINDOWS
9136 for (wp = firstwin; wp; wp = wp->w_next)
9137 {
9138 ++winnr;
9139 if (wp->w_buffer == buf)
9140 break;
9141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#endif
9146 --emsg_off;
9147}
9148
9149/*
9150 * "byte2line(byte)" function
9151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#else
9160 long boff = 0;
9161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 (linenr_T)0, &boff);
9168#endif
9169}
9170
9171/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009172 * "byteidx()" function
9173 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178{
9179#ifdef FEAT_MBYTE
9180 char_u *t;
9181#endif
9182 char_u *str;
9183 long idx;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 str = get_tv_string_chk(&argvars[0]);
9186 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189 return;
9190
9191#ifdef FEAT_MBYTE
9192 t = str;
9193 for ( ; idx > 0; idx--)
9194 {
9195 if (*t == NUL) /* EOL reached */
9196 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009197 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009199 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009200#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009201 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009203#endif
9204}
9205
Bram Moolenaardb913952012-06-29 12:54:53 +02009206 int
9207func_call(name, args, selfdict, rettv)
9208 char_u *name;
9209 typval_T *args;
9210 dict_T *selfdict;
9211 typval_T *rettv;
9212{
9213 listitem_T *item;
9214 typval_T argv[MAX_FUNC_ARGS + 1];
9215 int argc = 0;
9216 int dummy;
9217 int r = 0;
9218
9219 for (item = args->vval.v_list->lv_first; item != NULL;
9220 item = item->li_next)
9221 {
9222 if (argc == MAX_FUNC_ARGS)
9223 {
9224 EMSG(_("E699: Too many arguments"));
9225 break;
9226 }
9227 /* Make a copy of each argument. This is needed to be able to set
9228 * v_lock to VAR_FIXED in the copy without changing the original list.
9229 */
9230 copy_tv(&item->li_tv, &argv[argc++]);
9231 }
9232
9233 if (item == NULL)
9234 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9236 &dummy, TRUE, selfdict);
9237
9238 /* Free the arguments. */
9239 while (argc > 0)
9240 clear_tv(&argv[--argc]);
9241
9242 return r;
9243}
9244
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246 * "call(func, arglist)" function
9247 */
9248 static void
9249f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009250 typval_T *argvars;
9251 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009252{
9253 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009255
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009256 if (argvars[1].v_type != VAR_LIST)
9257 {
9258 EMSG(_(e_listreq));
9259 return;
9260 }
9261 if (argvars[1].vval.v_list == NULL)
9262 return;
9263
9264 if (argvars[0].v_type == VAR_FUNC)
9265 func = argvars[0].vval.v_string;
9266 else
9267 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 if (*func == NUL)
9269 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009270
Bram Moolenaare9a41262005-01-15 22:18:47 +00009271 if (argvars[2].v_type != VAR_UNKNOWN)
9272 {
9273 if (argvars[2].v_type != VAR_DICT)
9274 {
9275 EMSG(_(e_dictreq));
9276 return;
9277 }
9278 selfdict = argvars[2].vval.v_dict;
9279 }
9280
Bram Moolenaardb913952012-06-29 12:54:53 +02009281 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009282}
9283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009284#ifdef FEAT_FLOAT
9285/*
9286 * "ceil({float})" function
9287 */
9288 static void
9289f_ceil(argvars, rettv)
9290 typval_T *argvars;
9291 typval_T *rettv;
9292{
9293 float_T f;
9294
9295 rettv->v_type = VAR_FLOAT;
9296 if (get_float_arg(argvars, &f) == OK)
9297 rettv->vval.v_float = ceil(f);
9298 else
9299 rettv->vval.v_float = 0.0;
9300}
9301#endif
9302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009303/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009304 * "changenr()" function
9305 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009306 static void
9307f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009308 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009309 typval_T *rettv;
9310{
9311 rettv->vval.v_number = curbuf->b_u_seq_cur;
9312}
9313
9314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 * "char2nr(string)" function
9316 */
9317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *argvars;
9320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322#ifdef FEAT_MBYTE
9323 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009324 {
9325 int utf8 = 0;
9326
9327 if (argvars[1].v_type != VAR_UNKNOWN)
9328 utf8 = get_tv_number_chk(&argvars[1], NULL);
9329
9330 if (utf8)
9331 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9332 else
9333 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
9336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338}
9339
9340/*
9341 * "cindent(lnum)" function
9342 */
9343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348#ifdef FEAT_CINDENT
9349 pos_T pos;
9350 linenr_T lnum;
9351
9352 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9355 {
9356 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 curwin->w_cursor = pos;
9359 }
9360 else
9361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009366 * "clearmatches()" function
9367 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009368 static void
9369f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009370 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009371 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009372{
9373#ifdef FEAT_SEARCH_EXTRA
9374 clear_matches(curwin);
9375#endif
9376}
9377
9378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 * "col(string)" function
9380 */
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 colnr_T col = 0;
9387 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009390 fp = var2fpos(&argvars[0], FALSE, &fnum);
9391 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
9393 if (fp->col == MAXCOL)
9394 {
9395 /* '> can be MAXCOL, get the length of the line then */
9396 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009397 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 else
9399 col = MAXCOL;
9400 }
9401 else
9402 {
9403 col = fp->col + 1;
9404#ifdef FEAT_VIRTUALEDIT
9405 /* col(".") when the cursor is on the NUL at the end of the line
9406 * because of "coladd" can be seen as an extra column. */
9407 if (virtual_active() && fp == &curwin->w_cursor)
9408 {
9409 char_u *p = ml_get_cursor();
9410
9411 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9412 curwin->w_virtcol - curwin->w_cursor.coladd))
9413 {
9414# ifdef FEAT_MBYTE
9415 int l;
9416
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009417 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 col += l;
9419# else
9420 if (*p != NUL && p[1] == NUL)
9421 ++col;
9422# endif
9423 }
9424 }
9425#endif
9426 }
9427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429}
9430
Bram Moolenaar572cb562005-08-05 21:35:02 +00009431#if defined(FEAT_INS_EXPAND)
9432/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009433 * "complete()" function
9434 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009435 static void
9436f_complete(argvars, rettv)
9437 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009439{
9440 int startcol;
9441
9442 if ((State & INSERT) == 0)
9443 {
9444 EMSG(_("E785: complete() can only be used in Insert mode"));
9445 return;
9446 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009447
9448 /* Check for undo allowed here, because if something was already inserted
9449 * the line was already saved for undo and this check isn't done. */
9450 if (!undo_allowed())
9451 return;
9452
Bram Moolenaarade00832006-03-10 21:46:58 +00009453 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9454 {
9455 EMSG(_(e_invarg));
9456 return;
9457 }
9458
9459 startcol = get_tv_number_chk(&argvars[0], NULL);
9460 if (startcol <= 0)
9461 return;
9462
9463 set_completion(startcol - 1, argvars[1].vval.v_list);
9464}
9465
9466/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009467 * "complete_add()" function
9468 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009469 static void
9470f_complete_add(argvars, rettv)
9471 typval_T *argvars;
9472 typval_T *rettv;
9473{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009474 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475}
9476
9477/*
9478 * "complete_check()" function
9479 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009480 static void
9481f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009482 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009483 typval_T *rettv;
9484{
9485 int saved = RedrawingDisabled;
9486
9487 RedrawingDisabled = 0;
9488 ins_compl_check_keys(0);
9489 rettv->vval.v_number = compl_interrupted;
9490 RedrawingDisabled = saved;
9491}
9492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494/*
9495 * "confirm(message, buttons[, default [, type]])" function
9496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009499 typval_T *argvars UNUSED;
9500 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9503 char_u *message;
9504 char_u *buttons = NULL;
9505 char_u buf[NUMBUFLEN];
9506 char_u buf2[NUMBUFLEN];
9507 int def = 1;
9508 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 char_u *typestr;
9510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 message = get_tv_string_chk(&argvars[0]);
9513 if (message == NULL)
9514 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9518 if (buttons == NULL)
9519 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9526 if (typestr == NULL)
9527 error = TRUE;
9528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 switch (TOUPPER_ASC(*typestr))
9531 {
9532 case 'E': type = VIM_ERROR; break;
9533 case 'Q': type = VIM_QUESTION; break;
9534 case 'I': type = VIM_INFO; break;
9535 case 'W': type = VIM_WARNING; break;
9536 case 'G': type = VIM_GENERIC; break;
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 }
9540 }
9541 }
9542
9543 if (buttons == NULL || *buttons == NUL)
9544 buttons = (char_u *)_("&Ok");
9545
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009546 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009548 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#endif
9550}
9551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552/*
9553 * "copy()" function
9554 */
9555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009560 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009563#ifdef FEAT_FLOAT
9564/*
9565 * "cos()" function
9566 */
9567 static void
9568f_cos(argvars, rettv)
9569 typval_T *argvars;
9570 typval_T *rettv;
9571{
9572 float_T f;
9573
9574 rettv->v_type = VAR_FLOAT;
9575 if (get_float_arg(argvars, &f) == OK)
9576 rettv->vval.v_float = cos(f);
9577 else
9578 rettv->vval.v_float = 0.0;
9579}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009580
9581/*
9582 * "cosh()" function
9583 */
9584 static void
9585f_cosh(argvars, rettv)
9586 typval_T *argvars;
9587 typval_T *rettv;
9588{
9589 float_T f;
9590
9591 rettv->v_type = VAR_FLOAT;
9592 if (get_float_arg(argvars, &f) == OK)
9593 rettv->vval.v_float = cosh(f);
9594 else
9595 rettv->vval.v_float = 0.0;
9596}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009597#endif
9598
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 * "count()" function
9601 */
9602 static void
9603f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 typval_T *argvars;
9605 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607 long n = 0;
9608 int ic = FALSE;
9609
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009611 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 listitem_T *li;
9613 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009615
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 if ((l = argvars[0].vval.v_list) != NULL)
9617 {
9618 li = l->lv_first;
9619 if (argvars[2].v_type != VAR_UNKNOWN)
9620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 int error = FALSE;
9622
9623 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[3].v_type != VAR_UNKNOWN)
9625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 idx = get_tv_number_chk(&argvars[3], &error);
9627 if (!error)
9628 {
9629 li = list_find(l, idx);
9630 if (li == NULL)
9631 EMSGN(_(e_listidx), idx);
9632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 if (error)
9635 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 }
9637
9638 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009639 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 ++n;
9641 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 else if (argvars[0].v_type == VAR_DICT)
9644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009645 int todo;
9646 dict_T *d;
9647 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 if ((d = argvars[0].vval.v_dict) != NULL)
9650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 int error = FALSE;
9652
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 if (argvars[2].v_type != VAR_UNKNOWN)
9654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 if (argvars[3].v_type != VAR_UNKNOWN)
9657 EMSG(_(e_invarg));
9658 }
9659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009660 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 {
9663 if (!HASHITEM_EMPTY(hi))
9664 {
9665 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009666 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009667 ++n;
9668 }
9669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 }
9671 }
9672 else
9673 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 rettv->vval.v_number = n;
9675}
9676
9677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9679 *
9680 * Checks the existence of a cscope connection.
9681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009684 typval_T *argvars UNUSED;
9685 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_CSCOPE
9688 int num = 0;
9689 char_u *dbpath = NULL;
9690 char_u *prepend = NULL;
9691 char_u buf[NUMBUFLEN];
9692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 if (argvars[0].v_type != VAR_UNKNOWN
9694 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 num = (int)get_tv_number(&argvars[0]);
9697 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#endif
9704}
9705
9706/*
9707 * "cursor(lnum, col)" function
9708 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009709 * Moves the cursor to the specified line and column.
9710 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009718#ifdef FEAT_VIRTUALEDIT
9719 long coladd = 0;
9720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009722 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723 if (argvars[1].v_type == VAR_UNKNOWN)
9724 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009728 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009729 line = pos.lnum;
9730 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009733#endif
9734 }
9735 else
9736 {
9737 line = get_tv_lnum(argvars);
9738 col = get_tv_number_chk(&argvars[1], NULL);
9739#ifdef FEAT_VIRTUALEDIT
9740 if (argvars[2].v_type != VAR_UNKNOWN)
9741 coladd = get_tv_number_chk(&argvars[2], NULL);
9742#endif
9743 }
9744 if (line < 0 || col < 0
9745#ifdef FEAT_VIRTUALEDIT
9746 || coladd < 0
9747#endif
9748 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (line > 0)
9751 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (col > 0)
9753 curwin->w_cursor.col = col - 1;
9754#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009755 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#endif
9757
9758 /* Make sure the cursor is in a valid position. */
9759 check_cursor();
9760#ifdef FEAT_MBYTE
9761 /* Correct cursor for multi-byte character. */
9762 if (has_mbyte)
9763 mb_adjust_cursor();
9764#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009765
9766 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009767 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 */
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009778 int noref = 0;
9779
9780 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009782 if (noref < 0 || noref > 1)
9783 EMSG(_(e_invarg));
9784 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009785 {
9786 current_copyID += COPYID_INC;
9787 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791/*
9792 * "delete()" function
9793 */
9794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *argvars;
9797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
9806 * "did_filetype()" function
9807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009810 typval_T *argvars UNUSED;
9811 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#endif
9816}
9817
9818/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819 * "diff_filler()" function
9820 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009823 typval_T *argvars UNUSED;
9824 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009825{
9826#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828#endif
9829}
9830
9831/*
9832 * "diff_hlID()" function
9833 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009836 typval_T *argvars UNUSED;
9837 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009838{
9839#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 static linenr_T prev_lnum = 0;
9842 static int changedtick = 0;
9843 static int fnum = 0;
9844 static int change_start = 0;
9845 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009846 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847 int filler_lines;
9848 int col;
9849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009850 if (lnum < 0) /* ignore type error in {lnum} arg */
9851 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009852 if (lnum != prev_lnum
9853 || changedtick != curbuf->b_changedtick
9854 || fnum != curbuf->b_fnum)
9855 {
9856 /* New line, buffer, change: need to get the values. */
9857 filler_lines = diff_check(curwin, lnum);
9858 if (filler_lines < 0)
9859 {
9860 if (filler_lines == -1)
9861 {
9862 change_start = MAXCOL;
9863 change_end = -1;
9864 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9865 hlID = HLF_ADD; /* added line */
9866 else
9867 hlID = HLF_CHD; /* changed line */
9868 }
9869 else
9870 hlID = HLF_ADD; /* added line */
9871 }
9872 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009873 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009874 prev_lnum = lnum;
9875 changedtick = curbuf->b_changedtick;
9876 fnum = curbuf->b_fnum;
9877 }
9878
9879 if (hlID == HLF_CHD || hlID == HLF_TXD)
9880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 if (col >= change_start && col <= change_end)
9883 hlID = HLF_TXD; /* changed text */
9884 else
9885 hlID = HLF_CHD; /* changed line */
9886 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009887 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888#endif
9889}
9890
9891/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009892 * "empty({expr})" function
9893 */
9894 static void
9895f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009898{
9899 int n;
9900
9901 switch (argvars[0].v_type)
9902 {
9903 case VAR_STRING:
9904 case VAR_FUNC:
9905 n = argvars[0].vval.v_string == NULL
9906 || *argvars[0].vval.v_string == NUL;
9907 break;
9908 case VAR_NUMBER:
9909 n = argvars[0].vval.v_number == 0;
9910 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009911#ifdef FEAT_FLOAT
9912 case VAR_FLOAT:
9913 n = argvars[0].vval.v_float == 0.0;
9914 break;
9915#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009916 case VAR_LIST:
9917 n = argvars[0].vval.v_list == NULL
9918 || argvars[0].vval.v_list->lv_first == NULL;
9919 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 case VAR_DICT:
9921 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009924 default:
9925 EMSG2(_(e_intern2), "f_empty()");
9926 n = 0;
9927 }
9928
9929 rettv->vval.v_number = n;
9930}
9931
9932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 * "escape({string}, {chars})" function
9934 */
9935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 char_u buf[NUMBUFLEN];
9941
Bram Moolenaar758711c2005-02-02 23:11:38 +00009942 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9943 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 * "eval()" function
9949 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950 static void
9951f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954{
9955 char_u *s;
9956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 s = get_tv_string_chk(&argvars[0]);
9958 if (s != NULL)
9959 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009961 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9962 {
9963 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009964 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009966 else if (*s != NUL)
9967 EMSG(_(e_trailing));
9968}
9969
9970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 * "eventhandler()" function
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "executable()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990}
9991
9992/*
9993 * "exists()" function
9994 */
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 char_u *p;
10001 char_u *name;
10002 int n = FALSE;
10003 int len = 0;
10004
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010005 no_autoload = TRUE;
10006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (*p == '$') /* environment variable */
10009 {
10010 /* first try "normal" environment variables (fast) */
10011 if (mch_getenv(p + 1) != NULL)
10012 n = TRUE;
10013 else
10014 {
10015 /* try expanding things like $VIM and ${HOME} */
10016 p = expand_env_save(p);
10017 if (p != NULL && *p != '$')
10018 n = TRUE;
10019 vim_free(p);
10020 }
10021 }
10022 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010025 if (*skipwhite(p) != NUL)
10026 n = FALSE; /* trailing garbage */
10027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 else if (*p == '*') /* internal or user defined function */
10029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010030 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032 else if (*p == ':')
10033 {
10034 n = cmd_exists(p + 1);
10035 }
10036 else if (*p == '#')
10037 {
10038#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010039 if (p[1] == '#')
10040 n = autocmd_supported(p + 2);
10041 else
10042 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#endif
10044 }
10045 else /* internal variable */
10046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010047 char_u *tofree;
10048 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 /* get_name_len() takes care of expanding curly braces */
10051 name = p;
10052 len = get_name_len(&p, &tofree, TRUE, FALSE);
10053 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 if (tofree != NULL)
10056 name = tofree;
10057 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10058 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010060 /* handle d.key, l[idx], f(expr) */
10061 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10062 if (n)
10063 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 }
10065 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010066 if (*p != NUL)
10067 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010069 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 }
10071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010073
10074 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010077#ifdef FEAT_FLOAT
10078/*
10079 * "exp()" function
10080 */
10081 static void
10082f_exp(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 float_T f;
10087
10088 rettv->v_type = VAR_FLOAT;
10089 if (get_float_arg(argvars, &f) == OK)
10090 rettv->vval.v_float = exp(f);
10091 else
10092 rettv->vval.v_float = 0.0;
10093}
10094#endif
10095
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096/*
10097 * "expand()" function
10098 */
10099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 char_u *s;
10105 int len;
10106 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010107 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010113 if (argvars[1].v_type != VAR_UNKNOWN
10114 && argvars[2].v_type != VAR_UNKNOWN
10115 && get_tv_number_chk(&argvars[2], &error)
10116 && !error)
10117 {
10118 rettv->v_type = VAR_LIST;
10119 rettv->vval.v_list = NULL;
10120 }
10121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 if (*s == '%' || *s == '#' || *s == '<')
10124 {
10125 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010126 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010128 if (rettv->v_type == VAR_LIST)
10129 {
10130 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10131 list_append_string(rettv->vval.v_list, result, -1);
10132 else
10133 vim_free(result);
10134 }
10135 else
10136 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 else
10139 {
10140 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010141 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 if (argvars[1].v_type != VAR_UNKNOWN
10143 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010144 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 if (!error)
10146 {
10147 ExpandInit(&xpc);
10148 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010149 if (p_wic)
10150 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010151 if (rettv->v_type == VAR_STRING)
10152 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10153 options, WILD_ALL);
10154 else if (rettv_list_alloc(rettv) != FAIL)
10155 {
10156 int i;
10157
10158 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10159 for (i = 0; i < xpc.xp_numfiles; i++)
10160 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10161 ExpandCleanup(&xpc);
10162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 }
10164 else
10165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 }
10167}
10168
10169/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010170 * Go over all entries in "d2" and add them to "d1".
10171 * When "action" is "error" then a duplicate key is an error.
10172 * When "action" is "force" then a duplicate key is overwritten.
10173 * Otherwise duplicate keys are ignored ("action" is "keep").
10174 */
10175 void
10176dict_extend(d1, d2, action)
10177 dict_T *d1;
10178 dict_T *d2;
10179 char_u *action;
10180{
10181 dictitem_T *di1;
10182 hashitem_T *hi2;
10183 int todo;
10184
10185 todo = (int)d2->dv_hashtab.ht_used;
10186 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10187 {
10188 if (!HASHITEM_EMPTY(hi2))
10189 {
10190 --todo;
10191 di1 = dict_find(d1, hi2->hi_key, -1);
10192 if (d1->dv_scope != 0)
10193 {
10194 /* Disallow replacing a builtin function in l: and g:.
10195 * Check the key to be valid when adding to any
10196 * scope. */
10197 if (d1->dv_scope == VAR_DEF_SCOPE
10198 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10199 && var_check_func_name(hi2->hi_key,
10200 di1 == NULL))
10201 break;
10202 if (!valid_varname(hi2->hi_key))
10203 break;
10204 }
10205 if (di1 == NULL)
10206 {
10207 di1 = dictitem_copy(HI2DI(hi2));
10208 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10209 dictitem_free(di1);
10210 }
10211 else if (*action == 'e')
10212 {
10213 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10214 break;
10215 }
10216 else if (*action == 'f' && HI2DI(hi2) != di1)
10217 {
10218 clear_tv(&di1->di_tv);
10219 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10220 }
10221 }
10222 }
10223}
10224
10225/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010228 */
10229 static void
10230f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010231 typval_T *argvars;
10232 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010233{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010234 char *arg_errmsg = N_("extend() argument");
10235
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 list_T *l1, *l2;
10239 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010242
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 l1 = argvars[0].vval.v_list;
10244 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010245 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010246 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 {
10248 if (argvars[2].v_type != VAR_UNKNOWN)
10249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 before = get_tv_number_chk(&argvars[2], &error);
10251 if (error)
10252 return; /* type error; errmsg already given */
10253
Bram Moolenaar758711c2005-02-02 23:11:38 +000010254 if (before == l1->lv_len)
10255 item = NULL;
10256 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010258 item = list_find(l1, before);
10259 if (item == NULL)
10260 {
10261 EMSGN(_(e_listidx), before);
10262 return;
10263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 }
10265 }
10266 else
10267 item = NULL;
10268 list_extend(l1, l2, item);
10269
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 copy_tv(&argvars[0], rettv);
10271 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10274 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010275 dict_T *d1, *d2;
10276 char_u *action;
10277 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278
10279 d1 = argvars[0].vval.v_dict;
10280 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010281 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010282 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 {
10284 /* Check the third argument. */
10285 if (argvars[2].v_type != VAR_UNKNOWN)
10286 {
10287 static char *(av[]) = {"keep", "force", "error"};
10288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 action = get_tv_string_chk(&argvars[2]);
10290 if (action == NULL)
10291 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 for (i = 0; i < 3; ++i)
10293 if (STRCMP(action, av[i]) == 0)
10294 break;
10295 if (i == 3)
10296 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010297 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 return;
10299 }
10300 }
10301 else
10302 action = (char_u *)"force";
10303
Bram Moolenaara9922d62013-05-30 13:01:18 +020010304 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 copy_tv(&argvars[0], rettv);
10307 }
10308 }
10309 else
10310 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311}
10312
10313/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010314 * "feedkeys()" function
10315 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010316 static void
10317f_feedkeys(argvars, rettv)
10318 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010319 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010320{
10321 int remap = TRUE;
10322 char_u *keys, *flags;
10323 char_u nbuf[NUMBUFLEN];
10324 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010325 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010326
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010327 /* This is not allowed in the sandbox. If the commands would still be
10328 * executed in the sandbox it would be OK, but it probably happens later,
10329 * when "sandbox" is no longer set. */
10330 if (check_secure())
10331 return;
10332
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010333 keys = get_tv_string(&argvars[0]);
10334 if (*keys != NUL)
10335 {
10336 if (argvars[1].v_type != VAR_UNKNOWN)
10337 {
10338 flags = get_tv_string_buf(&argvars[1], nbuf);
10339 for ( ; *flags != NUL; ++flags)
10340 {
10341 switch (*flags)
10342 {
10343 case 'n': remap = FALSE; break;
10344 case 'm': remap = TRUE; break;
10345 case 't': typed = TRUE; break;
10346 }
10347 }
10348 }
10349
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010350 /* Need to escape K_SPECIAL and CSI before putting the string in the
10351 * typeahead buffer. */
10352 keys_esc = vim_strsave_escape_csi(keys);
10353 if (keys_esc != NULL)
10354 {
10355 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010356 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010357 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010358 if (vgetc_busy)
10359 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010360 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010361 }
10362}
10363
10364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 * "filereadable()" function
10366 */
10367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010369 typval_T *argvars;
10370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010372 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 char_u *p;
10374 int n;
10375
Bram Moolenaarc236c162008-07-13 17:41:49 +000010376#ifndef O_NONBLOCK
10377# define O_NONBLOCK 0
10378#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010380 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10381 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
10383 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010384 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 else
10387 n = FALSE;
10388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390}
10391
10392/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010393 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 * rights to write into.
10395 */
10396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *argvars;
10399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010401 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402}
10403
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010404static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010405
10406 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010407findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010410 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010411{
10412#ifdef FEAT_SEARCHPATH
10413 char_u *fname;
10414 char_u *fresult = NULL;
10415 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10416 char_u *p;
10417 char_u pathbuf[NUMBUFLEN];
10418 int count = 1;
10419 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 int error = FALSE;
10421#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010422
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010423 rettv->vval.v_string = NULL;
10424 rettv->v_type = VAR_STRING;
10425
10426#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10432 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010433 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 else
10435 {
10436 if (*p != NUL)
10437 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010440 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010442 }
10443
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010444 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10445 error = TRUE;
10446
10447 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 do
10450 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010451 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010452 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 fresult = find_file_in_path_option(first ? fname : NULL,
10454 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010455 0, first, path,
10456 find_what,
10457 curbuf->b_ffname,
10458 find_what == FINDFILE_DIR
10459 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010461
10462 if (fresult != NULL && rettv->v_type == VAR_LIST)
10463 list_append_string(rettv->vval.v_list, fresult, -1);
10464
10465 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010467
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010468 if (rettv->v_type == VAR_STRING)
10469 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010470#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010471}
10472
Bram Moolenaar33570922005-01-25 22:26:29 +000010473static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10474static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010475
10476/*
10477 * Implementation of map() and filter().
10478 */
10479 static void
10480filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010481 typval_T *argvars;
10482 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483 int map;
10484{
10485 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 listitem_T *li, *nli;
10488 list_T *l = NULL;
10489 dictitem_T *di;
10490 hashtab_T *ht;
10491 hashitem_T *hi;
10492 dict_T *d = NULL;
10493 typval_T save_val;
10494 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010496 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010497 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10498 char *arg_errmsg = (map ? N_("map() argument")
10499 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010500 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010501 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010504 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010505 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010506 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 return;
10508 }
10509 else if (argvars[0].v_type == VAR_DICT)
10510 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010511 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010512 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 return;
10514 }
10515 else
10516 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010517 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 return;
10519 }
10520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 expr = get_tv_string_buf_chk(&argvars[1], buf);
10522 /* On type errors, the preceding call has already displayed an error
10523 * message. Avoid a misleading error message for an empty string that
10524 * was not passed as argument. */
10525 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 prepare_vimvar(VV_VAL, &save_val);
10528 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010529
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010530 /* We reset "did_emsg" to be able to detect whether an error
10531 * occurred during evaluation of the expression. */
10532 save_did_emsg = did_emsg;
10533 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010534
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010535 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 vimvars[VV_KEY].vv_type = VAR_STRING;
10539
10540 ht = &d->dv_hashtab;
10541 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010542 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 if (!HASHITEM_EMPTY(hi))
10546 {
10547 --todo;
10548 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010549 if (tv_check_lock(di->di_tv.v_lock,
10550 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 break;
10552 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010553 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010554 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010555 break;
10556 if (!map && rem)
10557 dictitem_remove(d, di);
10558 clear_tv(&vimvars[VV_KEY].vv_tv);
10559 }
10560 }
10561 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 }
10563 else
10564 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010565 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 for (li = l->lv_first; li != NULL; li = nli)
10568 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010569 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010570 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010572 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010573 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010574 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010575 break;
10576 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010578 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010581
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010582 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010584
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010585 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010586 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587
10588 copy_tv(&argvars[0], rettv);
10589}
10590
10591 static int
10592filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 char_u *expr;
10595 int map;
10596 int *remp;
10597{
Bram Moolenaar33570922005-01-25 22:26:29 +000010598 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010600 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 s = expr;
10604 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010605 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 if (*s != NUL) /* check for trailing chars after expr */
10607 {
10608 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 }
10611 if (map)
10612 {
10613 /* map(): replace the list item value */
10614 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010615 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 *tv = rettv;
10617 }
10618 else
10619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 int error = FALSE;
10621
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 /* On type error, nothing has been removed; return FAIL to stop the
10626 * loop. The error message was given by get_tv_number_chk(). */
10627 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010628 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010629 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010630 retval = OK;
10631theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010633 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010634}
10635
10636/*
10637 * "filter()" function
10638 */
10639 static void
10640f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643{
10644 filter_map(argvars, rettv, FALSE);
10645}
10646
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010648 * "finddir({fname}[, {path}[, {count}]])" function
10649 */
10650 static void
10651f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 typval_T *argvars;
10653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010655 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656}
10657
10658/*
10659 * "findfile({fname}[, {path}[, {count}]])" function
10660 */
10661 static void
10662f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010663 typval_T *argvars;
10664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010666 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010667}
10668
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010669#ifdef FEAT_FLOAT
10670/*
10671 * "float2nr({float})" function
10672 */
10673 static void
10674f_float2nr(argvars, rettv)
10675 typval_T *argvars;
10676 typval_T *rettv;
10677{
10678 float_T f;
10679
10680 if (get_float_arg(argvars, &f) == OK)
10681 {
10682 if (f < -0x7fffffff)
10683 rettv->vval.v_number = -0x7fffffff;
10684 else if (f > 0x7fffffff)
10685 rettv->vval.v_number = 0x7fffffff;
10686 else
10687 rettv->vval.v_number = (varnumber_T)f;
10688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010689}
10690
10691/*
10692 * "floor({float})" function
10693 */
10694 static void
10695f_floor(argvars, rettv)
10696 typval_T *argvars;
10697 typval_T *rettv;
10698{
10699 float_T f;
10700
10701 rettv->v_type = VAR_FLOAT;
10702 if (get_float_arg(argvars, &f) == OK)
10703 rettv->vval.v_float = floor(f);
10704 else
10705 rettv->vval.v_float = 0.0;
10706}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010707
10708/*
10709 * "fmod()" function
10710 */
10711 static void
10712f_fmod(argvars, rettv)
10713 typval_T *argvars;
10714 typval_T *rettv;
10715{
10716 float_T fx, fy;
10717
10718 rettv->v_type = VAR_FLOAT;
10719 if (get_float_arg(argvars, &fx) == OK
10720 && get_float_arg(&argvars[1], &fy) == OK)
10721 rettv->vval.v_float = fmod(fx, fy);
10722 else
10723 rettv->vval.v_float = 0.0;
10724}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010725#endif
10726
Bram Moolenaar0d660222005-01-07 21:51:51 +000010727/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010728 * "fnameescape({string})" function
10729 */
10730 static void
10731f_fnameescape(argvars, rettv)
10732 typval_T *argvars;
10733 typval_T *rettv;
10734{
10735 rettv->vval.v_string = vim_strsave_fnameescape(
10736 get_tv_string(&argvars[0]), FALSE);
10737 rettv->v_type = VAR_STRING;
10738}
10739
10740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 * "fnamemodify({fname}, {mods})" function
10742 */
10743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010745 typval_T *argvars;
10746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747{
10748 char_u *fname;
10749 char_u *mods;
10750 int usedlen = 0;
10751 int len;
10752 char_u *fbuf = NULL;
10753 char_u buf[NUMBUFLEN];
10754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 fname = get_tv_string_chk(&argvars[0]);
10756 mods = get_tv_string_buf_chk(&argvars[1], buf);
10757 if (fname == NULL || mods == NULL)
10758 fname = NULL;
10759 else
10760 {
10761 len = (int)STRLEN(fname);
10762 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 vim_free(fbuf);
10771}
10772
Bram Moolenaar33570922005-01-25 22:26:29 +000010773static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774
10775/*
10776 * "foldclosed()" function
10777 */
10778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010782 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783{
10784#ifdef FEAT_FOLDING
10785 linenr_T lnum;
10786 linenr_T first, last;
10787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10790 {
10791 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10792 {
10793 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 return;
10798 }
10799 }
10800#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802}
10803
10804/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805 * "foldclosed()" function
10806 */
10807 static void
10808f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
10812 foldclosed_both(argvars, rettv, FALSE);
10813}
10814
10815/*
10816 * "foldclosedend()" function
10817 */
10818 static void
10819f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822{
10823 foldclosed_both(argvars, rettv, TRUE);
10824}
10825
10826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 * "foldlevel()" function
10828 */
10829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010831 typval_T *argvars UNUSED;
10832 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834#ifdef FEAT_FOLDING
10835 linenr_T lnum;
10836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
10844 * "foldtext()" function
10845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010848 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850{
10851#ifdef FEAT_FOLDING
10852 linenr_T lnum;
10853 char_u *s;
10854 char_u *r;
10855 int len;
10856 char *txt;
10857#endif
10858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->v_type = VAR_STRING;
10860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010862 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10863 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10864 <= curbuf->b_ml.ml_line_count
10865 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
10867 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10869 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 {
10871 if (!linewhite(lnum))
10872 break;
10873 ++lnum;
10874 }
10875
10876 /* Find interesting text in this line. */
10877 s = skipwhite(ml_get(lnum));
10878 /* skip C comment-start */
10879 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010882 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010884 {
10885 s = skipwhite(ml_get(lnum + 1));
10886 if (*s == '*')
10887 s = skipwhite(s + 1);
10888 }
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 txt = _("+-%s%3ld lines: ");
10891 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 + 20 /* for %3ld */
10894 + STRLEN(s))); /* concatenated */
10895 if (r != NULL)
10896 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10898 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10899 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 len = (int)STRLEN(r);
10901 STRCAT(r, s);
10902 /* remove 'foldmarker' and 'commentstring' */
10903 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 }
10907#endif
10908}
10909
10910/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010911 * "foldtextresult(lnum)" function
10912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010916 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010917{
10918#ifdef FEAT_FOLDING
10919 linenr_T lnum;
10920 char_u *text;
10921 char_u buf[51];
10922 foldinfo_T foldinfo;
10923 int fold_count;
10924#endif
10925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->v_type = VAR_STRING;
10927 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010928#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 /* treat illegal types and illegal string values for {lnum} the same */
10931 if (lnum < 0)
10932 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010933 fold_count = foldedCount(curwin, lnum, &foldinfo);
10934 if (fold_count > 0)
10935 {
10936 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10937 &foldinfo, buf);
10938 if (text == buf)
10939 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010941 }
10942#endif
10943}
10944
10945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 * "foreground()" function
10947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010950 typval_T *argvars UNUSED;
10951 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#ifdef FEAT_GUI
10954 if (gui.in_use)
10955 gui_mch_set_foreground();
10956#else
10957# ifdef WIN32
10958 win32_set_foreground();
10959# endif
10960#endif
10961}
10962
10963/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 * "function()" function
10965 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970{
10971 char_u *s;
10972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010976 /* Don't check an autoload name for existence here. */
10977 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010978 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010979 else
10980 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010981 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010982 {
10983 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010984 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010985
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010986 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10987 * also be called from another script. Using trans_function_name()
10988 * would also work, but some plugins depend on the name being
10989 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010990 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020010991 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010992 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010993 if (rettv->vval.v_string != NULL)
10994 {
10995 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010996 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010997 }
10998 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020010999 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011000 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002 }
11003}
11004
11005/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011006 * "garbagecollect()" function
11007 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011008 static void
11009f_garbagecollect(argvars, rettv)
11010 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011011 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011012{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011013 /* This is postponed until we are back at the toplevel, because we may be
11014 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11015 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011016
11017 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11018 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011019}
11020
11021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022 * "get()" function
11023 */
11024 static void
11025f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011028{
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 listitem_T *li;
11030 list_T *l;
11031 dictitem_T *di;
11032 dict_T *d;
11033 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011034
Bram Moolenaare9a41262005-01-15 22:18:47 +000011035 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 int error = FALSE;
11040
11041 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11042 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 else if (argvars[0].v_type == VAR_DICT)
11047 {
11048 if ((d = argvars[0].vval.v_dict) != NULL)
11049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011050 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 if (di != NULL)
11052 tv = &di->di_tv;
11053 }
11054 }
11055 else
11056 EMSG2(_(e_listdictarg), "get()");
11057
11058 if (tv == NULL)
11059 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 copy_tv(&argvars[2], rettv);
11062 }
11063 else
11064 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065}
11066
Bram Moolenaar342337a2005-07-21 21:11:17 +000011067static 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 +000011068
11069/*
11070 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011071 * Return a range (from start to end) of lines in rettv from the specified
11072 * buffer.
11073 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011074 */
11075 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011076get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011077 buf_T *buf;
11078 linenr_T start;
11079 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011080 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011081 typval_T *rettv;
11082{
11083 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011084
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011085 if (retlist && rettv_list_alloc(rettv) == FAIL)
11086 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011087
11088 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11089 return;
11090
11091 if (!retlist)
11092 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11094 p = ml_get_buf(buf, start, FALSE);
11095 else
11096 p = (char_u *)"";
11097
11098 rettv->v_type = VAR_STRING;
11099 rettv->vval.v_string = vim_strsave(p);
11100 }
11101 else
11102 {
11103 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011104 return;
11105
11106 if (start < 1)
11107 start = 1;
11108 if (end > buf->b_ml.ml_line_count)
11109 end = buf->b_ml.ml_line_count;
11110 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011111 if (list_append_string(rettv->vval.v_list,
11112 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011113 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011114 }
11115}
11116
11117/*
11118 * "getbufline()" function
11119 */
11120 static void
11121f_getbufline(argvars, rettv)
11122 typval_T *argvars;
11123 typval_T *rettv;
11124{
11125 linenr_T lnum;
11126 linenr_T end;
11127 buf_T *buf;
11128
11129 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11130 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011131 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011132 --emsg_off;
11133
Bram Moolenaar661b1822005-07-28 22:36:45 +000011134 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011135 if (argvars[2].v_type == VAR_UNKNOWN)
11136 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011137 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011138 end = get_tv_lnum_buf(&argvars[2], buf);
11139
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011141}
11142
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143/*
11144 * "getbufvar()" function
11145 */
11146 static void
11147f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150{
11151 buf_T *buf;
11152 buf_T *save_curbuf;
11153 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011155 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11158 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011160 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011162 rettv->v_type = VAR_STRING;
11163 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164
11165 if (buf != NULL && varname != NULL)
11166 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011167 /* set curbuf to be our buf, temporarily */
11168 save_curbuf = curbuf;
11169 curbuf = buf;
11170
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011172 {
11173 if (get_option_tv(&varname, rettv, TRUE) == OK)
11174 done = TRUE;
11175 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011176 else if (STRCMP(varname, "changedtick") == 0)
11177 {
11178 rettv->v_type = VAR_NUMBER;
11179 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011180 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011181 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 else
11183 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011184 /* Look up the variable. */
11185 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11186 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11187 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011188 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011189 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011191 done = TRUE;
11192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011194
11195 /* restore previous notion of curbuf */
11196 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197 }
11198
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011199 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11200 /* use the default value */
11201 copy_tv(&argvars[2], rettv);
11202
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203 --emsg_off;
11204}
11205
11206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 * "getchar()" function
11208 */
11209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011217 /* Position the cursor. Needed after a message that ends in a space. */
11218 windgoto(msg_row, msg_col);
11219
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 ++no_mapping;
11221 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011222 for (;;)
11223 {
11224 if (argvars[0].v_type == VAR_UNKNOWN)
11225 /* getchar(): blocking wait. */
11226 n = safe_vgetc();
11227 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11228 /* getchar(1): only check if char avail */
11229 n = vpeekc();
11230 else if (error || vpeekc() == NUL)
11231 /* illegal argument or getchar(0) and no char avail: return zero */
11232 n = 0;
11233 else
11234 /* getchar(0) and char avail: return char */
11235 n = safe_vgetc();
11236 if (n == K_IGNORE)
11237 continue;
11238 break;
11239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 --no_mapping;
11241 --allow_keys;
11242
Bram Moolenaar219b8702006-11-01 14:32:36 +000011243 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11244 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11245 vimvars[VV_MOUSE_COL].vv_nr = 0;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 if (IS_SPECIAL(n) || mod_mask != 0)
11249 {
11250 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11251 int i = 0;
11252
11253 /* Turn a special key into three bytes, plus modifier. */
11254 if (mod_mask != 0)
11255 {
11256 temp[i++] = K_SPECIAL;
11257 temp[i++] = KS_MODIFIER;
11258 temp[i++] = mod_mask;
11259 }
11260 if (IS_SPECIAL(n))
11261 {
11262 temp[i++] = K_SPECIAL;
11263 temp[i++] = K_SECOND(n);
11264 temp[i++] = K_THIRD(n);
11265 }
11266#ifdef FEAT_MBYTE
11267 else if (has_mbyte)
11268 i += (*mb_char2bytes)(n, temp + i);
11269#endif
11270 else
11271 temp[i++] = n;
11272 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
11274 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011275
11276#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011277 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011278 {
11279 int row = mouse_row;
11280 int col = mouse_col;
11281 win_T *win;
11282 linenr_T lnum;
11283# ifdef FEAT_WINDOWS
11284 win_T *wp;
11285# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011286 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011287
11288 if (row >= 0 && col >= 0)
11289 {
11290 /* Find the window at the mouse coordinates and compute the
11291 * text position. */
11292 win = mouse_find_win(&row, &col);
11293 (void)mouse_comp_pos(win, &row, &col, &lnum);
11294# ifdef FEAT_WINDOWS
11295 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011296 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011297# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011298 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011299 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11300 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11301 }
11302 }
11303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305}
11306
11307/*
11308 * "getcharmod()" function
11309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316}
11317
11318/*
11319 * "getcmdline()" function
11320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
11327 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328}
11329
11330/*
11331 * "getcmdpos()" function
11332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339}
11340
11341/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011342 * "getcmdtype()" function
11343 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011344 static void
11345f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011346 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011347 typval_T *rettv;
11348{
11349 rettv->v_type = VAR_STRING;
11350 rettv->vval.v_string = alloc(2);
11351 if (rettv->vval.v_string != NULL)
11352 {
11353 rettv->vval.v_string[0] = get_cmdline_type();
11354 rettv->vval.v_string[1] = NUL;
11355 }
11356}
11357
11358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 * "getcwd()" function
11360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011366 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011369 rettv->vval.v_string = NULL;
11370 cwd = alloc(MAXPATHL);
11371 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011373 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11374 {
11375 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011377 if (rettv->vval.v_string != NULL)
11378 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011380 }
11381 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383}
11384
11385/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011386 * "getfontname()" function
11387 */
11388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011392{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->v_type = VAR_STRING;
11394 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011395#ifdef FEAT_GUI
11396 if (gui.in_use)
11397 {
11398 GuiFont font;
11399 char_u *name = NULL;
11400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011402 {
11403 /* Get the "Normal" font. Either the name saved by
11404 * hl_set_font_name() or from the font ID. */
11405 font = gui.norm_font;
11406 name = hl_get_font_name();
11407 }
11408 else
11409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011411 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11412 return;
11413 font = gui_mch_get_font(name, FALSE);
11414 if (font == NOFONT)
11415 return; /* Invalid font name, return empty string. */
11416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011418 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011419 gui_mch_free_font(font);
11420 }
11421#endif
11422}
11423
11424/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011425 * "getfperm({fname})" function
11426 */
11427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *argvars;
11430 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011431{
11432 char_u *fname;
11433 struct stat st;
11434 char_u *perm = NULL;
11435 char_u flags[] = "rwx";
11436 int i;
11437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011441 if (mch_stat((char *)fname, &st) >= 0)
11442 {
11443 perm = vim_strsave((char_u *)"---------");
11444 if (perm != NULL)
11445 {
11446 for (i = 0; i < 9; i++)
11447 {
11448 if (st.st_mode & (1 << (8 - i)))
11449 perm[i] = flags[i % 3];
11450 }
11451 }
11452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011454}
11455
11456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 * "getfsize({fname})" function
11458 */
11459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
11464 char_u *fname;
11465 struct stat st;
11466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
11471 if (mch_stat((char *)fname, &st) >= 0)
11472 {
11473 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011478
11479 /* non-perfect check for overflow */
11480 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11481 rettv->vval.v_number = -2;
11482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 }
11484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
11489 * "getftime({fname})" function
11490 */
11491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *argvars;
11494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496 char_u *fname;
11497 struct stat st;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
11501 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505}
11506
11507/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011508 * "getftype({fname})" function
11509 */
11510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *argvars;
11513 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011514{
11515 char_u *fname;
11516 struct stat st;
11517 char_u *type = NULL;
11518 char *t;
11519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011523 if (mch_lstat((char *)fname, &st) >= 0)
11524 {
11525#ifdef S_ISREG
11526 if (S_ISREG(st.st_mode))
11527 t = "file";
11528 else if (S_ISDIR(st.st_mode))
11529 t = "dir";
11530# ifdef S_ISLNK
11531 else if (S_ISLNK(st.st_mode))
11532 t = "link";
11533# endif
11534# ifdef S_ISBLK
11535 else if (S_ISBLK(st.st_mode))
11536 t = "bdev";
11537# endif
11538# ifdef S_ISCHR
11539 else if (S_ISCHR(st.st_mode))
11540 t = "cdev";
11541# endif
11542# ifdef S_ISFIFO
11543 else if (S_ISFIFO(st.st_mode))
11544 t = "fifo";
11545# endif
11546# ifdef S_ISSOCK
11547 else if (S_ISSOCK(st.st_mode))
11548 t = "fifo";
11549# endif
11550 else
11551 t = "other";
11552#else
11553# ifdef S_IFMT
11554 switch (st.st_mode & S_IFMT)
11555 {
11556 case S_IFREG: t = "file"; break;
11557 case S_IFDIR: t = "dir"; break;
11558# ifdef S_IFLNK
11559 case S_IFLNK: t = "link"; break;
11560# endif
11561# ifdef S_IFBLK
11562 case S_IFBLK: t = "bdev"; break;
11563# endif
11564# ifdef S_IFCHR
11565 case S_IFCHR: t = "cdev"; break;
11566# endif
11567# ifdef S_IFIFO
11568 case S_IFIFO: t = "fifo"; break;
11569# endif
11570# ifdef S_IFSOCK
11571 case S_IFSOCK: t = "socket"; break;
11572# endif
11573 default: t = "other";
11574 }
11575# else
11576 if (mch_isdir(fname))
11577 t = "dir";
11578 else
11579 t = "file";
11580# endif
11581#endif
11582 type = vim_strsave((char_u *)t);
11583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011585}
11586
11587/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011588 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 */
11590 static void
11591f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 typval_T *argvars;
11593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594{
11595 linenr_T lnum;
11596 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011597 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598
11599 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011600 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011601 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011602 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011603 retlist = FALSE;
11604 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011605 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011608 retlist = TRUE;
11609 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011610
Bram Moolenaar342337a2005-07-21 21:11:17 +000011611 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011612}
11613
11614/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011615 * "getmatches()" function
11616 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011617 static void
11618f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011619 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011620 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011621{
11622#ifdef FEAT_SEARCH_EXTRA
11623 dict_T *dict;
11624 matchitem_T *cur = curwin->w_match_head;
11625
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011626 if (rettv_list_alloc(rettv) == OK)
11627 {
11628 while (cur != NULL)
11629 {
11630 dict = dict_alloc();
11631 if (dict == NULL)
11632 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011633 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11634 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11635 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11636 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11637 list_append_dict(rettv->vval.v_list, dict);
11638 cur = cur->next;
11639 }
11640 }
11641#endif
11642}
11643
11644/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011645 * "getpid()" function
11646 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011647 static void
11648f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011649 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011650 typval_T *rettv;
11651{
11652 rettv->vval.v_number = mch_get_pid();
11653}
11654
11655/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011656 * "getpos(string)" function
11657 */
11658 static void
11659f_getpos(argvars, rettv)
11660 typval_T *argvars;
11661 typval_T *rettv;
11662{
11663 pos_T *fp;
11664 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011665 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011666
11667 if (rettv_list_alloc(rettv) == OK)
11668 {
11669 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011670 fp = var2fpos(&argvars[0], TRUE, &fnum);
11671 if (fnum != -1)
11672 list_append_number(l, (varnumber_T)fnum);
11673 else
11674 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011675 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11676 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011677 list_append_number(l, (fp != NULL)
11678 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011679 : (varnumber_T)0);
11680 list_append_number(l,
11681#ifdef FEAT_VIRTUALEDIT
11682 (fp != NULL) ? (varnumber_T)fp->coladd :
11683#endif
11684 (varnumber_T)0);
11685 }
11686 else
11687 rettv->vval.v_number = FALSE;
11688}
11689
11690/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011691 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011692 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011693 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011694f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011695 typval_T *argvars UNUSED;
11696 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011697{
11698#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011699 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011700#endif
11701
Bram Moolenaar2641f772005-03-25 21:58:17 +000011702#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011703 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011704 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011705 wp = NULL;
11706 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11707 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011708 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011709 if (wp == NULL)
11710 return;
11711 }
11712
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011713 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011714 }
11715#endif
11716}
11717
11718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 * "getreg()" function
11720 */
11721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 typval_T *argvars;
11724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
11726 char_u *strregname;
11727 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011728 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011731 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 strregname = get_tv_string_chk(&argvars[0]);
11734 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011735 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 regname = (strregname == NULL ? '"' : *strregname);
11741 if (regname == 0)
11742 regname = '"';
11743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 rettv->vval.v_string = error ? NULL :
11746 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747}
11748
11749/*
11750 * "getregtype()" function
11751 */
11752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *argvars;
11755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756{
11757 char_u *strregname;
11758 int regname;
11759 char_u buf[NUMBUFLEN + 2];
11760 long reglen = 0;
11761
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011762 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 {
11764 strregname = get_tv_string_chk(&argvars[0]);
11765 if (strregname == NULL) /* type error; errmsg already given */
11766 {
11767 rettv->v_type = VAR_STRING;
11768 rettv->vval.v_string = NULL;
11769 return;
11770 }
11771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 else
11773 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775
11776 regname = (strregname == NULL ? '"' : *strregname);
11777 if (regname == 0)
11778 regname = '"';
11779
11780 buf[0] = NUL;
11781 buf[1] = NUL;
11782 switch (get_reg_type(regname, &reglen))
11783 {
11784 case MLINE: buf[0] = 'V'; break;
11785 case MCHAR: buf[0] = 'v'; break;
11786#ifdef FEAT_VISUAL
11787 case MBLOCK:
11788 buf[0] = Ctrl_V;
11789 sprintf((char *)buf + 1, "%ld", reglen + 1);
11790 break;
11791#endif
11792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->v_type = VAR_STRING;
11794 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795}
11796
11797/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011798 * "gettabvar()" function
11799 */
11800 static void
11801f_gettabvar(argvars, rettv)
11802 typval_T *argvars;
11803 typval_T *rettv;
11804{
11805 tabpage_T *tp;
11806 dictitem_T *v;
11807 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011808 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011809
11810 rettv->v_type = VAR_STRING;
11811 rettv->vval.v_string = NULL;
11812
11813 varname = get_tv_string_chk(&argvars[1]);
11814 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11815 if (tp != NULL && varname != NULL)
11816 {
11817 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011818 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011819 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011820 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011821 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011822 done = TRUE;
11823 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011824 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825
11826 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011827 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011828}
11829
11830/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011831 * "gettabwinvar()" function
11832 */
11833 static void
11834f_gettabwinvar(argvars, rettv)
11835 typval_T *argvars;
11836 typval_T *rettv;
11837{
11838 getwinvar(argvars, rettv, 1);
11839}
11840
11841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 * "getwinposx()" function
11843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850#ifdef FEAT_GUI
11851 if (gui.in_use)
11852 {
11853 int x, y;
11854
11855 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 }
11858#endif
11859}
11860
11861/*
11862 * "getwinposy()" function
11863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011866 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_GUI
11871 if (gui.in_use)
11872 {
11873 int x, y;
11874
11875 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 }
11878#endif
11879}
11880
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011882 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011883 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011884 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011885find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011886 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011887 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011888{
11889#ifdef FEAT_WINDOWS
11890 win_T *wp;
11891#endif
11892 int nr;
11893
11894 nr = get_tv_number_chk(vp, NULL);
11895
11896#ifdef FEAT_WINDOWS
11897 if (nr < 0)
11898 return NULL;
11899 if (nr == 0)
11900 return curwin;
11901
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11903 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011904 if (--nr <= 0)
11905 break;
11906 return wp;
11907#else
11908 if (nr == 0 || nr == 1)
11909 return curwin;
11910 return NULL;
11911#endif
11912}
11913
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914/*
11915 * "getwinvar()" function
11916 */
11917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011922 getwinvar(argvars, rettv, 0);
11923}
11924
11925/*
11926 * getwinvar() and gettabwinvar()
11927 */
11928 static void
11929getwinvar(argvars, rettv, off)
11930 typval_T *argvars;
11931 typval_T *rettv;
11932 int off; /* 1 for gettabwinvar() */
11933{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 win_T *win, *oldcurwin;
11935 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011937 tabpage_T *tp = NULL;
11938 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011939 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011941#ifdef FEAT_WINDOWS
11942 if (off == 1)
11943 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11944 else
11945 tp = curtab;
11946#endif
11947 win = find_win_by_nr(&argvars[off], tp);
11948 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 rettv->v_type = VAR_STRING;
11952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953
11954 if (win != NULL && varname != NULL)
11955 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011956 /* Set curwin to be our win, temporarily. Also set the tabpage,
11957 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011958 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011959
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011961 {
11962 if (get_option_tv(&varname, rettv, 1) == OK)
11963 done = TRUE;
11964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 else
11966 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011967 /* Look up the variable. */
11968 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11969 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011973 done = TRUE;
11974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011976
11977 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011978 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 }
11980
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011981 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11982 /* use the default return value */
11983 copy_tv(&argvars[off + 2], rettv);
11984
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 --emsg_off;
11986}
11987
11988/*
11989 * "glob()" function
11990 */
11991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 typval_T *argvars;
11994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011996 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011998 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012000 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012003 if (argvars[1].v_type != VAR_UNKNOWN)
12004 {
12005 if (get_tv_number_chk(&argvars[1], &error))
12006 options |= WILD_KEEP_ALL;
12007 if (argvars[2].v_type != VAR_UNKNOWN
12008 && get_tv_number_chk(&argvars[2], &error))
12009 {
12010 rettv->v_type = VAR_LIST;
12011 rettv->vval.v_list = NULL;
12012 }
12013 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012014 if (!error)
12015 {
12016 ExpandInit(&xpc);
12017 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012018 if (p_wic)
12019 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012020 if (rettv->v_type == VAR_STRING)
12021 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012022 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012023 else if (rettv_list_alloc(rettv) != FAIL)
12024 {
12025 int i;
12026
12027 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12028 NULL, options, WILD_ALL_KEEP);
12029 for (i = 0; i < xpc.xp_numfiles; i++)
12030 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12031
12032 ExpandCleanup(&xpc);
12033 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012034 }
12035 else
12036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
12040 * "globpath()" function
12041 */
12042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012047 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012050 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012052 /* When the optional second argument is non-zero, don't remove matches
12053 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12054 if (argvars[2].v_type != VAR_UNKNOWN
12055 && get_tv_number_chk(&argvars[2], &error))
12056 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012058 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 rettv->vval.v_string = NULL;
12060 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012061 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12062 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063}
12064
12065/*
12066 * "has()" function
12067 */
12068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012070 typval_T *argvars;
12071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073 int i;
12074 char_u *name;
12075 int n = FALSE;
12076 static char *(has_list[]) =
12077 {
12078#ifdef AMIGA
12079 "amiga",
12080# ifdef FEAT_ARP
12081 "arp",
12082# endif
12083#endif
12084#ifdef __BEOS__
12085 "beos",
12086#endif
12087#ifdef MSDOS
12088# ifdef DJGPP
12089 "dos32",
12090# else
12091 "dos16",
12092# endif
12093#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012094#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 "mac",
12096#endif
12097#if defined(MACOS_X_UNIX)
12098 "macunix",
12099#endif
12100#ifdef OS2
12101 "os2",
12102#endif
12103#ifdef __QNX__
12104 "qnx",
12105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106#ifdef UNIX
12107 "unix",
12108#endif
12109#ifdef VMS
12110 "vms",
12111#endif
12112#ifdef WIN16
12113 "win16",
12114#endif
12115#ifdef WIN32
12116 "win32",
12117#endif
12118#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12119 "win32unix",
12120#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012121#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 "win64",
12123#endif
12124#ifdef EBCDIC
12125 "ebcdic",
12126#endif
12127#ifndef CASE_INSENSITIVE_FILENAME
12128 "fname_case",
12129#endif
12130#ifdef FEAT_ARABIC
12131 "arabic",
12132#endif
12133#ifdef FEAT_AUTOCMD
12134 "autocmd",
12135#endif
12136#ifdef FEAT_BEVAL
12137 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012138# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12139 "balloon_multiline",
12140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#endif
12142#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12143 "builtin_terms",
12144# ifdef ALL_BUILTIN_TCAPS
12145 "all_builtin_terms",
12146# endif
12147#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012148#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12149 || defined(FEAT_GUI_W32) \
12150 || defined(FEAT_GUI_MOTIF))
12151 "browsefilter",
12152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#ifdef FEAT_BYTEOFF
12154 "byte_offset",
12155#endif
12156#ifdef FEAT_CINDENT
12157 "cindent",
12158#endif
12159#ifdef FEAT_CLIENTSERVER
12160 "clientserver",
12161#endif
12162#ifdef FEAT_CLIPBOARD
12163 "clipboard",
12164#endif
12165#ifdef FEAT_CMDL_COMPL
12166 "cmdline_compl",
12167#endif
12168#ifdef FEAT_CMDHIST
12169 "cmdline_hist",
12170#endif
12171#ifdef FEAT_COMMENTS
12172 "comments",
12173#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012174#ifdef FEAT_CONCEAL
12175 "conceal",
12176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef FEAT_CRYPT
12178 "cryptv",
12179#endif
12180#ifdef FEAT_CSCOPE
12181 "cscope",
12182#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012183#ifdef FEAT_CURSORBIND
12184 "cursorbind",
12185#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012186#ifdef CURSOR_SHAPE
12187 "cursorshape",
12188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#ifdef DEBUG
12190 "debug",
12191#endif
12192#ifdef FEAT_CON_DIALOG
12193 "dialog_con",
12194#endif
12195#ifdef FEAT_GUI_DIALOG
12196 "dialog_gui",
12197#endif
12198#ifdef FEAT_DIFF
12199 "diff",
12200#endif
12201#ifdef FEAT_DIGRAPHS
12202 "digraphs",
12203#endif
12204#ifdef FEAT_DND
12205 "dnd",
12206#endif
12207#ifdef FEAT_EMACS_TAGS
12208 "emacs_tags",
12209#endif
12210 "eval", /* always present, of course! */
12211#ifdef FEAT_EX_EXTRA
12212 "ex_extra",
12213#endif
12214#ifdef FEAT_SEARCH_EXTRA
12215 "extra_search",
12216#endif
12217#ifdef FEAT_FKMAP
12218 "farsi",
12219#endif
12220#ifdef FEAT_SEARCHPATH
12221 "file_in_path",
12222#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012223#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012224 "filterpipe",
12225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226#ifdef FEAT_FIND_ID
12227 "find_in_path",
12228#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012229#ifdef FEAT_FLOAT
12230 "float",
12231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#ifdef FEAT_FOLDING
12233 "folding",
12234#endif
12235#ifdef FEAT_FOOTER
12236 "footer",
12237#endif
12238#if !defined(USE_SYSTEM) && defined(UNIX)
12239 "fork",
12240#endif
12241#ifdef FEAT_GETTEXT
12242 "gettext",
12243#endif
12244#ifdef FEAT_GUI
12245 "gui",
12246#endif
12247#ifdef FEAT_GUI_ATHENA
12248# ifdef FEAT_GUI_NEXTAW
12249 "gui_neXtaw",
12250# else
12251 "gui_athena",
12252# endif
12253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254#ifdef FEAT_GUI_GTK
12255 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012258#ifdef FEAT_GUI_GNOME
12259 "gui_gnome",
12260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261#ifdef FEAT_GUI_MAC
12262 "gui_mac",
12263#endif
12264#ifdef FEAT_GUI_MOTIF
12265 "gui_motif",
12266#endif
12267#ifdef FEAT_GUI_PHOTON
12268 "gui_photon",
12269#endif
12270#ifdef FEAT_GUI_W16
12271 "gui_win16",
12272#endif
12273#ifdef FEAT_GUI_W32
12274 "gui_win32",
12275#endif
12276#ifdef FEAT_HANGULIN
12277 "hangul_input",
12278#endif
12279#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12280 "iconv",
12281#endif
12282#ifdef FEAT_INS_EXPAND
12283 "insert_expand",
12284#endif
12285#ifdef FEAT_JUMPLIST
12286 "jumplist",
12287#endif
12288#ifdef FEAT_KEYMAP
12289 "keymap",
12290#endif
12291#ifdef FEAT_LANGMAP
12292 "langmap",
12293#endif
12294#ifdef FEAT_LIBCALL
12295 "libcall",
12296#endif
12297#ifdef FEAT_LINEBREAK
12298 "linebreak",
12299#endif
12300#ifdef FEAT_LISP
12301 "lispindent",
12302#endif
12303#ifdef FEAT_LISTCMDS
12304 "listcmds",
12305#endif
12306#ifdef FEAT_LOCALMAP
12307 "localmap",
12308#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012309#ifdef FEAT_LUA
12310# ifndef DYNAMIC_LUA
12311 "lua",
12312# endif
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef FEAT_MENU
12315 "menu",
12316#endif
12317#ifdef FEAT_SESSION
12318 "mksession",
12319#endif
12320#ifdef FEAT_MODIFY_FNAME
12321 "modify_fname",
12322#endif
12323#ifdef FEAT_MOUSE
12324 "mouse",
12325#endif
12326#ifdef FEAT_MOUSESHAPE
12327 "mouseshape",
12328#endif
12329#if defined(UNIX) || defined(VMS)
12330# ifdef FEAT_MOUSE_DEC
12331 "mouse_dec",
12332# endif
12333# ifdef FEAT_MOUSE_GPM
12334 "mouse_gpm",
12335# endif
12336# ifdef FEAT_MOUSE_JSB
12337 "mouse_jsbterm",
12338# endif
12339# ifdef FEAT_MOUSE_NET
12340 "mouse_netterm",
12341# endif
12342# ifdef FEAT_MOUSE_PTERM
12343 "mouse_pterm",
12344# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012345# ifdef FEAT_MOUSE_SGR
12346 "mouse_sgr",
12347# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012348# ifdef FEAT_SYSMOUSE
12349 "mouse_sysmouse",
12350# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012351# ifdef FEAT_MOUSE_URXVT
12352 "mouse_urxvt",
12353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354# ifdef FEAT_MOUSE_XTERM
12355 "mouse_xterm",
12356# endif
12357#endif
12358#ifdef FEAT_MBYTE
12359 "multi_byte",
12360#endif
12361#ifdef FEAT_MBYTE_IME
12362 "multi_byte_ime",
12363#endif
12364#ifdef FEAT_MULTI_LANG
12365 "multi_lang",
12366#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012367#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012368#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012369 "mzscheme",
12370#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372#ifdef FEAT_OLE
12373 "ole",
12374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375#ifdef FEAT_PATH_EXTRA
12376 "path_extra",
12377#endif
12378#ifdef FEAT_PERL
12379#ifndef DYNAMIC_PERL
12380 "perl",
12381#endif
12382#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012383#ifdef FEAT_PERSISTENT_UNDO
12384 "persistent_undo",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_PYTHON
12387#ifndef DYNAMIC_PYTHON
12388 "python",
12389#endif
12390#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012391#ifdef FEAT_PYTHON3
12392#ifndef DYNAMIC_PYTHON3
12393 "python3",
12394#endif
12395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396#ifdef FEAT_POSTSCRIPT
12397 "postscript",
12398#endif
12399#ifdef FEAT_PRINTER
12400 "printer",
12401#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012402#ifdef FEAT_PROFILE
12403 "profile",
12404#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012405#ifdef FEAT_RELTIME
12406 "reltime",
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_QUICKFIX
12409 "quickfix",
12410#endif
12411#ifdef FEAT_RIGHTLEFT
12412 "rightleft",
12413#endif
12414#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12415 "ruby",
12416#endif
12417#ifdef FEAT_SCROLLBIND
12418 "scrollbind",
12419#endif
12420#ifdef FEAT_CMDL_INFO
12421 "showcmd",
12422 "cmdline_info",
12423#endif
12424#ifdef FEAT_SIGNS
12425 "signs",
12426#endif
12427#ifdef FEAT_SMARTINDENT
12428 "smartindent",
12429#endif
12430#ifdef FEAT_SNIFF
12431 "sniff",
12432#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012433#ifdef STARTUPTIME
12434 "startuptime",
12435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436#ifdef FEAT_STL_OPT
12437 "statusline",
12438#endif
12439#ifdef FEAT_SUN_WORKSHOP
12440 "sun_workshop",
12441#endif
12442#ifdef FEAT_NETBEANS_INTG
12443 "netbeans_intg",
12444#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012445#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012446 "spell",
12447#endif
12448#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 "syntax",
12450#endif
12451#if defined(USE_SYSTEM) || !defined(UNIX)
12452 "system",
12453#endif
12454#ifdef FEAT_TAG_BINS
12455 "tag_binary",
12456#endif
12457#ifdef FEAT_TAG_OLDSTATIC
12458 "tag_old_static",
12459#endif
12460#ifdef FEAT_TAG_ANYWHITE
12461 "tag_any_white",
12462#endif
12463#ifdef FEAT_TCL
12464# ifndef DYNAMIC_TCL
12465 "tcl",
12466# endif
12467#endif
12468#ifdef TERMINFO
12469 "terminfo",
12470#endif
12471#ifdef FEAT_TERMRESPONSE
12472 "termresponse",
12473#endif
12474#ifdef FEAT_TEXTOBJ
12475 "textobjects",
12476#endif
12477#ifdef HAVE_TGETENT
12478 "tgetent",
12479#endif
12480#ifdef FEAT_TITLE
12481 "title",
12482#endif
12483#ifdef FEAT_TOOLBAR
12484 "toolbar",
12485#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012486#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12487 "unnamedplus",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_USR_CMDS
12490 "user-commands", /* was accidentally included in 5.4 */
12491 "user_commands",
12492#endif
12493#ifdef FEAT_VIMINFO
12494 "viminfo",
12495#endif
12496#ifdef FEAT_VERTSPLIT
12497 "vertsplit",
12498#endif
12499#ifdef FEAT_VIRTUALEDIT
12500 "virtualedit",
12501#endif
12502#ifdef FEAT_VISUAL
12503 "visual",
12504#endif
12505#ifdef FEAT_VISUALEXTRA
12506 "visualextra",
12507#endif
12508#ifdef FEAT_VREPLACE
12509 "vreplace",
12510#endif
12511#ifdef FEAT_WILDIGN
12512 "wildignore",
12513#endif
12514#ifdef FEAT_WILDMENU
12515 "wildmenu",
12516#endif
12517#ifdef FEAT_WINDOWS
12518 "windows",
12519#endif
12520#ifdef FEAT_WAK
12521 "winaltkeys",
12522#endif
12523#ifdef FEAT_WRITEBACKUP
12524 "writebackup",
12525#endif
12526#ifdef FEAT_XIM
12527 "xim",
12528#endif
12529#ifdef FEAT_XFONTSET
12530 "xfontset",
12531#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012532#ifdef FEAT_XPM_W32
12533 "xpm_w32",
12534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535#ifdef USE_XSMP
12536 "xsmp",
12537#endif
12538#ifdef USE_XSMP_INTERACT
12539 "xsmp_interact",
12540#endif
12541#ifdef FEAT_XCLIPBOARD
12542 "xterm_clipboard",
12543#endif
12544#ifdef FEAT_XTERM_SAVE
12545 "xterm_save",
12546#endif
12547#if defined(UNIX) && defined(FEAT_X11)
12548 "X11",
12549#endif
12550 NULL
12551 };
12552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 for (i = 0; has_list[i] != NULL; ++i)
12555 if (STRICMP(name, has_list[i]) == 0)
12556 {
12557 n = TRUE;
12558 break;
12559 }
12560
12561 if (n == FALSE)
12562 {
12563 if (STRNICMP(name, "patch", 5) == 0)
12564 n = has_patch(atoi((char *)name + 5));
12565 else if (STRICMP(name, "vim_starting") == 0)
12566 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012567#ifdef FEAT_MBYTE
12568 else if (STRICMP(name, "multi_byte_encoding") == 0)
12569 n = has_mbyte;
12570#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012571#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12572 else if (STRICMP(name, "balloon_multiline") == 0)
12573 n = multiline_balloon_available();
12574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575#ifdef DYNAMIC_TCL
12576 else if (STRICMP(name, "tcl") == 0)
12577 n = tcl_enabled(FALSE);
12578#endif
12579#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12580 else if (STRICMP(name, "iconv") == 0)
12581 n = iconv_enabled(FALSE);
12582#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012583#ifdef DYNAMIC_LUA
12584 else if (STRICMP(name, "lua") == 0)
12585 n = lua_enabled(FALSE);
12586#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012587#ifdef DYNAMIC_MZSCHEME
12588 else if (STRICMP(name, "mzscheme") == 0)
12589 n = mzscheme_enabled(FALSE);
12590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591#ifdef DYNAMIC_RUBY
12592 else if (STRICMP(name, "ruby") == 0)
12593 n = ruby_enabled(FALSE);
12594#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012595#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#ifdef DYNAMIC_PYTHON
12597 else if (STRICMP(name, "python") == 0)
12598 n = python_enabled(FALSE);
12599#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012600#endif
12601#ifdef FEAT_PYTHON3
12602#ifdef DYNAMIC_PYTHON3
12603 else if (STRICMP(name, "python3") == 0)
12604 n = python3_enabled(FALSE);
12605#endif
12606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607#ifdef DYNAMIC_PERL
12608 else if (STRICMP(name, "perl") == 0)
12609 n = perl_enabled(FALSE);
12610#endif
12611#ifdef FEAT_GUI
12612 else if (STRICMP(name, "gui_running") == 0)
12613 n = (gui.in_use || gui.starting);
12614# ifdef FEAT_GUI_W32
12615 else if (STRICMP(name, "gui_win32s") == 0)
12616 n = gui_is_win32s();
12617# endif
12618# ifdef FEAT_BROWSE
12619 else if (STRICMP(name, "browse") == 0)
12620 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12621# endif
12622#endif
12623#ifdef FEAT_SYN_HL
12624 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012625 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626#endif
12627#if defined(WIN3264)
12628 else if (STRICMP(name, "win95") == 0)
12629 n = mch_windows95();
12630#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012631#ifdef FEAT_NETBEANS_INTG
12632 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012633 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 }
12636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638}
12639
12640/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012641 * "has_key()" function
12642 */
12643 static void
12644f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012645 typval_T *argvars;
12646 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012647{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012648 if (argvars[0].v_type != VAR_DICT)
12649 {
12650 EMSG(_(e_dictreq));
12651 return;
12652 }
12653 if (argvars[0].vval.v_dict == NULL)
12654 return;
12655
12656 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012657 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012658}
12659
12660/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012661 * "haslocaldir()" function
12662 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012663 static void
12664f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012665 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012666 typval_T *rettv;
12667{
12668 rettv->vval.v_number = (curwin->w_localdir != NULL);
12669}
12670
12671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 * "hasmapto()" function
12673 */
12674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
12679 char_u *name;
12680 char_u *mode;
12681 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012682 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686 mode = (char_u *)"nvo";
12687 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012690 if (argvars[2].v_type != VAR_UNKNOWN)
12691 abbr = get_tv_number(&argvars[2]);
12692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693
Bram Moolenaar2c932302006-03-18 21:42:09 +000012694 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698}
12699
12700/*
12701 * "histadd()" function
12702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707{
12708#ifdef FEAT_CMDHIST
12709 int histype;
12710 char_u *str;
12711 char_u buf[NUMBUFLEN];
12712#endif
12713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 if (check_restricted() || check_secure())
12716 return;
12717#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12719 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 if (histype >= 0)
12721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 if (*str != NUL)
12724 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012725 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 return;
12729 }
12730 }
12731#endif
12732}
12733
12734/*
12735 * "histdel()" function
12736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012739 typval_T *argvars UNUSED;
12740 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741{
12742#ifdef FEAT_CMDHIST
12743 int n;
12744 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12748 if (str == NULL)
12749 n = 0;
12750 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012753 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 else
12758 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 get_tv_string_buf(&argvars[1], buf));
12761 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#endif
12763}
12764
12765/*
12766 * "histget()" function
12767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
12773#ifdef FEAT_CMDHIST
12774 int type;
12775 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12779 if (str == NULL)
12780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 {
12783 type = get_histtype(str);
12784 if (argvars[1].v_type == VAR_UNKNOWN)
12785 idx = get_history_idx(type);
12786 else
12787 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12788 /* -1 on type error */
12789 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795}
12796
12797/*
12798 * "histnr()" function
12799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
12805 int i;
12806
12807#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808 char_u *history = get_tv_string_chk(&argvars[0]);
12809
12810 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 if (i >= HIST_CMD && i < HIST_COUNT)
12812 i = get_history_idx(i);
12813 else
12814#endif
12815 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 * "highlightID(name)" function
12821 */
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 * "highlight_exists()" function
12832 */
12833 static void
12834f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 typval_T *argvars;
12836 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012837{
12838 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "hostname()" function
12843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 char_u hostname[256];
12850
12851 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856/*
12857 * iconv() function
12858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012861 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863{
12864#ifdef FEAT_MBYTE
12865 char_u buf1[NUMBUFLEN];
12866 char_u buf2[NUMBUFLEN];
12867 char_u *from, *to, *str;
12868 vimconv_T vimconv;
12869#endif
12870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->v_type = VAR_STRING;
12872 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873
12874#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 str = get_tv_string(&argvars[0]);
12876 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12877 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 vimconv.vc_type = CONV_NONE;
12879 convert_setup(&vimconv, from, to);
12880
12881 /* If the encodings are equal, no conversion needed. */
12882 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
12887 convert_setup(&vimconv, NULL, NULL);
12888 vim_free(from);
12889 vim_free(to);
12890#endif
12891}
12892
12893/*
12894 * "indent()" function
12895 */
12896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 typval_T *argvars;
12899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900{
12901 linenr_T lnum;
12902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908}
12909
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012910/*
12911 * "index()" function
12912 */
12913 static void
12914f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012917{
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 list_T *l;
12919 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012920 long idx = 0;
12921 int ic = FALSE;
12922
12923 rettv->vval.v_number = -1;
12924 if (argvars[0].v_type != VAR_LIST)
12925 {
12926 EMSG(_(e_listreq));
12927 return;
12928 }
12929 l = argvars[0].vval.v_list;
12930 if (l != NULL)
12931 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012932 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012935 int error = FALSE;
12936
Bram Moolenaar758711c2005-02-02 23:11:38 +000012937 /* Start at specified item. Use the cached index that list_find()
12938 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012940 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 ic = get_tv_number_chk(&argvars[3], &error);
12943 if (error)
12944 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012946
Bram Moolenaar758711c2005-02-02 23:11:38 +000012947 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012948 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012949 {
12950 rettv->vval.v_number = idx;
12951 break;
12952 }
12953 }
12954}
12955
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956static int inputsecret_flag = 0;
12957
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012958static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012961 * This function is used by f_input() and f_inputdialog() functions. The third
12962 * argument to f_input() specifies the type of completion to use at the
12963 * prompt. The third argument to f_inputdialog() specifies the value to return
12964 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 */
12966 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012967get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012968 typval_T *argvars;
12969 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012970 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 char_u *p = NULL;
12974 int c;
12975 char_u buf[NUMBUFLEN];
12976 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012978 int xp_type = EXPAND_NOTHING;
12979 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
12984#ifdef NO_CONSOLE_INPUT
12985 /* While starting up, there is no place to enter text. */
12986 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988#endif
12989
12990 cmd_silent = FALSE; /* Want to see the prompt. */
12991 if (prompt != NULL)
12992 {
12993 /* Only the part of the message after the last NL is considered as
12994 * prompt for the command line */
12995 p = vim_strrchr(prompt, '\n');
12996 if (p == NULL)
12997 p = prompt;
12998 else
12999 {
13000 ++p;
13001 c = *p;
13002 *p = NUL;
13003 msg_start();
13004 msg_clr_eos();
13005 msg_puts_attr(prompt, echo_attr);
13006 msg_didout = FALSE;
13007 msg_starthere();
13008 *p = c;
13009 }
13010 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 if (argvars[1].v_type != VAR_UNKNOWN)
13013 {
13014 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13015 if (defstr != NULL)
13016 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013019 {
13020 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013021 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013022 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013023
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013024 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013025 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026
Bram Moolenaar4463f292005-09-25 22:20:24 +000013027 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13028 if (xp_name == NULL)
13029 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013030
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013031 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013032
Bram Moolenaar4463f292005-09-25 22:20:24 +000013033 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13034 &xp_arg) == FAIL)
13035 return;
13036 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013037 }
13038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013039 if (defstr != NULL)
13040 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013041 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13042 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013043 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013044 && argvars[1].v_type != VAR_UNKNOWN
13045 && argvars[2].v_type != VAR_UNKNOWN)
13046 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13047 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013048
13049 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 /* since the user typed this, no need to wait for return */
13052 need_wait_return = FALSE;
13053 msg_didout = FALSE;
13054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 cmd_silent = cmd_silent_save;
13056}
13057
13058/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013059 * "input()" function
13060 * Also handles inputsecret() when inputsecret is set.
13061 */
13062 static void
13063f_input(argvars, rettv)
13064 typval_T *argvars;
13065 typval_T *rettv;
13066{
13067 get_user_input(argvars, rettv, FALSE);
13068}
13069
13070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 * "inputdialog()" function
13072 */
13073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077{
13078#if defined(FEAT_GUI_TEXTDIALOG)
13079 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13080 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13081 {
13082 char_u *message;
13083 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 message = get_tv_string_chk(&argvars[0]);
13087 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013088 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013089 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 else
13091 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 if (message != NULL && defstr != NULL
13093 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013094 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 else
13097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098 if (message != NULL && defstr != NULL
13099 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013100 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_string = vim_strsave(
13102 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 }
13108 else
13109#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013110 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111}
13112
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013113/*
13114 * "inputlist()" function
13115 */
13116 static void
13117f_inputlist(argvars, rettv)
13118 typval_T *argvars;
13119 typval_T *rettv;
13120{
13121 listitem_T *li;
13122 int selected;
13123 int mouse_used;
13124
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013125#ifdef NO_CONSOLE_INPUT
13126 /* While starting up, there is no place to enter text. */
13127 if (no_console_input())
13128 return;
13129#endif
13130 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13131 {
13132 EMSG2(_(e_listarg), "inputlist()");
13133 return;
13134 }
13135
13136 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013137 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013138 lines_left = Rows; /* avoid more prompt */
13139 msg_scroll = TRUE;
13140 msg_clr_eos();
13141
13142 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13143 {
13144 msg_puts(get_tv_string(&li->li_tv));
13145 msg_putchar('\n');
13146 }
13147
13148 /* Ask for choice. */
13149 selected = prompt_for_number(&mouse_used);
13150 if (mouse_used)
13151 selected -= lines_left;
13152
13153 rettv->vval.v_number = selected;
13154}
13155
13156
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13158
13159/*
13160 * "inputrestore()" function
13161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166{
13167 if (ga_userinput.ga_len > 0)
13168 {
13169 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13171 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013172 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 }
13174 else if (p_verbose > 1)
13175 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013176 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 }
13179}
13180
13181/*
13182 * "inputsave()" function
13183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013189 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 if (ga_grow(&ga_userinput, 1) == OK)
13191 {
13192 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13193 + ga_userinput.ga_len);
13194 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013195 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 }
13197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199}
13200
13201/*
13202 * "inputsecret()" function
13203 */
13204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 typval_T *argvars;
13207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208{
13209 ++cmdline_star;
13210 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 --cmdline_star;
13213 --inputsecret_flag;
13214}
13215
13216/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217 * "insert()" function
13218 */
13219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 typval_T *argvars;
13222 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013223{
13224 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 listitem_T *item;
13226 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228
13229 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013231 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013232 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233 {
13234 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 before = get_tv_number_chk(&argvars[2], &error);
13236 if (error)
13237 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238
Bram Moolenaar758711c2005-02-02 23:11:38 +000013239 if (before == l->lv_len)
13240 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 else
13242 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013243 item = list_find(l, before);
13244 if (item == NULL)
13245 {
13246 EMSGN(_(e_listidx), before);
13247 l = NULL;
13248 }
13249 }
13250 if (l != NULL)
13251 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013252 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013253 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013254 }
13255 }
13256}
13257
13258/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013259 * "invert(expr)" function
13260 */
13261 static void
13262f_invert(argvars, rettv)
13263 typval_T *argvars;
13264 typval_T *rettv;
13265{
13266 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13267}
13268
13269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 * "isdirectory()" function
13271 */
13272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 typval_T *argvars;
13275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013280/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013281 * "islocked()" function
13282 */
13283 static void
13284f_islocked(argvars, rettv)
13285 typval_T *argvars;
13286 typval_T *rettv;
13287{
13288 lval_T lv;
13289 char_u *end;
13290 dictitem_T *di;
13291
13292 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013293 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13294 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013295 if (end != NULL && lv.ll_name != NULL)
13296 {
13297 if (*end != NUL)
13298 EMSG(_(e_trailing));
13299 else
13300 {
13301 if (lv.ll_tv == NULL)
13302 {
13303 if (check_changedtick(lv.ll_name))
13304 rettv->vval.v_number = 1; /* always locked */
13305 else
13306 {
13307 di = find_var(lv.ll_name, NULL);
13308 if (di != NULL)
13309 {
13310 /* Consider a variable locked when:
13311 * 1. the variable itself is locked
13312 * 2. the value of the variable is locked.
13313 * 3. the List or Dict value is locked.
13314 */
13315 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13316 || tv_islocked(&di->di_tv));
13317 }
13318 }
13319 }
13320 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013321 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013322 else if (lv.ll_newkey != NULL)
13323 EMSG2(_(e_dictkey), lv.ll_newkey);
13324 else if (lv.ll_list != NULL)
13325 /* List item. */
13326 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13327 else
13328 /* Dictionary item. */
13329 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13330 }
13331 }
13332
13333 clear_lval(&lv);
13334}
13335
Bram Moolenaar33570922005-01-25 22:26:29 +000013336static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013337
13338/*
13339 * Turn a dict into a list:
13340 * "what" == 0: list of keys
13341 * "what" == 1: list of values
13342 * "what" == 2: list of items
13343 */
13344 static void
13345dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 typval_T *argvars;
13347 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013348 int what;
13349{
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 list_T *l2;
13351 dictitem_T *di;
13352 hashitem_T *hi;
13353 listitem_T *li;
13354 listitem_T *li2;
13355 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013356 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357
Bram Moolenaar8c711452005-01-14 21:53:12 +000013358 if (argvars[0].v_type != VAR_DICT)
13359 {
13360 EMSG(_(e_dictreq));
13361 return;
13362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013363 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364 return;
13365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013369 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013374 --todo;
13375 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013377 li = listitem_alloc();
13378 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013379 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013380 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 if (what == 0)
13383 {
13384 /* keys() */
13385 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013386 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013387 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13388 }
13389 else if (what == 1)
13390 {
13391 /* values() */
13392 copy_tv(&di->di_tv, &li->li_tv);
13393 }
13394 else
13395 {
13396 /* items() */
13397 l2 = list_alloc();
13398 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013399 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013400 li->li_tv.vval.v_list = l2;
13401 if (l2 == NULL)
13402 break;
13403 ++l2->lv_refcount;
13404
13405 li2 = listitem_alloc();
13406 if (li2 == NULL)
13407 break;
13408 list_append(l2, li2);
13409 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013410 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013411 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13412
13413 li2 = listitem_alloc();
13414 if (li2 == NULL)
13415 break;
13416 list_append(l2, li2);
13417 copy_tv(&di->di_tv, &li2->li_tv);
13418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013419 }
13420 }
13421}
13422
13423/*
13424 * "items(dict)" function
13425 */
13426 static void
13427f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *argvars;
13429 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430{
13431 dict_list(argvars, rettv, 2);
13432}
13433
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013435 * "join()" function
13436 */
13437 static void
13438f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013441{
13442 garray_T ga;
13443 char_u *sep;
13444
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013445 if (argvars[0].v_type != VAR_LIST)
13446 {
13447 EMSG(_(e_listreq));
13448 return;
13449 }
13450 if (argvars[0].vval.v_list == NULL)
13451 return;
13452 if (argvars[1].v_type == VAR_UNKNOWN)
13453 sep = (char_u *)" ";
13454 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013456
13457 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458
13459 if (sep != NULL)
13460 {
13461 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013462 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 ga_append(&ga, NUL);
13464 rettv->vval.v_string = (char_u *)ga.ga_data;
13465 }
13466 else
13467 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013468}
13469
13470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013471 * "keys()" function
13472 */
13473 static void
13474f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 typval_T *argvars;
13476 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013477{
13478 dict_list(argvars, rettv, 0);
13479}
13480
13481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 * "last_buffer_nr()" function.
13483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013486 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
13489 int n = 0;
13490 buf_T *buf;
13491
13492 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13493 if (n < buf->b_fnum)
13494 n = buf->b_fnum;
13495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013497}
13498
13499/*
13500 * "len()" function
13501 */
13502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 typval_T *argvars;
13505 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506{
13507 switch (argvars[0].v_type)
13508 {
13509 case VAR_STRING:
13510 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->vval.v_number = (varnumber_T)STRLEN(
13512 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513 break;
13514 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013516 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013517 case VAR_DICT:
13518 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013521 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522 break;
13523 }
13524}
13525
Bram Moolenaar33570922005-01-25 22:26:29 +000013526static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013527
13528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532 int type;
13533{
13534#ifdef FEAT_LIBCALL
13535 char_u *string_in;
13536 char_u **string_result;
13537 int nr_result;
13538#endif
13539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013541 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543
13544 if (check_restricted() || check_secure())
13545 return;
13546
13547#ifdef FEAT_LIBCALL
13548 /* The first two args must be strings, otherwise its meaningless */
13549 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13550 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013551 string_in = NULL;
13552 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553 string_in = argvars[2].vval.v_string;
13554 if (type == VAR_NUMBER)
13555 string_result = NULL;
13556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558 if (mch_libcall(argvars[0].vval.v_string,
13559 argvars[1].vval.v_string,
13560 string_in,
13561 argvars[2].vval.v_number,
13562 string_result,
13563 &nr_result) == OK
13564 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566 }
13567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568}
13569
13570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571 * "libcall()" function
13572 */
13573 static void
13574f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577{
13578 libcall_common(argvars, rettv, VAR_STRING);
13579}
13580
13581/*
13582 * "libcallnr()" function
13583 */
13584 static void
13585f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588{
13589 libcall_common(argvars, rettv, VAR_NUMBER);
13590}
13591
13592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 * "line(string)" function
13594 */
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600 linenr_T lnum = 0;
13601 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013602 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013604 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 if (fp != NULL)
13606 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608}
13609
13610/*
13611 * "line2byte(lnum)" function
13612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620#else
13621 linenr_T lnum;
13622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13628 if (rettv->vval.v_number >= 0)
13629 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630#endif
13631}
13632
13633/*
13634 * "lispindent(lnum)" function
13635 */
13636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641#ifdef FEAT_LISP
13642 pos_T pos;
13643 linenr_T lnum;
13644
13645 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13648 {
13649 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 curwin->w_cursor = pos;
13652 }
13653 else
13654#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656}
13657
13658/*
13659 * "localtime()" function
13660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013663 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667}
13668
Bram Moolenaar33570922005-01-25 22:26:29 +000013669static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
13671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 int exact;
13676{
13677 char_u *keys;
13678 char_u *which;
13679 char_u buf[NUMBUFLEN];
13680 char_u *keys_buf = NULL;
13681 char_u *rhs;
13682 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013683 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013684 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013685 mapblock_T *mp;
13686 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
13688 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 rettv->v_type = VAR_STRING;
13690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 if (*keys == NUL)
13694 return;
13695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013696 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013700 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013701 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013702 if (argvars[3].v_type != VAR_UNKNOWN)
13703 get_dict = get_tv_number(&argvars[3]);
13704 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 else
13707 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 if (which == NULL)
13709 return;
13710
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 mode = get_map_mode(&which, 0);
13712
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013713 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013714 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013716
13717 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013719 /* Return a string. */
13720 if (rhs != NULL)
13721 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722
Bram Moolenaarbd743252010-10-20 21:23:33 +020013723 }
13724 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13725 {
13726 /* Return a dictionary. */
13727 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13728 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13729 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaarbd743252010-10-20 21:23:33 +020013731 dict_add_nr_str(dict, "lhs", 0L, lhs);
13732 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13733 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13734 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13735 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13736 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13737 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013738 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013739 dict_add_nr_str(dict, "mode", 0L, mapmode);
13740
13741 vim_free(lhs);
13742 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743 }
13744}
13745
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013746#ifdef FEAT_FLOAT
13747/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013748 * "log()" function
13749 */
13750 static void
13751f_log(argvars, rettv)
13752 typval_T *argvars;
13753 typval_T *rettv;
13754{
13755 float_T f;
13756
13757 rettv->v_type = VAR_FLOAT;
13758 if (get_float_arg(argvars, &f) == OK)
13759 rettv->vval.v_float = log(f);
13760 else
13761 rettv->vval.v_float = 0.0;
13762}
13763
13764/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013765 * "log10()" function
13766 */
13767 static void
13768f_log10(argvars, rettv)
13769 typval_T *argvars;
13770 typval_T *rettv;
13771{
13772 float_T f;
13773
13774 rettv->v_type = VAR_FLOAT;
13775 if (get_float_arg(argvars, &f) == OK)
13776 rettv->vval.v_float = log10(f);
13777 else
13778 rettv->vval.v_float = 0.0;
13779}
13780#endif
13781
Bram Moolenaar1dced572012-04-05 16:54:08 +020013782#ifdef FEAT_LUA
13783/*
13784 * "luaeval()" function
13785 */
13786 static void
13787f_luaeval(argvars, rettv)
13788 typval_T *argvars;
13789 typval_T *rettv;
13790{
13791 char_u *str;
13792 char_u buf[NUMBUFLEN];
13793
13794 str = get_tv_string_buf(&argvars[0], buf);
13795 do_luaeval(str, argvars + 1, rettv);
13796}
13797#endif
13798
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013800 * "map()" function
13801 */
13802 static void
13803f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013804 typval_T *argvars;
13805 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806{
13807 filter_map(argvars, rettv, TRUE);
13808}
13809
13810/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013811 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 */
13813 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013814f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 typval_T *argvars;
13816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013818 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819}
13820
13821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 */
13824 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013825f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 typval_T *argvars;
13827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013829 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830}
13831
Bram Moolenaar33570922005-01-25 22:26:29 +000013832static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
13834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013835find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013836 typval_T *argvars;
13837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838 int type;
13839{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013840 char_u *str = NULL;
13841 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 char_u *pat;
13843 regmatch_T regmatch;
13844 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013845 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 char_u *save_cpo;
13847 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013848 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013849 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013850 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013851 list_T *l = NULL;
13852 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013853 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013854 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
13856 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13857 save_cpo = p_cpo;
13858 p_cpo = (char_u *)"";
13859
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013860 rettv->vval.v_number = -1;
13861 if (type == 3)
13862 {
13863 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013864 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013865 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013866 }
13867 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013869 rettv->v_type = VAR_STRING;
13870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013873 if (argvars[0].v_type == VAR_LIST)
13874 {
13875 if ((l = argvars[0].vval.v_list) == NULL)
13876 goto theend;
13877 li = l->lv_first;
13878 }
13879 else
13880 expr = str = get_tv_string(&argvars[0]);
13881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13883 if (pat == NULL)
13884 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013886 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013888 int error = FALSE;
13889
13890 start = get_tv_number_chk(&argvars[2], &error);
13891 if (error)
13892 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013893 if (l != NULL)
13894 {
13895 li = list_find(l, start);
13896 if (li == NULL)
13897 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013898 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013899 }
13900 else
13901 {
13902 if (start < 0)
13903 start = 0;
13904 if (start > (long)STRLEN(str))
13905 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013906 /* When "count" argument is there ignore matches before "start",
13907 * otherwise skip part of the string. Differs when pattern is "^"
13908 * or "\<". */
13909 if (argvars[3].v_type != VAR_UNKNOWN)
13910 startcol = start;
13911 else
13912 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013913 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013914
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013915 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916 nth = get_tv_number_chk(&argvars[3], &error);
13917 if (error)
13918 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 }
13920
13921 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13922 if (regmatch.regprog != NULL)
13923 {
13924 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013925
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013926 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013927 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 if (l != NULL)
13929 {
13930 if (li == NULL)
13931 {
13932 match = FALSE;
13933 break;
13934 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013935 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013936 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013937 if (str == NULL)
13938 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 }
13940
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013941 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013943 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013944 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 if (l == NULL && !match)
13946 break;
13947
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013948 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013949 if (l != NULL)
13950 {
13951 li = li->li_next;
13952 ++idx;
13953 }
13954 else
13955 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013956#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013957 startcol = (colnr_T)(regmatch.startp[0]
13958 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013959#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013960 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013961#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013963 }
13964
13965 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013967 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013969 int i;
13970
13971 /* return list with matched string and submatches */
13972 for (i = 0; i < NSUBEXP; ++i)
13973 {
13974 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013975 {
13976 if (list_append_string(rettv->vval.v_list,
13977 (char_u *)"", 0) == FAIL)
13978 break;
13979 }
13980 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013981 regmatch.startp[i],
13982 (int)(regmatch.endp[i] - regmatch.startp[i]))
13983 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013984 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013985 }
13986 }
13987 else if (type == 2)
13988 {
13989 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013990 if (l != NULL)
13991 copy_tv(&li->li_tv, rettv);
13992 else
13993 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995 }
13996 else if (l != NULL)
13997 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 else
13999 {
14000 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 (varnumber_T)(regmatch.startp[0] - str);
14003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014006 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 }
14008 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014009 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 }
14011
14012theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014013 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 p_cpo = save_cpo;
14015}
14016
14017/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014018 * "match()" function
14019 */
14020 static void
14021f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 typval_T *argvars;
14023 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014024{
14025 find_some_match(argvars, rettv, 1);
14026}
14027
14028/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014029 * "matchadd()" function
14030 */
14031 static void
14032f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014033 typval_T *argvars UNUSED;
14034 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014035{
14036#ifdef FEAT_SEARCH_EXTRA
14037 char_u buf[NUMBUFLEN];
14038 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14039 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14040 int prio = 10; /* default priority */
14041 int id = -1;
14042 int error = FALSE;
14043
14044 rettv->vval.v_number = -1;
14045
14046 if (grp == NULL || pat == NULL)
14047 return;
14048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014049 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014050 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014051 if (argvars[3].v_type != VAR_UNKNOWN)
14052 id = get_tv_number_chk(&argvars[3], &error);
14053 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014054 if (error == TRUE)
14055 return;
14056 if (id >= 1 && id <= 3)
14057 {
14058 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14059 return;
14060 }
14061
14062 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14063#endif
14064}
14065
14066/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014067 * "matcharg()" function
14068 */
14069 static void
14070f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014071 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014072 typval_T *rettv;
14073{
14074 if (rettv_list_alloc(rettv) == OK)
14075 {
14076#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014077 int id = get_tv_number(&argvars[0]);
14078 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014079
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014080 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014081 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014082 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14083 {
14084 list_append_string(rettv->vval.v_list,
14085 syn_id2name(m->hlg_id), -1);
14086 list_append_string(rettv->vval.v_list, m->pattern, -1);
14087 }
14088 else
14089 {
14090 list_append_string(rettv->vval.v_list, NUL, -1);
14091 list_append_string(rettv->vval.v_list, NUL, -1);
14092 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014093 }
14094#endif
14095 }
14096}
14097
14098/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014099 * "matchdelete()" function
14100 */
14101 static void
14102f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014103 typval_T *argvars UNUSED;
14104 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014105{
14106#ifdef FEAT_SEARCH_EXTRA
14107 rettv->vval.v_number = match_delete(curwin,
14108 (int)get_tv_number(&argvars[0]), TRUE);
14109#endif
14110}
14111
14112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014113 * "matchend()" function
14114 */
14115 static void
14116f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 typval_T *argvars;
14118 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119{
14120 find_some_match(argvars, rettv, 0);
14121}
14122
14123/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014124 * "matchlist()" function
14125 */
14126 static void
14127f_matchlist(argvars, rettv)
14128 typval_T *argvars;
14129 typval_T *rettv;
14130{
14131 find_some_match(argvars, rettv, 3);
14132}
14133
14134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135 * "matchstr()" function
14136 */
14137 static void
14138f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 typval_T *argvars;
14140 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014141{
14142 find_some_match(argvars, rettv, 2);
14143}
14144
Bram Moolenaar33570922005-01-25 22:26:29 +000014145static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014146
14147 static void
14148max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014149 typval_T *argvars;
14150 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014151 int domax;
14152{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014153 long n = 0;
14154 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014156
14157 if (argvars[0].v_type == VAR_LIST)
14158 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014159 list_T *l;
14160 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014161
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014162 l = argvars[0].vval.v_list;
14163 if (l != NULL)
14164 {
14165 li = l->lv_first;
14166 if (li != NULL)
14167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014169 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 {
14171 li = li->li_next;
14172 if (li == NULL)
14173 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014175 if (domax ? i > n : i < n)
14176 n = i;
14177 }
14178 }
14179 }
14180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014181 else if (argvars[0].v_type == VAR_DICT)
14182 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014184 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014186 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014187
14188 d = argvars[0].vval.v_dict;
14189 if (d != NULL)
14190 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014191 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014194 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014195 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014196 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014198 if (first)
14199 {
14200 n = i;
14201 first = FALSE;
14202 }
14203 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014204 n = i;
14205 }
14206 }
14207 }
14208 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014209 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014210 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014212}
14213
14214/*
14215 * "max()" function
14216 */
14217 static void
14218f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014219 typval_T *argvars;
14220 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014221{
14222 max_min(argvars, rettv, TRUE);
14223}
14224
14225/*
14226 * "min()" function
14227 */
14228 static void
14229f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014230 typval_T *argvars;
14231 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014232{
14233 max_min(argvars, rettv, FALSE);
14234}
14235
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014236static int mkdir_recurse __ARGS((char_u *dir, int prot));
14237
14238/*
14239 * Create the directory in which "dir" is located, and higher levels when
14240 * needed.
14241 */
14242 static int
14243mkdir_recurse(dir, prot)
14244 char_u *dir;
14245 int prot;
14246{
14247 char_u *p;
14248 char_u *updir;
14249 int r = FAIL;
14250
14251 /* Get end of directory name in "dir".
14252 * We're done when it's "/" or "c:/". */
14253 p = gettail_sep(dir);
14254 if (p <= get_past_head(dir))
14255 return OK;
14256
14257 /* If the directory exists we're done. Otherwise: create it.*/
14258 updir = vim_strnsave(dir, (int)(p - dir));
14259 if (updir == NULL)
14260 return FAIL;
14261 if (mch_isdir(updir))
14262 r = OK;
14263 else if (mkdir_recurse(updir, prot) == OK)
14264 r = vim_mkdir_emsg(updir, prot);
14265 vim_free(updir);
14266 return r;
14267}
14268
14269#ifdef vim_mkdir
14270/*
14271 * "mkdir()" function
14272 */
14273 static void
14274f_mkdir(argvars, rettv)
14275 typval_T *argvars;
14276 typval_T *rettv;
14277{
14278 char_u *dir;
14279 char_u buf[NUMBUFLEN];
14280 int prot = 0755;
14281
14282 rettv->vval.v_number = FAIL;
14283 if (check_restricted() || check_secure())
14284 return;
14285
14286 dir = get_tv_string_buf(&argvars[0], buf);
14287 if (argvars[1].v_type != VAR_UNKNOWN)
14288 {
14289 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 prot = get_tv_number_chk(&argvars[2], NULL);
14291 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014292 mkdir_recurse(dir, prot);
14293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014295}
14296#endif
14297
Bram Moolenaar0d660222005-01-07 21:51:51 +000014298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 * "mode()" function
14300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014303 typval_T *argvars;
14304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 char_u buf[3];
14307
14308 buf[1] = NUL;
14309 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310
14311#ifdef FEAT_VISUAL
14312 if (VIsual_active)
14313 {
14314 if (VIsual_select)
14315 buf[0] = VIsual_mode + 's' - 'v';
14316 else
14317 buf[0] = VIsual_mode;
14318 }
14319 else
14320#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014321 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14322 || State == CONFIRM)
14323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014325 if (State == ASKMORE)
14326 buf[1] = 'm';
14327 else if (State == CONFIRM)
14328 buf[1] = '?';
14329 }
14330 else if (State == EXTERNCMD)
14331 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 else if (State & INSERT)
14333 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014334#ifdef FEAT_VREPLACE
14335 if (State & VREPLACE_FLAG)
14336 {
14337 buf[0] = 'R';
14338 buf[1] = 'v';
14339 }
14340 else
14341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 if (State & REPLACE_FLAG)
14343 buf[0] = 'R';
14344 else
14345 buf[0] = 'i';
14346 }
14347 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014350 if (exmode_active)
14351 buf[1] = 'v';
14352 }
14353 else if (exmode_active)
14354 {
14355 buf[0] = 'c';
14356 buf[1] = 'e';
14357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014361 if (finish_op)
14362 buf[1] = 'o';
14363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014365 /* Clear out the minor mode when the argument is not a non-zero number or
14366 * non-empty string. */
14367 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014368 buf[1] = NUL;
14369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->vval.v_string = vim_strsave(buf);
14371 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372}
14373
Bram Moolenaar429fa852013-04-15 12:27:36 +020014374#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014375/*
14376 * "mzeval()" function
14377 */
14378 static void
14379f_mzeval(argvars, rettv)
14380 typval_T *argvars;
14381 typval_T *rettv;
14382{
14383 char_u *str;
14384 char_u buf[NUMBUFLEN];
14385
14386 str = get_tv_string_buf(&argvars[0], buf);
14387 do_mzeval(str, rettv);
14388}
Bram Moolenaar75676462013-01-30 14:55:42 +010014389
14390 void
14391mzscheme_call_vim(name, args, rettv)
14392 char_u *name;
14393 typval_T *args;
14394 typval_T *rettv;
14395{
14396 typval_T argvars[3];
14397
14398 argvars[0].v_type = VAR_STRING;
14399 argvars[0].vval.v_string = name;
14400 copy_tv(args, &argvars[1]);
14401 argvars[2].v_type = VAR_UNKNOWN;
14402 f_call(argvars, rettv);
14403 clear_tv(&argvars[1]);
14404}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014405#endif
14406
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014408 * "nextnonblank()" function
14409 */
14410 static void
14411f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 typval_T *argvars;
14413 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014414{
14415 linenr_T lnum;
14416
14417 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014420 {
14421 lnum = 0;
14422 break;
14423 }
14424 if (*skipwhite(ml_get(lnum)) != NUL)
14425 break;
14426 }
14427 rettv->vval.v_number = lnum;
14428}
14429
14430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 * "nr2char()" function
14432 */
14433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014435 typval_T *argvars;
14436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437{
14438 char_u buf[NUMBUFLEN];
14439
14440#ifdef FEAT_MBYTE
14441 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014442 {
14443 int utf8 = 0;
14444
14445 if (argvars[1].v_type != VAR_UNKNOWN)
14446 utf8 = get_tv_number_chk(&argvars[1], NULL);
14447 if (utf8)
14448 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14449 else
14450 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 else
14453#endif
14454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 buf[1] = NUL;
14457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->v_type = VAR_STRING;
14459 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014460}
14461
14462/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014463 * "or(expr, expr)" function
14464 */
14465 static void
14466f_or(argvars, rettv)
14467 typval_T *argvars;
14468 typval_T *rettv;
14469{
14470 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14471 | get_tv_number_chk(&argvars[1], NULL);
14472}
14473
14474/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014475 * "pathshorten()" function
14476 */
14477 static void
14478f_pathshorten(argvars, rettv)
14479 typval_T *argvars;
14480 typval_T *rettv;
14481{
14482 char_u *p;
14483
14484 rettv->v_type = VAR_STRING;
14485 p = get_tv_string_chk(&argvars[0]);
14486 if (p == NULL)
14487 rettv->vval.v_string = NULL;
14488 else
14489 {
14490 p = vim_strsave(p);
14491 rettv->vval.v_string = p;
14492 if (p != NULL)
14493 shorten_dir(p);
14494 }
14495}
14496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014497#ifdef FEAT_FLOAT
14498/*
14499 * "pow()" function
14500 */
14501 static void
14502f_pow(argvars, rettv)
14503 typval_T *argvars;
14504 typval_T *rettv;
14505{
14506 float_T fx, fy;
14507
14508 rettv->v_type = VAR_FLOAT;
14509 if (get_float_arg(argvars, &fx) == OK
14510 && get_float_arg(&argvars[1], &fy) == OK)
14511 rettv->vval.v_float = pow(fx, fy);
14512 else
14513 rettv->vval.v_float = 0.0;
14514}
14515#endif
14516
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014517/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014518 * "prevnonblank()" function
14519 */
14520 static void
14521f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014522 typval_T *argvars;
14523 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014524{
14525 linenr_T lnum;
14526
14527 lnum = get_tv_lnum(argvars);
14528 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14529 lnum = 0;
14530 else
14531 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14532 --lnum;
14533 rettv->vval.v_number = lnum;
14534}
14535
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014536#ifdef HAVE_STDARG_H
14537/* This dummy va_list is here because:
14538 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14539 * - locally in the function results in a "used before set" warning
14540 * - using va_start() to initialize it gives "function with fixed args" error */
14541static va_list ap;
14542#endif
14543
Bram Moolenaar8c711452005-01-14 21:53:12 +000014544/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014545 * "printf()" function
14546 */
14547 static void
14548f_printf(argvars, rettv)
14549 typval_T *argvars;
14550 typval_T *rettv;
14551{
14552 rettv->v_type = VAR_STRING;
14553 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014554#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014555 {
14556 char_u buf[NUMBUFLEN];
14557 int len;
14558 char_u *s;
14559 int saved_did_emsg = did_emsg;
14560 char *fmt;
14561
14562 /* Get the required length, allocate the buffer and do it for real. */
14563 did_emsg = FALSE;
14564 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014565 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014566 if (!did_emsg)
14567 {
14568 s = alloc(len + 1);
14569 if (s != NULL)
14570 {
14571 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014572 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014573 }
14574 }
14575 did_emsg |= saved_did_emsg;
14576 }
14577#endif
14578}
14579
14580/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014581 * "pumvisible()" function
14582 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014583 static void
14584f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014585 typval_T *argvars UNUSED;
14586 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014587{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014588#ifdef FEAT_INS_EXPAND
14589 if (pum_visible())
14590 rettv->vval.v_number = 1;
14591#endif
14592}
14593
Bram Moolenaardb913952012-06-29 12:54:53 +020014594#ifdef FEAT_PYTHON3
14595/*
14596 * "py3eval()" function
14597 */
14598 static void
14599f_py3eval(argvars, rettv)
14600 typval_T *argvars;
14601 typval_T *rettv;
14602{
14603 char_u *str;
14604 char_u buf[NUMBUFLEN];
14605
14606 str = get_tv_string_buf(&argvars[0], buf);
14607 do_py3eval(str, rettv);
14608}
14609#endif
14610
14611#ifdef FEAT_PYTHON
14612/*
14613 * "pyeval()" function
14614 */
14615 static void
14616f_pyeval(argvars, rettv)
14617 typval_T *argvars;
14618 typval_T *rettv;
14619{
14620 char_u *str;
14621 char_u buf[NUMBUFLEN];
14622
14623 str = get_tv_string_buf(&argvars[0], buf);
14624 do_pyeval(str, rettv);
14625}
14626#endif
14627
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014628/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 * "range()" function
14630 */
14631 static void
14632f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 typval_T *argvars;
14634 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635{
14636 long start;
14637 long end;
14638 long stride = 1;
14639 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014643 if (argvars[1].v_type == VAR_UNKNOWN)
14644 {
14645 end = start - 1;
14646 start = 0;
14647 }
14648 else
14649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014651 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014652 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014653 }
14654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014655 if (error)
14656 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014658 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014659 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014660 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661 else
14662 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014664 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014665 if (list_append_number(rettv->vval.v_list,
14666 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014667 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014668 }
14669}
14670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014671/*
14672 * "readfile()" function
14673 */
14674 static void
14675f_readfile(argvars, rettv)
14676 typval_T *argvars;
14677 typval_T *rettv;
14678{
14679 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014680 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014681 char_u *fname;
14682 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014683 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14684 int io_size = sizeof(buf);
14685 int readlen; /* size of last fread() */
14686 char_u *prev = NULL; /* previously read bytes, if any */
14687 long prevlen = 0; /* length of data in prev */
14688 long prevsize = 0; /* size of prev buffer */
14689 long maxline = MAXLNUM;
14690 long cnt = 0;
14691 char_u *p; /* position in buf */
14692 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014693
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014694 if (argvars[1].v_type != VAR_UNKNOWN)
14695 {
14696 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14697 binary = TRUE;
14698 if (argvars[2].v_type != VAR_UNKNOWN)
14699 maxline = get_tv_number(&argvars[2]);
14700 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014701
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014702 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014703 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014704
14705 /* Always open the file in binary mode, library functions have a mind of
14706 * their own about CR-LF conversion. */
14707 fname = get_tv_string(&argvars[0]);
14708 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14709 {
14710 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14711 return;
14712 }
14713
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014714 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014715 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014716 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014717
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014718 /* This for loop processes what was read, but is also entered at end
14719 * of file so that either:
14720 * - an incomplete line gets written
14721 * - a "binary" file gets an empty line at the end if it ends in a
14722 * newline. */
14723 for (p = buf, start = buf;
14724 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14725 ++p)
14726 {
14727 if (*p == '\n' || readlen <= 0)
14728 {
14729 listitem_T *li;
14730 char_u *s = NULL;
14731 long_u len = p - start;
14732
14733 /* Finished a line. Remove CRs before NL. */
14734 if (readlen > 0 && !binary)
14735 {
14736 while (len > 0 && start[len - 1] == '\r')
14737 --len;
14738 /* removal may cross back to the "prev" string */
14739 if (len == 0)
14740 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14741 --prevlen;
14742 }
14743 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014744 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014745 else
14746 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014747 /* Change "prev" buffer to be the right size. This way
14748 * the bytes are only copied once, and very long lines are
14749 * allocated only once. */
14750 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014751 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014752 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014754 prev = NULL; /* the list will own the string */
14755 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014756 }
14757 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 if (s == NULL)
14759 {
14760 do_outofmem_msg((long_u) prevlen + len + 1);
14761 failed = TRUE;
14762 break;
14763 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014764
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014765 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014766 {
14767 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014768 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769 break;
14770 }
14771 li->li_tv.v_type = VAR_STRING;
14772 li->li_tv.v_lock = 0;
14773 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014774 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014775
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014776 start = p + 1; /* step over newline */
14777 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014778 break;
14779 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014780 else if (*p == NUL)
14781 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014782#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14784 * when finding the BF and check the previous two bytes. */
14785 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014786 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014787 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14788 * + 1, these may be in the "prev" string. */
14789 char_u back1 = p >= buf + 1 ? p[-1]
14790 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14791 char_u back2 = p >= buf + 2 ? p[-2]
14792 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14793 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014794
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014795 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014796 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014797 char_u *dest = p - 2;
14798
14799 /* Usually a BOM is at the beginning of a file, and so at
14800 * the beginning of a line; then we can just step over it.
14801 */
14802 if (start == dest)
14803 start = p + 1;
14804 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014805 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 /* have to shuffle buf to close gap */
14807 int adjust_prevlen = 0;
14808
14809 if (dest < buf)
14810 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014811 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014812 dest = buf;
14813 }
14814 if (readlen > p - buf + 1)
14815 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14816 readlen -= 3 - adjust_prevlen;
14817 prevlen -= adjust_prevlen;
14818 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014819 }
14820 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822#endif
14823 } /* for */
14824
14825 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14826 break;
14827 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014828 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014829 /* There's part of a line in buf, store it in "prev". */
14830 if (p - start + prevlen >= prevsize)
14831 {
14832 /* need bigger "prev" buffer */
14833 char_u *newprev;
14834
14835 /* A common use case is ordinary text files and "prev" gets a
14836 * fragment of a line, so the first allocation is made
14837 * small, to avoid repeatedly 'allocing' large and
14838 * 'reallocing' small. */
14839 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014840 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014841 else
14842 {
14843 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014844 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014845 prevsize = grow50pc > growmin ? grow50pc : growmin;
14846 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014847 newprev = prev == NULL ? alloc(prevsize)
14848 : vim_realloc(prev, prevsize);
14849 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014850 {
14851 do_outofmem_msg((long_u)prevsize);
14852 failed = TRUE;
14853 break;
14854 }
14855 prev = newprev;
14856 }
14857 /* Add the line part to end of "prev". */
14858 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014859 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014860 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014861 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014863 /*
14864 * For a negative line count use only the lines at the end of the file,
14865 * free the rest.
14866 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014867 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014868 while (cnt > -maxline)
14869 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014870 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014871 --cnt;
14872 }
14873
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014874 if (failed)
14875 {
14876 list_free(rettv->vval.v_list, TRUE);
14877 /* readfile doc says an empty list is returned on error */
14878 rettv->vval.v_list = list_alloc();
14879 }
14880
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014881 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014882 fclose(fd);
14883}
14884
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014885#if defined(FEAT_RELTIME)
14886static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14887
14888/*
14889 * Convert a List to proftime_T.
14890 * Return FAIL when there is something wrong.
14891 */
14892 static int
14893list2proftime(arg, tm)
14894 typval_T *arg;
14895 proftime_T *tm;
14896{
14897 long n1, n2;
14898 int error = FALSE;
14899
14900 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14901 || arg->vval.v_list->lv_len != 2)
14902 return FAIL;
14903 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14904 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14905# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014906 tm->HighPart = n1;
14907 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014908# else
14909 tm->tv_sec = n1;
14910 tm->tv_usec = n2;
14911# endif
14912 return error ? FAIL : OK;
14913}
14914#endif /* FEAT_RELTIME */
14915
14916/*
14917 * "reltime()" function
14918 */
14919 static void
14920f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014921 typval_T *argvars UNUSED;
14922 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014923{
14924#ifdef FEAT_RELTIME
14925 proftime_T res;
14926 proftime_T start;
14927
14928 if (argvars[0].v_type == VAR_UNKNOWN)
14929 {
14930 /* No arguments: get current time. */
14931 profile_start(&res);
14932 }
14933 else if (argvars[1].v_type == VAR_UNKNOWN)
14934 {
14935 if (list2proftime(&argvars[0], &res) == FAIL)
14936 return;
14937 profile_end(&res);
14938 }
14939 else
14940 {
14941 /* Two arguments: compute the difference. */
14942 if (list2proftime(&argvars[0], &start) == FAIL
14943 || list2proftime(&argvars[1], &res) == FAIL)
14944 return;
14945 profile_sub(&res, &start);
14946 }
14947
14948 if (rettv_list_alloc(rettv) == OK)
14949 {
14950 long n1, n2;
14951
14952# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014953 n1 = res.HighPart;
14954 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014955# else
14956 n1 = res.tv_sec;
14957 n2 = res.tv_usec;
14958# endif
14959 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14960 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14961 }
14962#endif
14963}
14964
14965/*
14966 * "reltimestr()" function
14967 */
14968 static void
14969f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014970 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014971 typval_T *rettv;
14972{
14973#ifdef FEAT_RELTIME
14974 proftime_T tm;
14975#endif
14976
14977 rettv->v_type = VAR_STRING;
14978 rettv->vval.v_string = NULL;
14979#ifdef FEAT_RELTIME
14980 if (list2proftime(&argvars[0], &tm) == OK)
14981 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14982#endif
14983}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014984
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14986static void make_connection __ARGS((void));
14987static int check_connection __ARGS((void));
14988
14989 static void
14990make_connection()
14991{
14992 if (X_DISPLAY == NULL
14993# ifdef FEAT_GUI
14994 && !gui.in_use
14995# endif
14996 )
14997 {
14998 x_force_connect = TRUE;
14999 setup_term_clip();
15000 x_force_connect = FALSE;
15001 }
15002}
15003
15004 static int
15005check_connection()
15006{
15007 make_connection();
15008 if (X_DISPLAY == NULL)
15009 {
15010 EMSG(_("E240: No connection to Vim server"));
15011 return FAIL;
15012 }
15013 return OK;
15014}
15015#endif
15016
15017#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015018static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015019
15020 static void
15021remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015022 typval_T *argvars;
15023 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015024 int expr;
15025{
15026 char_u *server_name;
15027 char_u *keys;
15028 char_u *r = NULL;
15029 char_u buf[NUMBUFLEN];
15030# ifdef WIN32
15031 HWND w;
15032# else
15033 Window w;
15034# endif
15035
15036 if (check_restricted() || check_secure())
15037 return;
15038
15039# ifdef FEAT_X11
15040 if (check_connection() == FAIL)
15041 return;
15042# endif
15043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015044 server_name = get_tv_string_chk(&argvars[0]);
15045 if (server_name == NULL)
15046 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047 keys = get_tv_string_buf(&argvars[1], buf);
15048# ifdef WIN32
15049 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15050# else
15051 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15052 < 0)
15053# endif
15054 {
15055 if (r != NULL)
15056 EMSG(r); /* sending worked but evaluation failed */
15057 else
15058 EMSG2(_("E241: Unable to send to %s"), server_name);
15059 return;
15060 }
15061
15062 rettv->vval.v_string = r;
15063
15064 if (argvars[2].v_type != VAR_UNKNOWN)
15065 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015066 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015067 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015070 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015071 v.di_tv.v_type = VAR_STRING;
15072 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 idvar = get_tv_string_chk(&argvars[2]);
15074 if (idvar != NULL)
15075 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015076 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077 }
15078}
15079#endif
15080
15081/*
15082 * "remote_expr()" function
15083 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 static void
15085f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015086 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088{
15089 rettv->v_type = VAR_STRING;
15090 rettv->vval.v_string = NULL;
15091#ifdef FEAT_CLIENTSERVER
15092 remote_common(argvars, rettv, TRUE);
15093#endif
15094}
15095
15096/*
15097 * "remote_foreground()" function
15098 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 static void
15100f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015101 typval_T *argvars UNUSED;
15102 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104#ifdef FEAT_CLIENTSERVER
15105# ifdef WIN32
15106 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015107 {
15108 char_u *server_name = get_tv_string_chk(&argvars[0]);
15109
15110 if (server_name != NULL)
15111 serverForeground(server_name);
15112 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113# else
15114 /* Send a foreground() expression to the server. */
15115 argvars[1].v_type = VAR_STRING;
15116 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15117 argvars[2].v_type = VAR_UNKNOWN;
15118 remote_common(argvars, rettv, TRUE);
15119 vim_free(argvars[1].vval.v_string);
15120# endif
15121#endif
15122}
15123
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124 static void
15125f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015126 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128{
15129#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015131 char_u *s = NULL;
15132# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015133 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015134# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015135 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136
15137 if (check_restricted() || check_secure())
15138 {
15139 rettv->vval.v_number = -1;
15140 return;
15141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 serverid = get_tv_string_chk(&argvars[0]);
15143 if (serverid == NULL)
15144 {
15145 rettv->vval.v_number = -1;
15146 return; /* type error; errmsg already given */
15147 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015149 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 if (n == 0)
15151 rettv->vval.v_number = -1;
15152 else
15153 {
15154 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15155 rettv->vval.v_number = (s != NULL);
15156 }
15157# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 if (check_connection() == FAIL)
15159 return;
15160
15161 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015162 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163# endif
15164
15165 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 char_u *retvar;
15168
Bram Moolenaar33570922005-01-25 22:26:29 +000015169 v.di_tv.v_type = VAR_STRING;
15170 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015171 retvar = get_tv_string_chk(&argvars[1]);
15172 if (retvar != NULL)
15173 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015174 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 }
15176#else
15177 rettv->vval.v_number = -1;
15178#endif
15179}
15180
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 static void
15182f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185{
15186 char_u *r = NULL;
15187
15188#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015189 char_u *serverid = get_tv_string_chk(&argvars[0]);
15190
15191 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 {
15193# ifdef WIN32
15194 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015195 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015196
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015197 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015198 if (n != 0)
15199 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15200 if (r == NULL)
15201# else
15202 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204# endif
15205 EMSG(_("E277: Unable to read a server reply"));
15206 }
15207#endif
15208 rettv->v_type = VAR_STRING;
15209 rettv->vval.v_string = r;
15210}
15211
15212/*
15213 * "remote_send()" function
15214 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015215 static void
15216f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015217 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015218 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219{
15220 rettv->v_type = VAR_STRING;
15221 rettv->vval.v_string = NULL;
15222#ifdef FEAT_CLIENTSERVER
15223 remote_common(argvars, rettv, FALSE);
15224#endif
15225}
15226
15227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015229 */
15230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *argvars;
15233 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015234{
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 list_T *l;
15236 listitem_T *item, *item2;
15237 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015238 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015239 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015241 dict_T *d;
15242 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015243 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015244
Bram Moolenaar8c711452005-01-14 21:53:12 +000015245 if (argvars[0].v_type == VAR_DICT)
15246 {
15247 if (argvars[2].v_type != VAR_UNKNOWN)
15248 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015249 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015250 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 key = get_tv_string_chk(&argvars[1]);
15253 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015255 di = dict_find(d, key, -1);
15256 if (di == NULL)
15257 EMSG2(_(e_dictkey), key);
15258 else
15259 {
15260 *rettv = di->di_tv;
15261 init_tv(&di->di_tv);
15262 dictitem_remove(d, di);
15263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015265 }
15266 }
15267 else if (argvars[0].v_type != VAR_LIST)
15268 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015269 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015270 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 int error = FALSE;
15273
15274 idx = get_tv_number_chk(&argvars[1], &error);
15275 if (error)
15276 ; /* type error: do nothing, errmsg already given */
15277 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015278 EMSGN(_(e_listidx), idx);
15279 else
15280 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015281 if (argvars[2].v_type == VAR_UNKNOWN)
15282 {
15283 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015284 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015285 *rettv = item->li_tv;
15286 vim_free(item);
15287 }
15288 else
15289 {
15290 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 end = get_tv_number_chk(&argvars[2], &error);
15292 if (error)
15293 ; /* type error: do nothing */
15294 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015295 EMSGN(_(e_listidx), end);
15296 else
15297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015298 int cnt = 0;
15299
15300 for (li = item; li != NULL; li = li->li_next)
15301 {
15302 ++cnt;
15303 if (li == item2)
15304 break;
15305 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015306 if (li == NULL) /* didn't find "item2" after "item" */
15307 EMSG(_(e_invrange));
15308 else
15309 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015310 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015311 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015312 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015313 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 l->lv_first = item;
15315 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316 item->li_prev = NULL;
15317 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015318 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015319 }
15320 }
15321 }
15322 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015323 }
15324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325}
15326
15327/*
15328 * "rename({from}, {to})" function
15329 */
15330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015332 typval_T *argvars;
15333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334{
15335 char_u buf[NUMBUFLEN];
15336
15337 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015340 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15341 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342}
15343
15344/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015345 * "repeat()" function
15346 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015347 static void
15348f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015349 typval_T *argvars;
15350 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015351{
15352 char_u *p;
15353 int n;
15354 int slen;
15355 int len;
15356 char_u *r;
15357 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015358
15359 n = get_tv_number(&argvars[1]);
15360 if (argvars[0].v_type == VAR_LIST)
15361 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015362 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015363 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015364 if (list_extend(rettv->vval.v_list,
15365 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015367 }
15368 else
15369 {
15370 p = get_tv_string(&argvars[0]);
15371 rettv->v_type = VAR_STRING;
15372 rettv->vval.v_string = NULL;
15373
15374 slen = (int)STRLEN(p);
15375 len = slen * n;
15376 if (len <= 0)
15377 return;
15378
15379 r = alloc(len + 1);
15380 if (r != NULL)
15381 {
15382 for (i = 0; i < n; i++)
15383 mch_memmove(r + i * slen, p, (size_t)slen);
15384 r[len] = NUL;
15385 }
15386
15387 rettv->vval.v_string = r;
15388 }
15389}
15390
15391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392 * "resolve()" function
15393 */
15394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015396 typval_T *argvars;
15397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398{
15399 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015400#ifdef HAVE_READLINK
15401 char_u *buf = NULL;
15402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405#ifdef FEAT_SHORTCUT
15406 {
15407 char_u *v = NULL;
15408
15409 v = mch_resolve_shortcut(p);
15410 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 }
15415#else
15416# ifdef HAVE_READLINK
15417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 char_u *cpy;
15419 int len;
15420 char_u *remain = NULL;
15421 char_u *q;
15422 int is_relative_to_current = FALSE;
15423 int has_trailing_pathsep = FALSE;
15424 int limit = 100;
15425
15426 p = vim_strsave(p);
15427
15428 if (p[0] == '.' && (vim_ispathsep(p[1])
15429 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15430 is_relative_to_current = TRUE;
15431
15432 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015433 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015436 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
15439 q = getnextcomp(p);
15440 if (*q != NUL)
15441 {
15442 /* Separate the first path component in "p", and keep the
15443 * remainder (beginning with the path separator). */
15444 remain = vim_strsave(q - 1);
15445 q[-1] = NUL;
15446 }
15447
Bram Moolenaard9462e32011-04-11 21:35:11 +020015448 buf = alloc(MAXPATHL + 1);
15449 if (buf == NULL)
15450 goto fail;
15451
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 for (;;)
15453 {
15454 for (;;)
15455 {
15456 len = readlink((char *)p, (char *)buf, MAXPATHL);
15457 if (len <= 0)
15458 break;
15459 buf[len] = NUL;
15460
15461 if (limit-- == 0)
15462 {
15463 vim_free(p);
15464 vim_free(remain);
15465 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 goto fail;
15468 }
15469
15470 /* Ensure that the result will have a trailing path separator
15471 * if the argument has one. */
15472 if (remain == NULL && has_trailing_pathsep)
15473 add_pathsep(buf);
15474
15475 /* Separate the first path component in the link value and
15476 * concatenate the remainders. */
15477 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15478 if (*q != NUL)
15479 {
15480 if (remain == NULL)
15481 remain = vim_strsave(q - 1);
15482 else
15483 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015484 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 if (cpy != NULL)
15486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 vim_free(remain);
15488 remain = cpy;
15489 }
15490 }
15491 q[-1] = NUL;
15492 }
15493
15494 q = gettail(p);
15495 if (q > p && *q == NUL)
15496 {
15497 /* Ignore trailing path separator. */
15498 q[-1] = NUL;
15499 q = gettail(p);
15500 }
15501 if (q > p && !mch_isFullName(buf))
15502 {
15503 /* symlink is relative to directory of argument */
15504 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15505 if (cpy != NULL)
15506 {
15507 STRCPY(cpy, p);
15508 STRCPY(gettail(cpy), buf);
15509 vim_free(p);
15510 p = cpy;
15511 }
15512 }
15513 else
15514 {
15515 vim_free(p);
15516 p = vim_strsave(buf);
15517 }
15518 }
15519
15520 if (remain == NULL)
15521 break;
15522
15523 /* Append the first path component of "remain" to "p". */
15524 q = getnextcomp(remain + 1);
15525 len = q - remain - (*q != NUL);
15526 cpy = vim_strnsave(p, STRLEN(p) + len);
15527 if (cpy != NULL)
15528 {
15529 STRNCAT(cpy, remain, len);
15530 vim_free(p);
15531 p = cpy;
15532 }
15533 /* Shorten "remain". */
15534 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015535 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 else
15537 {
15538 vim_free(remain);
15539 remain = NULL;
15540 }
15541 }
15542
15543 /* If the result is a relative path name, make it explicitly relative to
15544 * the current directory if and only if the argument had this form. */
15545 if (!vim_ispathsep(*p))
15546 {
15547 if (is_relative_to_current
15548 && *p != NUL
15549 && !(p[0] == '.'
15550 && (p[1] == NUL
15551 || vim_ispathsep(p[1])
15552 || (p[1] == '.'
15553 && (p[2] == NUL
15554 || vim_ispathsep(p[2]))))))
15555 {
15556 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015557 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 if (cpy != NULL)
15559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 vim_free(p);
15561 p = cpy;
15562 }
15563 }
15564 else if (!is_relative_to_current)
15565 {
15566 /* Strip leading "./". */
15567 q = p;
15568 while (q[0] == '.' && vim_ispathsep(q[1]))
15569 q += 2;
15570 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015571 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 }
15573 }
15574
15575 /* Ensure that the result will have no trailing path separator
15576 * if the argument had none. But keep "/" or "//". */
15577 if (!has_trailing_pathsep)
15578 {
15579 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015580 if (after_pathsep(p, q))
15581 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 }
15583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 }
15586# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588# endif
15589#endif
15590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592
15593#ifdef HAVE_READLINK
15594fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015595 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598}
15599
15600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 */
15603 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *argvars;
15606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607{
Bram Moolenaar33570922005-01-25 22:26:29 +000015608 list_T *l;
15609 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 if (argvars[0].v_type != VAR_LIST)
15612 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015613 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015614 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615 {
15616 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015617 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015618 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 while (li != NULL)
15620 {
15621 ni = li->li_prev;
15622 list_append(l, li);
15623 li = ni;
15624 }
15625 rettv->vval.v_list = l;
15626 rettv->v_type = VAR_LIST;
15627 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015628 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630}
15631
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015632#define SP_NOMOVE 0x01 /* don't move cursor */
15633#define SP_REPEAT 0x02 /* repeat to find outer pair */
15634#define SP_RETCOUNT 0x04 /* return matchcount */
15635#define SP_SETPCMARK 0x08 /* set previous context mark */
15636#define SP_START 0x10 /* accept match at start position */
15637#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15638#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015639
Bram Moolenaar33570922005-01-25 22:26:29 +000015640static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641
15642/*
15643 * Get flags for a search function.
15644 * Possibly sets "p_ws".
15645 * Returns BACKWARD, FORWARD or zero (for an error).
15646 */
15647 static int
15648get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015650 int *flagsp;
15651{
15652 int dir = FORWARD;
15653 char_u *flags;
15654 char_u nbuf[NUMBUFLEN];
15655 int mask;
15656
15657 if (varp->v_type != VAR_UNKNOWN)
15658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015659 flags = get_tv_string_buf_chk(varp, nbuf);
15660 if (flags == NULL)
15661 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662 while (*flags != NUL)
15663 {
15664 switch (*flags)
15665 {
15666 case 'b': dir = BACKWARD; break;
15667 case 'w': p_ws = TRUE; break;
15668 case 'W': p_ws = FALSE; break;
15669 default: mask = 0;
15670 if (flagsp != NULL)
15671 switch (*flags)
15672 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015673 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015674 case 'e': mask = SP_END; break;
15675 case 'm': mask = SP_RETCOUNT; break;
15676 case 'n': mask = SP_NOMOVE; break;
15677 case 'p': mask = SP_SUBPAT; break;
15678 case 'r': mask = SP_REPEAT; break;
15679 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 }
15681 if (mask == 0)
15682 {
15683 EMSG2(_(e_invarg2), flags);
15684 dir = 0;
15685 }
15686 else
15687 *flagsp |= mask;
15688 }
15689 if (dir == 0)
15690 break;
15691 ++flags;
15692 }
15693 }
15694 return dir;
15695}
15696
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015698 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015700 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015701search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015702 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015703 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015704 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015706 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 char_u *pat;
15708 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015709 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 int save_p_ws = p_ws;
15711 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015712 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015713 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015714 proftime_T tm;
15715#ifdef FEAT_RELTIME
15716 long time_limit = 0;
15717#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015718 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015719 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015721 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015722 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015723 if (dir == 0)
15724 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015725 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015726 if (flags & SP_START)
15727 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015728 if (flags & SP_END)
15729 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015730
Bram Moolenaar76929292008-01-06 19:07:36 +000015731 /* Optional arguments: line number to stop searching and timeout. */
15732 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015733 {
15734 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15735 if (lnum_stop < 0)
15736 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015737#ifdef FEAT_RELTIME
15738 if (argvars[3].v_type != VAR_UNKNOWN)
15739 {
15740 time_limit = get_tv_number_chk(&argvars[3], NULL);
15741 if (time_limit < 0)
15742 goto theend;
15743 }
15744#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015745 }
15746
Bram Moolenaar76929292008-01-06 19:07:36 +000015747#ifdef FEAT_RELTIME
15748 /* Set the time limit, if there is one. */
15749 profile_setlimit(time_limit, &tm);
15750#endif
15751
Bram Moolenaar231334e2005-07-25 20:46:57 +000015752 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015753 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015754 * Check to make sure only those flags are set.
15755 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15756 * flags cannot be set. Check for that condition also.
15757 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015758 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015759 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015762 goto theend;
15763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015765 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015766 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015767 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015768 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015770 if (flags & SP_SUBPAT)
15771 retval = subpatnum;
15772 else
15773 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015774 if (flags & SP_SETPCMARK)
15775 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015777 if (match_pos != NULL)
15778 {
15779 /* Store the match cursor position */
15780 match_pos->lnum = pos.lnum;
15781 match_pos->col = pos.col + 1;
15782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 /* "/$" will put the cursor after the end of the line, may need to
15784 * correct that here */
15785 check_cursor();
15786 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015787
15788 /* If 'n' flag is used: restore cursor position. */
15789 if (flags & SP_NOMOVE)
15790 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015791 else
15792 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015793theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015795
15796 return retval;
15797}
15798
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015799#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015800
15801/*
15802 * round() is not in C90, use ceil() or floor() instead.
15803 */
15804 float_T
15805vim_round(f)
15806 float_T f;
15807{
15808 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15809}
15810
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015811/*
15812 * "round({float})" function
15813 */
15814 static void
15815f_round(argvars, rettv)
15816 typval_T *argvars;
15817 typval_T *rettv;
15818{
15819 float_T f;
15820
15821 rettv->v_type = VAR_FLOAT;
15822 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015823 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015824 else
15825 rettv->vval.v_float = 0.0;
15826}
15827#endif
15828
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015829/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015830 * "screenattr()" function
15831 */
15832 static void
15833f_screenattr(argvars, rettv)
15834 typval_T *argvars UNUSED;
15835 typval_T *rettv;
15836{
15837 int row;
15838 int col;
15839 int c;
15840
15841 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15842 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15843 if (row < 0 || row >= screen_Rows
15844 || col < 0 || col >= screen_Columns)
15845 c = -1;
15846 else
15847 c = ScreenAttrs[LineOffset[row] + col];
15848 rettv->vval.v_number = c;
15849}
15850
15851/*
15852 * "screenchar()" function
15853 */
15854 static void
15855f_screenchar(argvars, rettv)
15856 typval_T *argvars UNUSED;
15857 typval_T *rettv;
15858{
15859 int row;
15860 int col;
15861 int off;
15862 int c;
15863
15864 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15865 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15866 if (row < 0 || row >= screen_Rows
15867 || col < 0 || col >= screen_Columns)
15868 c = -1;
15869 else
15870 {
15871 off = LineOffset[row] + col;
15872#ifdef FEAT_MBYTE
15873 if (enc_utf8 && ScreenLinesUC[off] != 0)
15874 c = ScreenLinesUC[off];
15875 else
15876#endif
15877 c = ScreenLines[off];
15878 }
15879 rettv->vval.v_number = c;
15880}
15881
15882/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015883 * "screencol()" function
15884 *
15885 * First column is 1 to be consistent with virtcol().
15886 */
15887 static void
15888f_screencol(argvars, rettv)
15889 typval_T *argvars UNUSED;
15890 typval_T *rettv;
15891{
15892 rettv->vval.v_number = screen_screencol() + 1;
15893}
15894
15895/*
15896 * "screenrow()" function
15897 */
15898 static void
15899f_screenrow(argvars, rettv)
15900 typval_T *argvars UNUSED;
15901 typval_T *rettv;
15902{
15903 rettv->vval.v_number = screen_screenrow() + 1;
15904}
15905
15906/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015907 * "search()" function
15908 */
15909 static void
15910f_search(argvars, rettv)
15911 typval_T *argvars;
15912 typval_T *rettv;
15913{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015914 int flags = 0;
15915
15916 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917}
15918
Bram Moolenaar071d4272004-06-13 20:20:40 +000015919/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015920 * "searchdecl()" function
15921 */
15922 static void
15923f_searchdecl(argvars, rettv)
15924 typval_T *argvars;
15925 typval_T *rettv;
15926{
15927 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015928 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015929 int error = FALSE;
15930 char_u *name;
15931
15932 rettv->vval.v_number = 1; /* default: FAIL */
15933
15934 name = get_tv_string_chk(&argvars[0]);
15935 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015936 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015937 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015938 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15939 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15940 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015941 if (!error && name != NULL)
15942 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015943 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015944}
15945
15946/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015947 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015948 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949 static int
15950searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015951 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015952 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015953{
15954 char_u *spat, *mpat, *epat;
15955 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 int dir;
15958 int flags = 0;
15959 char_u nbuf1[NUMBUFLEN];
15960 char_u nbuf2[NUMBUFLEN];
15961 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015962 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015964 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015967 spat = get_tv_string_chk(&argvars[0]);
15968 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15969 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15970 if (spat == NULL || mpat == NULL || epat == NULL)
15971 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 /* Handle the optional fourth argument: flags */
15974 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015975 if (dir == 0)
15976 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015977
15978 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015979 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15980 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015981 if ((flags & (SP_END | SP_SUBPAT)) != 0
15982 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015983 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015984 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015985 goto theend;
15986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015988 /* Using 'r' implies 'W', otherwise it doesn't work. */
15989 if (flags & SP_REPEAT)
15990 p_ws = FALSE;
15991
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015992 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015993 if (argvars[3].v_type == VAR_UNKNOWN
15994 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995 skip = (char_u *)"";
15996 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015999 if (argvars[5].v_type != VAR_UNKNOWN)
16000 {
16001 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16002 if (lnum_stop < 0)
16003 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016004#ifdef FEAT_RELTIME
16005 if (argvars[6].v_type != VAR_UNKNOWN)
16006 {
16007 time_limit = get_tv_number_chk(&argvars[6], NULL);
16008 if (time_limit < 0)
16009 goto theend;
16010 }
16011#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016012 }
16013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016014 if (skip == NULL)
16015 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016017 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016018 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016019
16020theend:
16021 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016022
16023 return retval;
16024}
16025
16026/*
16027 * "searchpair()" function
16028 */
16029 static void
16030f_searchpair(argvars, rettv)
16031 typval_T *argvars;
16032 typval_T *rettv;
16033{
16034 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16035}
16036
16037/*
16038 * "searchpairpos()" function
16039 */
16040 static void
16041f_searchpairpos(argvars, rettv)
16042 typval_T *argvars;
16043 typval_T *rettv;
16044{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016045 pos_T match_pos;
16046 int lnum = 0;
16047 int col = 0;
16048
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016049 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016051
16052 if (searchpair_cmn(argvars, &match_pos) > 0)
16053 {
16054 lnum = match_pos.lnum;
16055 col = match_pos.col;
16056 }
16057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016058 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16059 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016060}
16061
16062/*
16063 * Search for a start/middle/end thing.
16064 * Used by searchpair(), see its documentation for the details.
16065 * Returns 0 or -1 for no match,
16066 */
16067 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016068do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16069 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016070 char_u *spat; /* start pattern */
16071 char_u *mpat; /* middle pattern */
16072 char_u *epat; /* end pattern */
16073 int dir; /* BACKWARD or FORWARD */
16074 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016075 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016076 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016077 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016078 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016079{
16080 char_u *save_cpo;
16081 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16082 long retval = 0;
16083 pos_T pos;
16084 pos_T firstpos;
16085 pos_T foundpos;
16086 pos_T save_cursor;
16087 pos_T save_pos;
16088 int n;
16089 int r;
16090 int nest = 1;
16091 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016092 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016093 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016094
16095 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16096 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016097 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016098
Bram Moolenaar76929292008-01-06 19:07:36 +000016099#ifdef FEAT_RELTIME
16100 /* Set the time limit, if there is one. */
16101 profile_setlimit(time_limit, &tm);
16102#endif
16103
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016104 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16105 * start/middle/end (pat3, for the top pair). */
16106 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16107 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16108 if (pat2 == NULL || pat3 == NULL)
16109 goto theend;
16110 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16111 if (*mpat == NUL)
16112 STRCPY(pat3, pat2);
16113 else
16114 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16115 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016116 if (flags & SP_START)
16117 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016118
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 save_cursor = curwin->w_cursor;
16120 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016121 clearpos(&firstpos);
16122 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123 pat = pat3;
16124 for (;;)
16125 {
16126 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016127 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16129 /* didn't find it or found the first match again: FAIL */
16130 break;
16131
16132 if (firstpos.lnum == 0)
16133 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016134 if (equalpos(pos, foundpos))
16135 {
16136 /* Found the same position again. Can happen with a pattern that
16137 * has "\zs" at the end and searching backwards. Advance one
16138 * character and try again. */
16139 if (dir == BACKWARD)
16140 decl(&pos);
16141 else
16142 incl(&pos);
16143 }
16144 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016146 /* clear the start flag to avoid getting stuck here */
16147 options &= ~SEARCH_START;
16148
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 /* If the skip pattern matches, ignore this match. */
16150 if (*skip != NUL)
16151 {
16152 save_pos = curwin->w_cursor;
16153 curwin->w_cursor = pos;
16154 r = eval_to_bool(skip, &err, NULL, FALSE);
16155 curwin->w_cursor = save_pos;
16156 if (err)
16157 {
16158 /* Evaluating {skip} caused an error, break here. */
16159 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016160 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 break;
16162 }
16163 if (r)
16164 continue;
16165 }
16166
16167 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16168 {
16169 /* Found end when searching backwards or start when searching
16170 * forward: nested pair. */
16171 ++nest;
16172 pat = pat2; /* nested, don't search for middle */
16173 }
16174 else
16175 {
16176 /* Found end when searching forward or start when searching
16177 * backward: end of (nested) pair; or found middle in outer pair. */
16178 if (--nest == 1)
16179 pat = pat3; /* outer level, search for middle */
16180 }
16181
16182 if (nest == 0)
16183 {
16184 /* Found the match: return matchcount or line number. */
16185 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016186 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016188 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016189 if (flags & SP_SETPCMARK)
16190 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 curwin->w_cursor = pos;
16192 if (!(flags & SP_REPEAT))
16193 break;
16194 nest = 1; /* search for next unmatched */
16195 }
16196 }
16197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198 if (match_pos != NULL)
16199 {
16200 /* Store the match cursor position */
16201 match_pos->lnum = curwin->w_cursor.lnum;
16202 match_pos->col = curwin->w_cursor.col + 1;
16203 }
16204
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016206 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 curwin->w_cursor = save_cursor;
16208
16209theend:
16210 vim_free(pat2);
16211 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016212 if (p_cpo == empty_option)
16213 p_cpo = save_cpo;
16214 else
16215 /* Darn, evaluating the {skip} expression changed the value. */
16216 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016217
16218 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219}
16220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016221/*
16222 * "searchpos()" function
16223 */
16224 static void
16225f_searchpos(argvars, rettv)
16226 typval_T *argvars;
16227 typval_T *rettv;
16228{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016229 pos_T match_pos;
16230 int lnum = 0;
16231 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016232 int n;
16233 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016235 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016236 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016237
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016238 n = search_cmn(argvars, &match_pos, &flags);
16239 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016240 {
16241 lnum = match_pos.lnum;
16242 col = match_pos.col;
16243 }
16244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016245 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16246 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016247 if (flags & SP_SUBPAT)
16248 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016249}
16250
16251
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252 static void
16253f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016254 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257#ifdef FEAT_CLIENTSERVER
16258 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259 char_u *server = get_tv_string_chk(&argvars[0]);
16260 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 if (server == NULL || reply == NULL)
16264 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 if (check_restricted() || check_secure())
16266 return;
16267# ifdef FEAT_X11
16268 if (check_connection() == FAIL)
16269 return;
16270# endif
16271
16272 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 EMSG(_("E258: Unable to send to client"));
16275 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 rettv->vval.v_number = 0;
16278#else
16279 rettv->vval.v_number = -1;
16280#endif
16281}
16282
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283 static void
16284f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016285 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287{
16288 char_u *r = NULL;
16289
16290#ifdef FEAT_CLIENTSERVER
16291# ifdef WIN32
16292 r = serverGetVimNames();
16293# else
16294 make_connection();
16295 if (X_DISPLAY != NULL)
16296 r = serverGetVimNames(X_DISPLAY);
16297# endif
16298#endif
16299 rettv->v_type = VAR_STRING;
16300 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301}
16302
16303/*
16304 * "setbufvar()" function
16305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016307f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016309 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
16311 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016314 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 char_u nbuf[NUMBUFLEN];
16316
16317 if (check_restricted() || check_secure())
16318 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16320 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016321 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 varp = &argvars[2];
16323
16324 if (buf != NULL && varname != NULL && varp != NULL)
16325 {
16326 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328
16329 if (*varname == '&')
16330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016331 long numval;
16332 char_u *strval;
16333 int error = FALSE;
16334
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016336 numval = get_tv_number_chk(varp, &error);
16337 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016338 if (!error && strval != NULL)
16339 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 }
16341 else
16342 {
16343 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16344 if (bufvarname != NULL)
16345 {
16346 STRCPY(bufvarname, "b:");
16347 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016348 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 vim_free(bufvarname);
16350 }
16351 }
16352
16353 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356}
16357
16358/*
16359 * "setcmdpos()" function
16360 */
16361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016363 typval_T *argvars;
16364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 int pos = (int)get_tv_number(&argvars[0]) - 1;
16367
16368 if (pos >= 0)
16369 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016370}
16371
16372/*
16373 * "setline()" function
16374 */
16375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016376f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *argvars;
16378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379{
16380 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016381 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016382 list_T *l = NULL;
16383 listitem_T *li = NULL;
16384 long added = 0;
16385 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016387 lnum = get_tv_lnum(&argvars[0]);
16388 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016390 l = argvars[1].vval.v_list;
16391 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016393 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016395
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016396 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016397 for (;;)
16398 {
16399 if (l != NULL)
16400 {
16401 /* list argument, get next string */
16402 if (li == NULL)
16403 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016405 li = li->li_next;
16406 }
16407
16408 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016410 break;
16411 if (lnum <= curbuf->b_ml.ml_line_count)
16412 {
16413 /* existing line, replace it */
16414 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16415 {
16416 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016417 if (lnum == curwin->w_cursor.lnum)
16418 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016419 rettv->vval.v_number = 0; /* OK */
16420 }
16421 }
16422 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16423 {
16424 /* lnum is one past the last line, append the line */
16425 ++added;
16426 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16427 rettv->vval.v_number = 0; /* OK */
16428 }
16429
16430 if (l == NULL) /* only one string argument */
16431 break;
16432 ++lnum;
16433 }
16434
16435 if (added > 0)
16436 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437}
16438
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016439static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16440
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016442 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016443 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016444 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016445set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016446 win_T *wp UNUSED;
16447 typval_T *list_arg UNUSED;
16448 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016449 typval_T *rettv;
16450{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016451#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016452 char_u *act;
16453 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016454#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016455
Bram Moolenaar2641f772005-03-25 21:58:17 +000016456 rettv->vval.v_number = -1;
16457
16458#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016459 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016460 EMSG(_(e_listreq));
16461 else
16462 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016463 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016464
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016465 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016466 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016467 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016468 if (act == NULL)
16469 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016470 if (*act == 'a' || *act == 'r')
16471 action = *act;
16472 }
16473
Bram Moolenaar81484f42012-12-05 15:16:47 +010016474 if (l != NULL && set_errorlist(wp, l, action,
16475 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016476 rettv->vval.v_number = 0;
16477 }
16478#endif
16479}
16480
16481/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016482 * "setloclist()" function
16483 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016484 static void
16485f_setloclist(argvars, rettv)
16486 typval_T *argvars;
16487 typval_T *rettv;
16488{
16489 win_T *win;
16490
16491 rettv->vval.v_number = -1;
16492
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016493 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016494 if (win != NULL)
16495 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16496}
16497
16498/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016499 * "setmatches()" function
16500 */
16501 static void
16502f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016503 typval_T *argvars UNUSED;
16504 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016505{
16506#ifdef FEAT_SEARCH_EXTRA
16507 list_T *l;
16508 listitem_T *li;
16509 dict_T *d;
16510
16511 rettv->vval.v_number = -1;
16512 if (argvars[0].v_type != VAR_LIST)
16513 {
16514 EMSG(_(e_listreq));
16515 return;
16516 }
16517 if ((l = argvars[0].vval.v_list) != NULL)
16518 {
16519
16520 /* To some extent make sure that we are dealing with a list from
16521 * "getmatches()". */
16522 li = l->lv_first;
16523 while (li != NULL)
16524 {
16525 if (li->li_tv.v_type != VAR_DICT
16526 || (d = li->li_tv.vval.v_dict) == NULL)
16527 {
16528 EMSG(_(e_invarg));
16529 return;
16530 }
16531 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16532 && dict_find(d, (char_u *)"pattern", -1) != NULL
16533 && dict_find(d, (char_u *)"priority", -1) != NULL
16534 && dict_find(d, (char_u *)"id", -1) != NULL))
16535 {
16536 EMSG(_(e_invarg));
16537 return;
16538 }
16539 li = li->li_next;
16540 }
16541
16542 clear_matches(curwin);
16543 li = l->lv_first;
16544 while (li != NULL)
16545 {
16546 d = li->li_tv.vval.v_dict;
16547 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16548 get_dict_string(d, (char_u *)"pattern", FALSE),
16549 (int)get_dict_number(d, (char_u *)"priority"),
16550 (int)get_dict_number(d, (char_u *)"id"));
16551 li = li->li_next;
16552 }
16553 rettv->vval.v_number = 0;
16554 }
16555#endif
16556}
16557
16558/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016559 * "setpos()" function
16560 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016561 static void
16562f_setpos(argvars, rettv)
16563 typval_T *argvars;
16564 typval_T *rettv;
16565{
16566 pos_T pos;
16567 int fnum;
16568 char_u *name;
16569
Bram Moolenaar08250432008-02-13 11:42:46 +000016570 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016571 name = get_tv_string_chk(argvars);
16572 if (name != NULL)
16573 {
16574 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16575 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016576 if (--pos.col < 0)
16577 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016578 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016579 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016580 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016581 if (fnum == curbuf->b_fnum)
16582 {
16583 curwin->w_cursor = pos;
16584 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016585 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016586 }
16587 else
16588 EMSG(_(e_invarg));
16589 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016590 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16591 {
16592 /* set mark */
16593 if (setmark_pos(name[1], &pos, fnum) == OK)
16594 rettv->vval.v_number = 0;
16595 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016596 else
16597 EMSG(_(e_invarg));
16598 }
16599 }
16600}
16601
16602/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016603 * "setqflist()" function
16604 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016605 static void
16606f_setqflist(argvars, rettv)
16607 typval_T *argvars;
16608 typval_T *rettv;
16609{
16610 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16611}
16612
16613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016614 * "setreg()" function
16615 */
16616 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016617f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 typval_T *argvars;
16619 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016620{
16621 int regname;
16622 char_u *strregname;
16623 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016624 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 int append;
16626 char_u yank_type;
16627 long block_len;
16628
16629 block_len = -1;
16630 yank_type = MAUTO;
16631 append = FALSE;
16632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016633 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016636 if (strregname == NULL)
16637 return; /* type error; errmsg already given */
16638 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016639 if (regname == 0 || regname == '@')
16640 regname = '"';
16641 else if (regname == '=')
16642 return;
16643
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016646 stropt = get_tv_string_chk(&argvars[2]);
16647 if (stropt == NULL)
16648 return; /* type error */
16649 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 switch (*stropt)
16651 {
16652 case 'a': case 'A': /* append */
16653 append = TRUE;
16654 break;
16655 case 'v': case 'c': /* character-wise selection */
16656 yank_type = MCHAR;
16657 break;
16658 case 'V': case 'l': /* line-wise selection */
16659 yank_type = MLINE;
16660 break;
16661#ifdef FEAT_VISUAL
16662 case 'b': case Ctrl_V: /* block-wise selection */
16663 yank_type = MBLOCK;
16664 if (VIM_ISDIGIT(stropt[1]))
16665 {
16666 ++stropt;
16667 block_len = getdigits(&stropt) - 1;
16668 --stropt;
16669 }
16670 break;
16671#endif
16672 }
16673 }
16674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016675 strval = get_tv_string_chk(&argvars[1]);
16676 if (strval != NULL)
16677 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016678 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680}
16681
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016682/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016683 * "settabvar()" function
16684 */
16685 static void
16686f_settabvar(argvars, rettv)
16687 typval_T *argvars;
16688 typval_T *rettv;
16689{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016690#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016691 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016692 tabpage_T *tp;
16693#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016694 char_u *varname, *tabvarname;
16695 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016696
16697 rettv->vval.v_number = 0;
16698
16699 if (check_restricted() || check_secure())
16700 return;
16701
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016702#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016703 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016704#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016705 varname = get_tv_string_chk(&argvars[1]);
16706 varp = &argvars[2];
16707
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016708 if (varname != NULL && varp != NULL
16709#ifdef FEAT_WINDOWS
16710 && tp != NULL
16711#endif
16712 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016713 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016714#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016715 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016716 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016717#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016718
16719 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16720 if (tabvarname != NULL)
16721 {
16722 STRCPY(tabvarname, "t:");
16723 STRCPY(tabvarname + 2, varname);
16724 set_var(tabvarname, varp, TRUE);
16725 vim_free(tabvarname);
16726 }
16727
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016728#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016729 /* Restore current tabpage */
16730 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016731 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016732#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016733 }
16734}
16735
16736/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016737 * "settabwinvar()" function
16738 */
16739 static void
16740f_settabwinvar(argvars, rettv)
16741 typval_T *argvars;
16742 typval_T *rettv;
16743{
16744 setwinvar(argvars, rettv, 1);
16745}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
16747/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016748 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016752 typval_T *argvars;
16753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016755 setwinvar(argvars, rettv, 0);
16756}
16757
16758/*
16759 * "setwinvar()" and "settabwinvar()" functions
16760 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016761
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016762 static void
16763setwinvar(argvars, rettv, off)
16764 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016765 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016766 int off;
16767{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 win_T *win;
16769#ifdef FEAT_WINDOWS
16770 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016771 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772#endif
16773 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016776 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777
16778 if (check_restricted() || check_secure())
16779 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016780
16781#ifdef FEAT_WINDOWS
16782 if (off == 1)
16783 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16784 else
16785 tp = curtab;
16786#endif
16787 win = find_win_by_nr(&argvars[off], tp);
16788 varname = get_tv_string_chk(&argvars[off + 1]);
16789 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790
16791 if (win != NULL && varname != NULL && varp != NULL)
16792 {
16793#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016794 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016795 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796#endif
16797
16798 if (*varname == '&')
16799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016800 long numval;
16801 char_u *strval;
16802 int error = FALSE;
16803
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016805 numval = get_tv_number_chk(varp, &error);
16806 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016807 if (!error && strval != NULL)
16808 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 }
16810 else
16811 {
16812 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16813 if (winvarname != NULL)
16814 {
16815 STRCPY(winvarname, "w:");
16816 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016817 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016818 vim_free(winvarname);
16819 }
16820 }
16821
16822#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016823 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824#endif
16825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826}
16827
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016828#ifdef FEAT_CRYPT
16829/*
16830 * "sha256({string})" function
16831 */
16832 static void
16833f_sha256(argvars, rettv)
16834 typval_T *argvars;
16835 typval_T *rettv;
16836{
16837 char_u *p;
16838
16839 p = get_tv_string(&argvars[0]);
16840 rettv->vval.v_string = vim_strsave(
16841 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16842 rettv->v_type = VAR_STRING;
16843}
16844#endif /* FEAT_CRYPT */
16845
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016847 * "shellescape({string})" function
16848 */
16849 static void
16850f_shellescape(argvars, rettv)
16851 typval_T *argvars;
16852 typval_T *rettv;
16853{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016854 rettv->vval.v_string = vim_strsave_shellescape(
16855 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016856 rettv->v_type = VAR_STRING;
16857}
16858
16859/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016860 * shiftwidth() function
16861 */
16862 static void
16863f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016864 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016865 typval_T *rettv;
16866{
16867 rettv->vval.v_number = get_sw_value();
16868}
16869
16870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016871 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 */
16873 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016875 typval_T *argvars;
16876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880 p = get_tv_string(&argvars[0]);
16881 rettv->vval.v_string = vim_strsave(p);
16882 simplify_filename(rettv->vval.v_string); /* simplify in place */
16883 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884}
16885
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016886#ifdef FEAT_FLOAT
16887/*
16888 * "sin()" function
16889 */
16890 static void
16891f_sin(argvars, rettv)
16892 typval_T *argvars;
16893 typval_T *rettv;
16894{
16895 float_T f;
16896
16897 rettv->v_type = VAR_FLOAT;
16898 if (get_float_arg(argvars, &f) == OK)
16899 rettv->vval.v_float = sin(f);
16900 else
16901 rettv->vval.v_float = 0.0;
16902}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016903
16904/*
16905 * "sinh()" function
16906 */
16907 static void
16908f_sinh(argvars, rettv)
16909 typval_T *argvars;
16910 typval_T *rettv;
16911{
16912 float_T f;
16913
16914 rettv->v_type = VAR_FLOAT;
16915 if (get_float_arg(argvars, &f) == OK)
16916 rettv->vval.v_float = sinh(f);
16917 else
16918 rettv->vval.v_float = 0.0;
16919}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016920#endif
16921
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922static int
16923#ifdef __BORLANDC__
16924 _RTLENTRYF
16925#endif
16926 item_compare __ARGS((const void *s1, const void *s2));
16927static int
16928#ifdef __BORLANDC__
16929 _RTLENTRYF
16930#endif
16931 item_compare2 __ARGS((const void *s1, const void *s2));
16932
16933static int item_compare_ic;
16934static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016935static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016936static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937#define ITEM_COMPARE_FAIL 999
16938
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942 static int
16943#ifdef __BORLANDC__
16944_RTLENTRYF
16945#endif
16946item_compare(s1, s2)
16947 const void *s1;
16948 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 char_u *p1, *p2;
16951 char_u *tofree1, *tofree2;
16952 int res;
16953 char_u numbuf1[NUMBUFLEN];
16954 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016956 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16957 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016958 if (p1 == NULL)
16959 p1 = (char_u *)"";
16960 if (p2 == NULL)
16961 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 if (item_compare_ic)
16963 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 res = STRCMP(p1, p2);
16966 vim_free(tofree1);
16967 vim_free(tofree2);
16968 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969}
16970
16971 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972#ifdef __BORLANDC__
16973_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975item_compare2(s1, s2)
16976 const void *s1;
16977 const void *s2;
16978{
16979 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016981 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 /* shortcut after failure in previous call; compare all items equal */
16985 if (item_compare_func_err)
16986 return 0;
16987
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016988 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16989 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016990 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16991 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992
16993 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016994 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016995 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16996 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 clear_tv(&argv[0]);
16998 clear_tv(&argv[1]);
16999
17000 if (res == FAIL)
17001 res = ITEM_COMPARE_FAIL;
17002 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17004 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017005 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 clear_tv(&rettv);
17007 return res;
17008}
17009
17010/*
17011 * "sort({list})" function
17012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017013 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 typval_T *argvars;
17016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017{
Bram Moolenaar33570922005-01-25 22:26:29 +000017018 list_T *l;
17019 listitem_T *li;
17020 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 long len;
17022 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 if (argvars[0].v_type != VAR_LIST)
17025 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 else
17027 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017029 if (l == NULL || tv_check_lock(l->lv_lock,
17030 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 return;
17032 rettv->vval.v_list = l;
17033 rettv->v_type = VAR_LIST;
17034 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036 len = list_len(l);
17037 if (len <= 1)
17038 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 item_compare_ic = FALSE;
17041 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017042 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017043 if (argvars[1].v_type != VAR_UNKNOWN)
17044 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017045 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048 else
17049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017050 int error = FALSE;
17051
17052 i = get_tv_number_chk(&argvars[1], &error);
17053 if (error)
17054 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 if (i == 1)
17056 item_compare_ic = TRUE;
17057 else
17058 item_compare_func = get_tv_string(&argvars[1]);
17059 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017060
17061 if (argvars[2].v_type != VAR_UNKNOWN)
17062 {
17063 /* optional third argument: {dict} */
17064 if (argvars[2].v_type != VAR_DICT)
17065 {
17066 EMSG(_(e_dictreq));
17067 return;
17068 }
17069 item_compare_selfdict = argvars[2].vval.v_dict;
17070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017074 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 if (ptrs == NULL)
17076 return;
17077 i = 0;
17078 for (li = l->lv_first; li != NULL; li = li->li_next)
17079 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017081 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 /* test the compare function */
17083 if (item_compare_func != NULL
17084 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17085 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017086 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088 {
17089 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 item_compare_func == NULL ? item_compare : item_compare2);
17092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017093 if (!item_compare_func_err)
17094 {
17095 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017096 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 l->lv_len = 0;
17098 for (i = 0; i < len; ++i)
17099 list_append(l, ptrs[i]);
17100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 }
17102
17103 vim_free(ptrs);
17104 }
17105}
17106
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017107/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017108 * "soundfold({word})" function
17109 */
17110 static void
17111f_soundfold(argvars, rettv)
17112 typval_T *argvars;
17113 typval_T *rettv;
17114{
17115 char_u *s;
17116
17117 rettv->v_type = VAR_STRING;
17118 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017119#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017120 rettv->vval.v_string = eval_soundfold(s);
17121#else
17122 rettv->vval.v_string = vim_strsave(s);
17123#endif
17124}
17125
17126/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017127 * "spellbadword()" function
17128 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017129 static void
17130f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017131 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017132 typval_T *rettv;
17133{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017134 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017135 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017136 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017137
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017138 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017139 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017140
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017141#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017142 if (argvars[0].v_type == VAR_UNKNOWN)
17143 {
17144 /* Find the start and length of the badly spelled word. */
17145 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17146 if (len != 0)
17147 word = ml_get_cursor();
17148 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017149 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017150 {
17151 char_u *str = get_tv_string_chk(&argvars[0]);
17152 int capcol = -1;
17153
17154 if (str != NULL)
17155 {
17156 /* Check the argument for spelling. */
17157 while (*str != NUL)
17158 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017159 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017160 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017161 {
17162 word = str;
17163 break;
17164 }
17165 str += len;
17166 }
17167 }
17168 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017169#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017170
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017171 list_append_string(rettv->vval.v_list, word, len);
17172 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017173 attr == HLF_SPB ? "bad" :
17174 attr == HLF_SPR ? "rare" :
17175 attr == HLF_SPL ? "local" :
17176 attr == HLF_SPC ? "caps" :
17177 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017178}
17179
17180/*
17181 * "spellsuggest()" function
17182 */
17183 static void
17184f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017185 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017186 typval_T *rettv;
17187{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017188#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017189 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017190 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017191 int maxcount;
17192 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017193 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017194 listitem_T *li;
17195 int need_capital = FALSE;
17196#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017198 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017199 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017200
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017201#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017202 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017203 {
17204 str = get_tv_string(&argvars[0]);
17205 if (argvars[1].v_type != VAR_UNKNOWN)
17206 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017207 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017208 if (maxcount <= 0)
17209 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017210 if (argvars[2].v_type != VAR_UNKNOWN)
17211 {
17212 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17213 if (typeerr)
17214 return;
17215 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017216 }
17217 else
17218 maxcount = 25;
17219
Bram Moolenaar4770d092006-01-12 23:22:24 +000017220 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017221
17222 for (i = 0; i < ga.ga_len; ++i)
17223 {
17224 str = ((char_u **)ga.ga_data)[i];
17225
17226 li = listitem_alloc();
17227 if (li == NULL)
17228 vim_free(str);
17229 else
17230 {
17231 li->li_tv.v_type = VAR_STRING;
17232 li->li_tv.v_lock = 0;
17233 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017234 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017235 }
17236 }
17237 ga_clear(&ga);
17238 }
17239#endif
17240}
17241
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017243f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 typval_T *argvars;
17245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017246{
17247 char_u *str;
17248 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017249 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017250 regmatch_T regmatch;
17251 char_u patbuf[NUMBUFLEN];
17252 char_u *save_cpo;
17253 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017254 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017255 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017256 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257
17258 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17259 save_cpo = p_cpo;
17260 p_cpo = (char_u *)"";
17261
17262 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017263 if (argvars[1].v_type != VAR_UNKNOWN)
17264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017265 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17266 if (pat == NULL)
17267 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017268 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017269 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017270 }
17271 if (pat == NULL || *pat == NUL)
17272 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017273
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017274 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017276 if (typeerr)
17277 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278
Bram Moolenaar0d660222005-01-07 21:51:51 +000017279 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17280 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017283 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017285 if (*str == NUL)
17286 match = FALSE; /* empty item at the end */
17287 else
17288 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017289 if (match)
17290 end = regmatch.startp[0];
17291 else
17292 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017293 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17294 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017296 if (list_append_string(rettv->vval.v_list, str,
17297 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 }
17300 if (!match)
17301 break;
17302 /* Advance to just after the match. */
17303 if (regmatch.endp[0] > str)
17304 col = 0;
17305 else
17306 {
17307 /* Don't get stuck at the same match. */
17308#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017309 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310#else
17311 col = 1;
17312#endif
17313 }
17314 str = regmatch.endp[0];
17315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316
Bram Moolenaar473de612013-06-08 18:19:48 +020017317 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321}
17322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017323#ifdef FEAT_FLOAT
17324/*
17325 * "sqrt()" function
17326 */
17327 static void
17328f_sqrt(argvars, rettv)
17329 typval_T *argvars;
17330 typval_T *rettv;
17331{
17332 float_T f;
17333
17334 rettv->v_type = VAR_FLOAT;
17335 if (get_float_arg(argvars, &f) == OK)
17336 rettv->vval.v_float = sqrt(f);
17337 else
17338 rettv->vval.v_float = 0.0;
17339}
17340
17341/*
17342 * "str2float()" function
17343 */
17344 static void
17345f_str2float(argvars, rettv)
17346 typval_T *argvars;
17347 typval_T *rettv;
17348{
17349 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17350
17351 if (*p == '+')
17352 p = skipwhite(p + 1);
17353 (void)string2float(p, &rettv->vval.v_float);
17354 rettv->v_type = VAR_FLOAT;
17355}
17356#endif
17357
Bram Moolenaar2c932302006-03-18 21:42:09 +000017358/*
17359 * "str2nr()" function
17360 */
17361 static void
17362f_str2nr(argvars, rettv)
17363 typval_T *argvars;
17364 typval_T *rettv;
17365{
17366 int base = 10;
17367 char_u *p;
17368 long n;
17369
17370 if (argvars[1].v_type != VAR_UNKNOWN)
17371 {
17372 base = get_tv_number(&argvars[1]);
17373 if (base != 8 && base != 10 && base != 16)
17374 {
17375 EMSG(_(e_invarg));
17376 return;
17377 }
17378 }
17379
17380 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017381 if (*p == '+')
17382 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017383 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17384 rettv->vval.v_number = n;
17385}
17386
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387#ifdef HAVE_STRFTIME
17388/*
17389 * "strftime({format}[, {time}])" function
17390 */
17391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017392f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017393 typval_T *argvars;
17394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395{
17396 char_u result_buf[256];
17397 struct tm *curtime;
17398 time_t seconds;
17399 char_u *p;
17400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017404 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 seconds = time(NULL);
17406 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017407 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 curtime = localtime(&seconds);
17409 /* MSVC returns NULL for an invalid value of seconds. */
17410 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 else
17413 {
17414# ifdef FEAT_MBYTE
17415 vimconv_T conv;
17416 char_u *enc;
17417
17418 conv.vc_type = CONV_NONE;
17419 enc = enc_locale();
17420 convert_setup(&conv, p_enc, enc);
17421 if (conv.vc_type != CONV_NONE)
17422 p = string_convert(&conv, p, NULL);
17423# endif
17424 if (p != NULL)
17425 (void)strftime((char *)result_buf, sizeof(result_buf),
17426 (char *)p, curtime);
17427 else
17428 result_buf[0] = NUL;
17429
17430# ifdef FEAT_MBYTE
17431 if (conv.vc_type != CONV_NONE)
17432 vim_free(p);
17433 convert_setup(&conv, enc, p_enc);
17434 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 else
17437# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017438 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439
17440# ifdef FEAT_MBYTE
17441 /* Release conversion descriptors */
17442 convert_setup(&conv, NULL, NULL);
17443 vim_free(enc);
17444# endif
17445 }
17446}
17447#endif
17448
17449/*
17450 * "stridx()" function
17451 */
17452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017454 typval_T *argvars;
17455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456{
17457 char_u buf[NUMBUFLEN];
17458 char_u *needle;
17459 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017460 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017464 needle = get_tv_string_chk(&argvars[1]);
17465 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017466 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017467 if (needle == NULL || haystack == NULL)
17468 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469
Bram Moolenaar33570922005-01-25 22:26:29 +000017470 if (argvars[2].v_type != VAR_UNKNOWN)
17471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017472 int error = FALSE;
17473
17474 start_idx = get_tv_number_chk(&argvars[2], &error);
17475 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017476 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017477 if (start_idx >= 0)
17478 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 }
17480
17481 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17482 if (pos != NULL)
17483 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484}
17485
17486/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017487 * "string()" function
17488 */
17489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017491 typval_T *argvars;
17492 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017493{
17494 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017495 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017498 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017499 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017500 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502}
17503
17504/*
17505 * "strlen()" function
17506 */
17507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T *argvars;
17510 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->vval.v_number = (varnumber_T)(STRLEN(
17513 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514}
17515
17516/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017517 * "strchars()" function
17518 */
17519 static void
17520f_strchars(argvars, rettv)
17521 typval_T *argvars;
17522 typval_T *rettv;
17523{
17524 char_u *s = get_tv_string(&argvars[0]);
17525#ifdef FEAT_MBYTE
17526 varnumber_T len = 0;
17527
17528 while (*s != NUL)
17529 {
17530 mb_cptr2char_adv(&s);
17531 ++len;
17532 }
17533 rettv->vval.v_number = len;
17534#else
17535 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17536#endif
17537}
17538
17539/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017540 * "strdisplaywidth()" function
17541 */
17542 static void
17543f_strdisplaywidth(argvars, rettv)
17544 typval_T *argvars;
17545 typval_T *rettv;
17546{
17547 char_u *s = get_tv_string(&argvars[0]);
17548 int col = 0;
17549
17550 if (argvars[1].v_type != VAR_UNKNOWN)
17551 col = get_tv_number(&argvars[1]);
17552
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017553 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017554}
17555
17556/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017557 * "strwidth()" function
17558 */
17559 static void
17560f_strwidth(argvars, rettv)
17561 typval_T *argvars;
17562 typval_T *rettv;
17563{
17564 char_u *s = get_tv_string(&argvars[0]);
17565
17566 rettv->vval.v_number = (varnumber_T)(
17567#ifdef FEAT_MBYTE
17568 mb_string2cells(s, -1)
17569#else
17570 STRLEN(s)
17571#endif
17572 );
17573}
17574
17575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 * "strpart()" function
17577 */
17578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
17583 char_u *p;
17584 int n;
17585 int len;
17586 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017587 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 slen = (int)STRLEN(p);
17591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017592 n = get_tv_number_chk(&argvars[1], &error);
17593 if (error)
17594 len = 0;
17595 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 else
17598 len = slen - n; /* default len: all bytes that are available. */
17599
17600 /*
17601 * Only return the overlap between the specified part and the actual
17602 * string.
17603 */
17604 if (n < 0)
17605 {
17606 len += n;
17607 n = 0;
17608 }
17609 else if (n > slen)
17610 n = slen;
17611 if (len < 0)
17612 len = 0;
17613 else if (n + len > slen)
17614 len = slen - n;
17615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017616 rettv->v_type = VAR_STRING;
17617 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618}
17619
17620/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 * "strridx()" function
17622 */
17623 static void
17624f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017625 typval_T *argvars;
17626 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017627{
17628 char_u buf[NUMBUFLEN];
17629 char_u *needle;
17630 char_u *haystack;
17631 char_u *rest;
17632 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017633 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017635 needle = get_tv_string_chk(&argvars[1]);
17636 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017637
17638 rettv->vval.v_number = -1;
17639 if (needle == NULL || haystack == NULL)
17640 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017641
17642 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017643 if (argvars[2].v_type != VAR_UNKNOWN)
17644 {
17645 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017647 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017648 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017649 }
17650 else
17651 end_idx = haystack_len;
17652
Bram Moolenaar0d660222005-01-07 21:51:51 +000017653 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017654 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017655 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017656 lastmatch = haystack + end_idx;
17657 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017658 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017659 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017660 for (rest = haystack; *rest != '\0'; ++rest)
17661 {
17662 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017663 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017664 break;
17665 lastmatch = rest;
17666 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017667 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668
17669 if (lastmatch == NULL)
17670 rettv->vval.v_number = -1;
17671 else
17672 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17673}
17674
17675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 * "strtrans()" function
17677 */
17678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017680 typval_T *argvars;
17681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017683 rettv->v_type = VAR_STRING;
17684 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685}
17686
17687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688 * "submatch()" function
17689 */
17690 static void
17691f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017692 typval_T *argvars;
17693 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694{
17695 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017696 rettv->vval.v_string =
17697 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698}
17699
17700/*
17701 * "substitute()" function
17702 */
17703 static void
17704f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017705 typval_T *argvars;
17706 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707{
17708 char_u patbuf[NUMBUFLEN];
17709 char_u subbuf[NUMBUFLEN];
17710 char_u flagsbuf[NUMBUFLEN];
17711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 char_u *str = get_tv_string_chk(&argvars[0]);
17713 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17714 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17715 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17716
Bram Moolenaar0d660222005-01-07 21:51:51 +000017717 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017718 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17719 rettv->vval.v_string = NULL;
17720 else
17721 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017722}
17723
17724/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017725 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017728f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731{
17732 int id = 0;
17733#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017734 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 long col;
17736 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017737 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 lnum = get_tv_lnum(argvars); /* -1 on type error */
17740 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17741 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017743 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017744 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017745 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746#endif
17747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017748 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749}
17750
17751/*
17752 * "synIDattr(id, what [, mode])" function
17753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017755f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
17759 char_u *p = NULL;
17760#ifdef FEAT_SYN_HL
17761 int id;
17762 char_u *what;
17763 char_u *mode;
17764 char_u modebuf[NUMBUFLEN];
17765 int modec;
17766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017767 id = get_tv_number(&argvars[0]);
17768 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017769 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017773 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 modec = 0; /* replace invalid with current */
17775 }
17776 else
17777 {
17778#ifdef FEAT_GUI
17779 if (gui.in_use)
17780 modec = 'g';
17781 else
17782#endif
17783 if (t_colors > 1)
17784 modec = 'c';
17785 else
17786 modec = 't';
17787 }
17788
17789
17790 switch (TOLOWER_ASC(what[0]))
17791 {
17792 case 'b':
17793 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17794 p = highlight_color(id, what, modec);
17795 else /* bold */
17796 p = highlight_has_attr(id, HL_BOLD, modec);
17797 break;
17798
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017799 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 p = highlight_color(id, what, modec);
17801 break;
17802
17803 case 'i':
17804 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17805 p = highlight_has_attr(id, HL_INVERSE, modec);
17806 else /* italic */
17807 p = highlight_has_attr(id, HL_ITALIC, modec);
17808 break;
17809
17810 case 'n': /* name */
17811 p = get_highlight_name(NULL, id - 1);
17812 break;
17813
17814 case 'r': /* reverse */
17815 p = highlight_has_attr(id, HL_INVERSE, modec);
17816 break;
17817
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017818 case 's':
17819 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17820 p = highlight_color(id, what, modec);
17821 else /* standout */
17822 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 break;
17824
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017825 case 'u':
17826 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17827 /* underline */
17828 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17829 else
17830 /* undercurl */
17831 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 break;
17833 }
17834
17835 if (p != NULL)
17836 p = vim_strsave(p);
17837#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 rettv->v_type = VAR_STRING;
17839 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840}
17841
17842/*
17843 * "synIDtrans(id)" function
17844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017846f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849{
17850 int id;
17851
17852#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854
17855 if (id > 0)
17856 id = syn_get_final_id(id);
17857 else
17858#endif
17859 id = 0;
17860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017861 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862}
17863
17864/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017865 * "synconcealed(lnum, col)" function
17866 */
17867 static void
17868f_synconcealed(argvars, rettv)
17869 typval_T *argvars UNUSED;
17870 typval_T *rettv;
17871{
17872#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17873 long lnum;
17874 long col;
17875 int syntax_flags = 0;
17876 int cchar;
17877 int matchid = 0;
17878 char_u str[NUMBUFLEN];
17879#endif
17880
17881 rettv->v_type = VAR_LIST;
17882 rettv->vval.v_list = NULL;
17883
17884#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17885 lnum = get_tv_lnum(argvars); /* -1 on type error */
17886 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17887
17888 vim_memset(str, NUL, sizeof(str));
17889
17890 if (rettv_list_alloc(rettv) != FAIL)
17891 {
17892 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17893 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17894 && curwin->w_p_cole > 0)
17895 {
17896 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17897 syntax_flags = get_syntax_info(&matchid);
17898
17899 /* get the conceal character */
17900 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17901 {
17902 cchar = syn_get_sub_char();
17903 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17904 cchar = lcs_conceal;
17905 if (cchar != NUL)
17906 {
17907# ifdef FEAT_MBYTE
17908 if (has_mbyte)
17909 (*mb_char2bytes)(cchar, str);
17910 else
17911# endif
17912 str[0] = cchar;
17913 }
17914 }
17915 }
17916
17917 list_append_number(rettv->vval.v_list,
17918 (syntax_flags & HL_CONCEAL) != 0);
17919 /* -1 to auto-determine strlen */
17920 list_append_string(rettv->vval.v_list, str, -1);
17921 list_append_number(rettv->vval.v_list, matchid);
17922 }
17923#endif
17924}
17925
17926/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017927 * "synstack(lnum, col)" function
17928 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017929 static void
17930f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017932 typval_T *rettv;
17933{
17934#ifdef FEAT_SYN_HL
17935 long lnum;
17936 long col;
17937 int i;
17938 int id;
17939#endif
17940
17941 rettv->v_type = VAR_LIST;
17942 rettv->vval.v_list = NULL;
17943
17944#ifdef FEAT_SYN_HL
17945 lnum = get_tv_lnum(argvars); /* -1 on type error */
17946 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17947
17948 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017949 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017950 && rettv_list_alloc(rettv) != FAIL)
17951 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017952 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017953 for (i = 0; ; ++i)
17954 {
17955 id = syn_get_stack_item(i);
17956 if (id < 0)
17957 break;
17958 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17959 break;
17960 }
17961 }
17962#endif
17963}
17964
17965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 * "system()" function
17967 */
17968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017969f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017970 typval_T *argvars;
17971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017973 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017975 char_u *infile = NULL;
17976 char_u buf[NUMBUFLEN];
17977 int err = FALSE;
17978 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017980 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017981 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017983 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017984 {
17985 /*
17986 * Write the string to a temp file, to be used for input of the shell
17987 * command.
17988 */
17989 if ((infile = vim_tempname('i')) == NULL)
17990 {
17991 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017992 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017993 }
17994
17995 fd = mch_fopen((char *)infile, WRITEBIN);
17996 if (fd == NULL)
17997 {
17998 EMSG2(_(e_notopen), infile);
17999 goto done;
18000 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018001 p = get_tv_string_buf_chk(&argvars[1], buf);
18002 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018003 {
18004 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018005 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018006 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018007 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18008 err = TRUE;
18009 if (fclose(fd) != 0)
18010 err = TRUE;
18011 if (err)
18012 {
18013 EMSG(_("E677: Error writing temp file"));
18014 goto done;
18015 }
18016 }
18017
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018018 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18019 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018020
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021#ifdef USE_CR
18022 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018023 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 {
18025 char_u *s;
18026
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018027 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 {
18029 if (*s == CAR)
18030 *s = NL;
18031 }
18032 }
18033#else
18034# ifdef USE_CRNL
18035 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018036 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037 {
18038 char_u *s, *d;
18039
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018040 d = res;
18041 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 {
18043 if (s[0] == CAR && s[1] == NL)
18044 ++s;
18045 *d++ = *s;
18046 }
18047 *d = NUL;
18048 }
18049# endif
18050#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018051
18052done:
18053 if (infile != NULL)
18054 {
18055 mch_remove(infile);
18056 vim_free(infile);
18057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058 rettv->v_type = VAR_STRING;
18059 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060}
18061
18062/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018063 * "tabpagebuflist()" function
18064 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018065 static void
18066f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018067 typval_T *argvars UNUSED;
18068 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018069{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018070#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018071 tabpage_T *tp;
18072 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018073
18074 if (argvars[0].v_type == VAR_UNKNOWN)
18075 wp = firstwin;
18076 else
18077 {
18078 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18079 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018080 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018081 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018082 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018083 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018084 for (; wp != NULL; wp = wp->w_next)
18085 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018086 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018087 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018088 }
18089#endif
18090}
18091
18092
18093/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018094 * "tabpagenr()" function
18095 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018096 static void
18097f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018098 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018099 typval_T *rettv;
18100{
18101 int nr = 1;
18102#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018103 char_u *arg;
18104
18105 if (argvars[0].v_type != VAR_UNKNOWN)
18106 {
18107 arg = get_tv_string_chk(&argvars[0]);
18108 nr = 0;
18109 if (arg != NULL)
18110 {
18111 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018112 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018113 else
18114 EMSG2(_(e_invexpr2), arg);
18115 }
18116 }
18117 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018118 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018119#endif
18120 rettv->vval.v_number = nr;
18121}
18122
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018123
18124#ifdef FEAT_WINDOWS
18125static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18126
18127/*
18128 * Common code for tabpagewinnr() and winnr().
18129 */
18130 static int
18131get_winnr(tp, argvar)
18132 tabpage_T *tp;
18133 typval_T *argvar;
18134{
18135 win_T *twin;
18136 int nr = 1;
18137 win_T *wp;
18138 char_u *arg;
18139
18140 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18141 if (argvar->v_type != VAR_UNKNOWN)
18142 {
18143 arg = get_tv_string_chk(argvar);
18144 if (arg == NULL)
18145 nr = 0; /* type error; errmsg already given */
18146 else if (STRCMP(arg, "$") == 0)
18147 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18148 else if (STRCMP(arg, "#") == 0)
18149 {
18150 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18151 if (twin == NULL)
18152 nr = 0;
18153 }
18154 else
18155 {
18156 EMSG2(_(e_invexpr2), arg);
18157 nr = 0;
18158 }
18159 }
18160
18161 if (nr > 0)
18162 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18163 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018164 {
18165 if (wp == NULL)
18166 {
18167 /* didn't find it in this tabpage */
18168 nr = 0;
18169 break;
18170 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018171 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018172 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018173 return nr;
18174}
18175#endif
18176
18177/*
18178 * "tabpagewinnr()" function
18179 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018180 static void
18181f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018182 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018183 typval_T *rettv;
18184{
18185 int nr = 1;
18186#ifdef FEAT_WINDOWS
18187 tabpage_T *tp;
18188
18189 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18190 if (tp == NULL)
18191 nr = 0;
18192 else
18193 nr = get_winnr(tp, &argvars[1]);
18194#endif
18195 rettv->vval.v_number = nr;
18196}
18197
18198
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018199/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018200 * "tagfiles()" function
18201 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018202 static void
18203f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018204 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018205 typval_T *rettv;
18206{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018207 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018208 tagname_T tn;
18209 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018210
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018211 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018212 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018213 fname = alloc(MAXPATHL);
18214 if (fname == NULL)
18215 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018216
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018217 for (first = TRUE; ; first = FALSE)
18218 if (get_tagfname(&tn, first, fname) == FAIL
18219 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018220 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018221 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018222 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018223}
18224
18225/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018226 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018227 */
18228 static void
18229f_taglist(argvars, rettv)
18230 typval_T *argvars;
18231 typval_T *rettv;
18232{
18233 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018234
18235 tag_pattern = get_tv_string(&argvars[0]);
18236
18237 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018238 if (*tag_pattern == NUL)
18239 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018241 if (rettv_list_alloc(rettv) == OK)
18242 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018243}
18244
18245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 * "tempname()" function
18247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018249f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252{
18253 static int x = 'A';
18254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018255 rettv->v_type = VAR_STRING;
18256 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257
18258 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18259 * names. Skip 'I' and 'O', they are used for shell redirection. */
18260 do
18261 {
18262 if (x == 'Z')
18263 x = '0';
18264 else if (x == '9')
18265 x = 'A';
18266 else
18267 {
18268#ifdef EBCDIC
18269 if (x == 'I')
18270 x = 'J';
18271 else if (x == 'R')
18272 x = 'S';
18273 else
18274#endif
18275 ++x;
18276 }
18277 } while (x == 'I' || x == 'O');
18278}
18279
18280/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018281 * "test(list)" function: Just checking the walls...
18282 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018283 static void
18284f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018285 typval_T *argvars UNUSED;
18286 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018287{
18288 /* Used for unit testing. Change the code below to your liking. */
18289#if 0
18290 listitem_T *li;
18291 list_T *l;
18292 char_u *bad, *good;
18293
18294 if (argvars[0].v_type != VAR_LIST)
18295 return;
18296 l = argvars[0].vval.v_list;
18297 if (l == NULL)
18298 return;
18299 li = l->lv_first;
18300 if (li == NULL)
18301 return;
18302 bad = get_tv_string(&li->li_tv);
18303 li = li->li_next;
18304 if (li == NULL)
18305 return;
18306 good = get_tv_string(&li->li_tv);
18307 rettv->vval.v_number = test_edit_score(bad, good);
18308#endif
18309}
18310
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018311#ifdef FEAT_FLOAT
18312/*
18313 * "tan()" function
18314 */
18315 static void
18316f_tan(argvars, rettv)
18317 typval_T *argvars;
18318 typval_T *rettv;
18319{
18320 float_T f;
18321
18322 rettv->v_type = VAR_FLOAT;
18323 if (get_float_arg(argvars, &f) == OK)
18324 rettv->vval.v_float = tan(f);
18325 else
18326 rettv->vval.v_float = 0.0;
18327}
18328
18329/*
18330 * "tanh()" function
18331 */
18332 static void
18333f_tanh(argvars, rettv)
18334 typval_T *argvars;
18335 typval_T *rettv;
18336{
18337 float_T f;
18338
18339 rettv->v_type = VAR_FLOAT;
18340 if (get_float_arg(argvars, &f) == OK)
18341 rettv->vval.v_float = tanh(f);
18342 else
18343 rettv->vval.v_float = 0.0;
18344}
18345#endif
18346
Bram Moolenaard52d9742005-08-21 22:20:28 +000018347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 * "tolower(string)" function
18349 */
18350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018351f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018352 typval_T *argvars;
18353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354{
18355 char_u *p;
18356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018357 p = vim_strsave(get_tv_string(&argvars[0]));
18358 rettv->v_type = VAR_STRING;
18359 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360
18361 if (p != NULL)
18362 while (*p != NUL)
18363 {
18364#ifdef FEAT_MBYTE
18365 int l;
18366
18367 if (enc_utf8)
18368 {
18369 int c, lc;
18370
18371 c = utf_ptr2char(p);
18372 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018373 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 /* TODO: reallocate string when byte count changes. */
18375 if (utf_char2len(lc) == l)
18376 utf_char2bytes(lc, p);
18377 p += l;
18378 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018379 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 p += l; /* skip multi-byte character */
18381 else
18382#endif
18383 {
18384 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18385 ++p;
18386 }
18387 }
18388}
18389
18390/*
18391 * "toupper(string)" function
18392 */
18393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018394f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018395 typval_T *argvars;
18396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018398 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018399 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400}
18401
18402/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018403 * "tr(string, fromstr, tostr)" function
18404 */
18405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018406f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018407 typval_T *argvars;
18408 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018409{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018410 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018411 char_u *fromstr;
18412 char_u *tostr;
18413 char_u *p;
18414#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018415 int inlen;
18416 int fromlen;
18417 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018418 int idx;
18419 char_u *cpstr;
18420 int cplen;
18421 int first = TRUE;
18422#endif
18423 char_u buf[NUMBUFLEN];
18424 char_u buf2[NUMBUFLEN];
18425 garray_T ga;
18426
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018427 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018428 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18429 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018430
18431 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 rettv->v_type = VAR_STRING;
18433 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018434 if (fromstr == NULL || tostr == NULL)
18435 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018436 ga_init2(&ga, (int)sizeof(char), 80);
18437
18438#ifdef FEAT_MBYTE
18439 if (!has_mbyte)
18440#endif
18441 /* not multi-byte: fromstr and tostr must be the same length */
18442 if (STRLEN(fromstr) != STRLEN(tostr))
18443 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018444#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018445error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018446#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018447 EMSG2(_(e_invarg2), fromstr);
18448 ga_clear(&ga);
18449 return;
18450 }
18451
18452 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018453 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018454 {
18455#ifdef FEAT_MBYTE
18456 if (has_mbyte)
18457 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018458 inlen = (*mb_ptr2len)(in_str);
18459 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018460 cplen = inlen;
18461 idx = 0;
18462 for (p = fromstr; *p != NUL; p += fromlen)
18463 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018464 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018465 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018466 {
18467 for (p = tostr; *p != NUL; p += tolen)
18468 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018469 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018470 if (idx-- == 0)
18471 {
18472 cplen = tolen;
18473 cpstr = p;
18474 break;
18475 }
18476 }
18477 if (*p == NUL) /* tostr is shorter than fromstr */
18478 goto error;
18479 break;
18480 }
18481 ++idx;
18482 }
18483
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018484 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018485 {
18486 /* Check that fromstr and tostr have the same number of
18487 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018488 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018489 first = FALSE;
18490 for (p = tostr; *p != NUL; p += tolen)
18491 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018492 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018493 --idx;
18494 }
18495 if (idx != 0)
18496 goto error;
18497 }
18498
18499 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018500 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018501 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018502
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018503 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018504 }
18505 else
18506#endif
18507 {
18508 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018509 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018510 if (p != NULL)
18511 ga_append(&ga, tostr[p - fromstr]);
18512 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018513 ga_append(&ga, *in_str);
18514 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018515 }
18516 }
18517
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018518 /* add a terminating NUL */
18519 ga_grow(&ga, 1);
18520 ga_append(&ga, NUL);
18521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018522 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018523}
18524
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018525#ifdef FEAT_FLOAT
18526/*
18527 * "trunc({float})" function
18528 */
18529 static void
18530f_trunc(argvars, rettv)
18531 typval_T *argvars;
18532 typval_T *rettv;
18533{
18534 float_T f;
18535
18536 rettv->v_type = VAR_FLOAT;
18537 if (get_float_arg(argvars, &f) == OK)
18538 /* trunc() is not in C90, use floor() or ceil() instead. */
18539 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18540 else
18541 rettv->vval.v_float = 0.0;
18542}
18543#endif
18544
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546 * "type(expr)" function
18547 */
18548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018549f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018550 typval_T *argvars;
18551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018553 int n;
18554
18555 switch (argvars[0].v_type)
18556 {
18557 case VAR_NUMBER: n = 0; break;
18558 case VAR_STRING: n = 1; break;
18559 case VAR_FUNC: n = 2; break;
18560 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018561 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018562#ifdef FEAT_FLOAT
18563 case VAR_FLOAT: n = 5; break;
18564#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018565 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18566 }
18567 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568}
18569
18570/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018571 * "undofile(name)" function
18572 */
18573 static void
18574f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018575 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018576 typval_T *rettv;
18577{
18578 rettv->v_type = VAR_STRING;
18579#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018580 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018581 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018582
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018583 if (*fname == NUL)
18584 {
18585 /* If there is no file name there will be no undo file. */
18586 rettv->vval.v_string = NULL;
18587 }
18588 else
18589 {
18590 char_u *ffname = FullName_save(fname, FALSE);
18591
18592 if (ffname != NULL)
18593 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18594 vim_free(ffname);
18595 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018596 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018597#else
18598 rettv->vval.v_string = NULL;
18599#endif
18600}
18601
18602/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018603 * "undotree()" function
18604 */
18605 static void
18606f_undotree(argvars, rettv)
18607 typval_T *argvars UNUSED;
18608 typval_T *rettv;
18609{
18610 if (rettv_dict_alloc(rettv) == OK)
18611 {
18612 dict_T *dict = rettv->vval.v_dict;
18613 list_T *list;
18614
Bram Moolenaar730cde92010-06-27 05:18:54 +020018615 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018616 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018617 dict_add_nr_str(dict, "save_last",
18618 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018619 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18620 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018621 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018622
18623 list = list_alloc();
18624 if (list != NULL)
18625 {
18626 u_eval_tree(curbuf->b_u_oldhead, list);
18627 dict_add_list(dict, "entries", list);
18628 }
18629 }
18630}
18631
18632/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018633 * "values(dict)" function
18634 */
18635 static void
18636f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *argvars;
18638 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018639{
18640 dict_list(argvars, rettv, 1);
18641}
18642
18643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 * "virtcol(string)" function
18645 */
18646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018648 typval_T *argvars;
18649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650{
18651 colnr_T vcol = 0;
18652 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018653 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018655 fp = var2fpos(&argvars[0], FALSE, &fnum);
18656 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18657 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 {
18659 getvvcol(curwin, fp, NULL, NULL, &vcol);
18660 ++vcol;
18661 }
18662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664}
18665
18666/*
18667 * "visualmode()" function
18668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018670f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018671 typval_T *argvars UNUSED;
18672 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673{
18674#ifdef FEAT_VISUAL
18675 char_u str[2];
18676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018677 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 str[0] = curbuf->b_visual_mode_eval;
18679 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681
18682 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018683 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685#endif
18686}
18687
18688/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018689 * "wildmenumode()" function
18690 */
18691 static void
18692f_wildmenumode(argvars, rettv)
18693 typval_T *argvars UNUSED;
18694 typval_T *rettv UNUSED;
18695{
18696#ifdef FEAT_WILDMENU
18697 if (wild_menu_showing)
18698 rettv->vval.v_number = 1;
18699#endif
18700}
18701
18702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 * "winbufnr(nr)" function
18704 */
18705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 typval_T *argvars;
18708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709{
18710 win_T *wp;
18711
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018712 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018716 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717}
18718
18719/*
18720 * "wincol()" function
18721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726{
18727 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729}
18730
18731/*
18732 * "winheight(nr)" function
18733 */
18734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018735f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018736 typval_T *argvars;
18737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738{
18739 win_T *wp;
18740
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018741 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018743 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746}
18747
18748/*
18749 * "winline()" function
18750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018753 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
18756 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758}
18759
18760/*
18761 * "winnr()" function
18762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018765 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767{
18768 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018769
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018771 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018773 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774}
18775
18776/*
18777 * "winrestcmd()" function
18778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018781 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783{
18784#ifdef FEAT_WINDOWS
18785 win_T *wp;
18786 int winnr = 1;
18787 garray_T ga;
18788 char_u buf[50];
18789
18790 ga_init2(&ga, (int)sizeof(char), 70);
18791 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18792 {
18793 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18794 ga_concat(&ga, buf);
18795# ifdef FEAT_VERTSPLIT
18796 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18797 ga_concat(&ga, buf);
18798# endif
18799 ++winnr;
18800 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018801 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808}
18809
18810/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018811 * "winrestview()" function
18812 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018813 static void
18814f_winrestview(argvars, rettv)
18815 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018816 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018817{
18818 dict_T *dict;
18819
18820 if (argvars[0].v_type != VAR_DICT
18821 || (dict = argvars[0].vval.v_dict) == NULL)
18822 EMSG(_(e_invarg));
18823 else
18824 {
18825 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18826 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18827#ifdef FEAT_VIRTUALEDIT
18828 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18829#endif
18830 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018831 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018832
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018833 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018834#ifdef FEAT_DIFF
18835 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18836#endif
18837 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18838 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18839
18840 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018841 win_new_height(curwin, curwin->w_height);
18842# ifdef FEAT_VERTSPLIT
18843 win_new_width(curwin, W_WIDTH(curwin));
18844# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018845 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018846
18847 if (curwin->w_topline == 0)
18848 curwin->w_topline = 1;
18849 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18850 curwin->w_topline = curbuf->b_ml.ml_line_count;
18851#ifdef FEAT_DIFF
18852 check_topfill(curwin, TRUE);
18853#endif
18854 }
18855}
18856
18857/*
18858 * "winsaveview()" function
18859 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018860 static void
18861f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018862 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018863 typval_T *rettv;
18864{
18865 dict_T *dict;
18866
Bram Moolenaara800b422010-06-27 01:15:55 +020018867 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018868 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018869 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018870
18871 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18872 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18873#ifdef FEAT_VIRTUALEDIT
18874 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18875#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018876 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018877 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18878
18879 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18880#ifdef FEAT_DIFF
18881 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18882#endif
18883 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18884 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18885}
18886
18887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 * "winwidth(nr)" function
18889 */
18890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018892 typval_T *argvars;
18893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894{
18895 win_T *wp;
18896
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018897 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900 else
18901#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018902 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018904 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905#endif
18906}
18907
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018909 * "writefile()" function
18910 */
18911 static void
18912f_writefile(argvars, rettv)
18913 typval_T *argvars;
18914 typval_T *rettv;
18915{
18916 int binary = FALSE;
18917 char_u *fname;
18918 FILE *fd;
18919 listitem_T *li;
18920 char_u *s;
18921 int ret = 0;
18922 int c;
18923
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018924 if (check_restricted() || check_secure())
18925 return;
18926
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018927 if (argvars[0].v_type != VAR_LIST)
18928 {
18929 EMSG2(_(e_listarg), "writefile()");
18930 return;
18931 }
18932 if (argvars[0].vval.v_list == NULL)
18933 return;
18934
18935 if (argvars[2].v_type != VAR_UNKNOWN
18936 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18937 binary = TRUE;
18938
18939 /* Always open the file in binary mode, library functions have a mind of
18940 * their own about CR-LF conversion. */
18941 fname = get_tv_string(&argvars[1]);
18942 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18943 {
18944 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18945 ret = -1;
18946 }
18947 else
18948 {
18949 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18950 li = li->li_next)
18951 {
18952 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18953 {
18954 if (*s == '\n')
18955 c = putc(NUL, fd);
18956 else
18957 c = putc(*s, fd);
18958 if (c == EOF)
18959 {
18960 ret = -1;
18961 break;
18962 }
18963 }
18964 if (!binary || li->li_next != NULL)
18965 if (putc('\n', fd) == EOF)
18966 {
18967 ret = -1;
18968 break;
18969 }
18970 if (ret < 0)
18971 {
18972 EMSG(_(e_write));
18973 break;
18974 }
18975 }
18976 fclose(fd);
18977 }
18978
18979 rettv->vval.v_number = ret;
18980}
18981
18982/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018983 * "xor(expr, expr)" function
18984 */
18985 static void
18986f_xor(argvars, rettv)
18987 typval_T *argvars;
18988 typval_T *rettv;
18989{
18990 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18991 ^ get_tv_number_chk(&argvars[1], NULL);
18992}
18993
18994
18995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018997 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 */
18999 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019000var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019001 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019002 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019003 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019005 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019007 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008
Bram Moolenaara5525202006-03-02 22:52:09 +000019009 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019010 if (varp->v_type == VAR_LIST)
19011 {
19012 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019013 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019014 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019015 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019016
19017 l = varp->vval.v_list;
19018 if (l == NULL)
19019 return NULL;
19020
19021 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019022 pos.lnum = list_find_nr(l, 0L, &error);
19023 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019024 return NULL; /* invalid line number */
19025
19026 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019027 pos.col = list_find_nr(l, 1L, &error);
19028 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019029 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019030 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019031
19032 /* We accept "$" for the column number: last column. */
19033 li = list_find(l, 1L);
19034 if (li != NULL && li->li_tv.v_type == VAR_STRING
19035 && li->li_tv.vval.v_string != NULL
19036 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19037 pos.col = len + 1;
19038
Bram Moolenaara5525202006-03-02 22:52:09 +000019039 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019040 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019041 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019042 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019043
Bram Moolenaara5525202006-03-02 22:52:09 +000019044#ifdef FEAT_VIRTUALEDIT
19045 /* Get the virtual offset. Defaults to zero. */
19046 pos.coladd = list_find_nr(l, 2L, &error);
19047 if (error)
19048 pos.coladd = 0;
19049#endif
19050
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019051 return &pos;
19052 }
19053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019054 name = get_tv_string_chk(varp);
19055 if (name == NULL)
19056 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019057 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019059#ifdef FEAT_VISUAL
19060 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19061 {
19062 if (VIsual_active)
19063 return &VIsual;
19064 return &curwin->w_cursor;
19065 }
19066#endif
19067 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019069 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19071 return NULL;
19072 return pp;
19073 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019074
19075#ifdef FEAT_VIRTUALEDIT
19076 pos.coladd = 0;
19077#endif
19078
Bram Moolenaar477933c2007-07-17 14:32:23 +000019079 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019080 {
19081 pos.col = 0;
19082 if (name[1] == '0') /* "w0": first visible line */
19083 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019084 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019085 pos.lnum = curwin->w_topline;
19086 return &pos;
19087 }
19088 else if (name[1] == '$') /* "w$": last visible line */
19089 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019090 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019091 pos.lnum = curwin->w_botline - 1;
19092 return &pos;
19093 }
19094 }
19095 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019097 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 {
19099 pos.lnum = curbuf->b_ml.ml_line_count;
19100 pos.col = 0;
19101 }
19102 else
19103 {
19104 pos.lnum = curwin->w_cursor.lnum;
19105 pos.col = (colnr_T)STRLEN(ml_get_curline());
19106 }
19107 return &pos;
19108 }
19109 return NULL;
19110}
19111
19112/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019113 * Convert list in "arg" into a position and optional file number.
19114 * When "fnump" is NULL there is no file number, only 3 items.
19115 * Note that the column is passed on as-is, the caller may want to decrement
19116 * it to use 1 for the first column.
19117 * Return FAIL when conversion is not possible, doesn't check the position for
19118 * validity.
19119 */
19120 static int
19121list2fpos(arg, posp, fnump)
19122 typval_T *arg;
19123 pos_T *posp;
19124 int *fnump;
19125{
19126 list_T *l = arg->vval.v_list;
19127 long i = 0;
19128 long n;
19129
Bram Moolenaarbde35262006-07-23 20:12:24 +000019130 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19131 * when "fnump" isn't NULL and "coladd" is optional. */
19132 if (arg->v_type != VAR_LIST
19133 || l == NULL
19134 || l->lv_len < (fnump == NULL ? 2 : 3)
19135 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019136 return FAIL;
19137
19138 if (fnump != NULL)
19139 {
19140 n = list_find_nr(l, i++, NULL); /* fnum */
19141 if (n < 0)
19142 return FAIL;
19143 if (n == 0)
19144 n = curbuf->b_fnum; /* current buffer */
19145 *fnump = n;
19146 }
19147
19148 n = list_find_nr(l, i++, NULL); /* lnum */
19149 if (n < 0)
19150 return FAIL;
19151 posp->lnum = n;
19152
19153 n = list_find_nr(l, i++, NULL); /* col */
19154 if (n < 0)
19155 return FAIL;
19156 posp->col = n;
19157
19158#ifdef FEAT_VIRTUALEDIT
19159 n = list_find_nr(l, i, NULL);
19160 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019161 posp->coladd = 0;
19162 else
19163 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019164#endif
19165
19166 return OK;
19167}
19168
19169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170 * Get the length of an environment variable name.
19171 * Advance "arg" to the first character after the name.
19172 * Return 0 for error.
19173 */
19174 static int
19175get_env_len(arg)
19176 char_u **arg;
19177{
19178 char_u *p;
19179 int len;
19180
19181 for (p = *arg; vim_isIDc(*p); ++p)
19182 ;
19183 if (p == *arg) /* no name found */
19184 return 0;
19185
19186 len = (int)(p - *arg);
19187 *arg = p;
19188 return len;
19189}
19190
19191/*
19192 * Get the length of the name of a function or internal variable.
19193 * "arg" is advanced to the first non-white character after the name.
19194 * Return 0 if something is wrong.
19195 */
19196 static int
19197get_id_len(arg)
19198 char_u **arg;
19199{
19200 char_u *p;
19201 int len;
19202
19203 /* Find the end of the name. */
19204 for (p = *arg; eval_isnamec(*p); ++p)
19205 ;
19206 if (p == *arg) /* no name found */
19207 return 0;
19208
19209 len = (int)(p - *arg);
19210 *arg = skipwhite(p);
19211
19212 return len;
19213}
19214
19215/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019216 * Get the length of the name of a variable or function.
19217 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019219 * Return -1 if curly braces expansion failed.
19220 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 * If the name contains 'magic' {}'s, expand them and return the
19222 * expanded name in an allocated string via 'alias' - caller must free.
19223 */
19224 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019225get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 char_u **arg;
19227 char_u **alias;
19228 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019229 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230{
19231 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232 char_u *p;
19233 char_u *expr_start;
19234 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
19236 *alias = NULL; /* default to no alias */
19237
19238 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19239 && (*arg)[2] == (int)KE_SNR)
19240 {
19241 /* hard coded <SNR>, already translated */
19242 *arg += 3;
19243 return get_id_len(arg) + 3;
19244 }
19245 len = eval_fname_script(*arg);
19246 if (len > 0)
19247 {
19248 /* literal "<SID>", "s:" or "<SNR>" */
19249 *arg += len;
19250 }
19251
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019255 p = find_name_end(*arg, &expr_start, &expr_end,
19256 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257 if (expr_start != NULL)
19258 {
19259 char_u *temp_string;
19260
19261 if (!evaluate)
19262 {
19263 len += (int)(p - *arg);
19264 *arg = skipwhite(p);
19265 return len;
19266 }
19267
19268 /*
19269 * Include any <SID> etc in the expanded string:
19270 * Thus the -len here.
19271 */
19272 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19273 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019274 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 *alias = temp_string;
19276 *arg = skipwhite(p);
19277 return (int)STRLEN(temp_string);
19278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279
19280 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019281 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 EMSG2(_(e_invexpr2), *arg);
19283
19284 return len;
19285}
19286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287/*
19288 * Find the end of a variable or function name, taking care of magic braces.
19289 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19290 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019291 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019292 * Return a pointer to just after the name. Equal to "arg" if there is no
19293 * valid name.
19294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019296find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297 char_u *arg;
19298 char_u **expr_start;
19299 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019300 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019302 int mb_nest = 0;
19303 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304 char_u *p;
19305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019306 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308 *expr_start = NULL;
19309 *expr_end = NULL;
19310 }
19311
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019312 /* Quick check for valid starting character. */
19313 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19314 return arg;
19315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019316 for (p = arg; *p != NUL
19317 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019318 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019319 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019321 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019323 if (*p == '\'')
19324 {
19325 /* skip over 'string' to avoid counting [ and ] inside it. */
19326 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19327 ;
19328 if (*p == NUL)
19329 break;
19330 }
19331 else if (*p == '"')
19332 {
19333 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19334 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19335 if (*p == '\\' && p[1] != NUL)
19336 ++p;
19337 if (*p == NUL)
19338 break;
19339 }
19340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019341 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343 if (*p == '[')
19344 ++br_nest;
19345 else if (*p == ']')
19346 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019349 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 if (*p == '{')
19352 {
19353 mb_nest++;
19354 if (expr_start != NULL && *expr_start == NULL)
19355 *expr_start = p;
19356 }
19357 else if (*p == '}')
19358 {
19359 mb_nest--;
19360 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19361 *expr_end = p;
19362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 }
19365
19366 return p;
19367}
19368
19369/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019370 * Expands out the 'magic' {}'s in a variable/function name.
19371 * Note that this can call itself recursively, to deal with
19372 * constructs like foo{bar}{baz}{bam}
19373 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19374 * "in_start" ^
19375 * "expr_start" ^
19376 * "expr_end" ^
19377 * "in_end" ^
19378 *
19379 * Returns a new allocated string, which the caller must free.
19380 * Returns NULL for failure.
19381 */
19382 static char_u *
19383make_expanded_name(in_start, expr_start, expr_end, in_end)
19384 char_u *in_start;
19385 char_u *expr_start;
19386 char_u *expr_end;
19387 char_u *in_end;
19388{
19389 char_u c1;
19390 char_u *retval = NULL;
19391 char_u *temp_result;
19392 char_u *nextcmd = NULL;
19393
19394 if (expr_end == NULL || in_end == NULL)
19395 return NULL;
19396 *expr_start = NUL;
19397 *expr_end = NUL;
19398 c1 = *in_end;
19399 *in_end = NUL;
19400
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019401 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019402 if (temp_result != NULL && nextcmd == NULL)
19403 {
19404 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19405 + (in_end - expr_end) + 1));
19406 if (retval != NULL)
19407 {
19408 STRCPY(retval, in_start);
19409 STRCAT(retval, temp_result);
19410 STRCAT(retval, expr_end + 1);
19411 }
19412 }
19413 vim_free(temp_result);
19414
19415 *in_end = c1; /* put char back for error messages */
19416 *expr_start = '{';
19417 *expr_end = '}';
19418
19419 if (retval != NULL)
19420 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019421 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019422 if (expr_start != NULL)
19423 {
19424 /* Further expansion! */
19425 temp_result = make_expanded_name(retval, expr_start,
19426 expr_end, temp_result);
19427 vim_free(retval);
19428 retval = temp_result;
19429 }
19430 }
19431
19432 return retval;
19433}
19434
19435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019436 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019437 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438 */
19439 static int
19440eval_isnamec(c)
19441 int c;
19442{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019443 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19444}
19445
19446/*
19447 * Return TRUE if character "c" can be used as the first character in a
19448 * variable or function name (excluding '{' and '}').
19449 */
19450 static int
19451eval_isnamec1(c)
19452 int c;
19453{
19454 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455}
19456
19457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 * Set number v: variable to "val".
19459 */
19460 void
19461set_vim_var_nr(idx, val)
19462 int idx;
19463 long val;
19464{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019465 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466}
19467
19468/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019469 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 */
19471 long
19472get_vim_var_nr(idx)
19473 int idx;
19474{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019475 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476}
19477
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019478/*
19479 * Get string v: variable value. Uses a static buffer, can only be used once.
19480 */
19481 char_u *
19482get_vim_var_str(idx)
19483 int idx;
19484{
19485 return get_tv_string(&vimvars[idx].vv_tv);
19486}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019487
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019489 * Get List v: variable value. Caller must take care of reference count when
19490 * needed.
19491 */
19492 list_T *
19493get_vim_var_list(idx)
19494 int idx;
19495{
19496 return vimvars[idx].vv_list;
19497}
19498
19499/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019500 * Set v:char to character "c".
19501 */
19502 void
19503set_vim_var_char(c)
19504 int c;
19505{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019506 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019507
19508#ifdef FEAT_MBYTE
19509 if (has_mbyte)
19510 buf[(*mb_char2bytes)(c, buf)] = NUL;
19511 else
19512#endif
19513 {
19514 buf[0] = c;
19515 buf[1] = NUL;
19516 }
19517 set_vim_var_string(VV_CHAR, buf, -1);
19518}
19519
19520/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019521 * Set v:count to "count" and v:count1 to "count1".
19522 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 */
19524 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019525set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 long count;
19527 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019528 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019530 if (set_prevcount)
19531 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019532 vimvars[VV_COUNT].vv_nr = count;
19533 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534}
19535
19536/*
19537 * Set string v: variable to a copy of "val".
19538 */
19539 void
19540set_vim_var_string(idx, val, len)
19541 int idx;
19542 char_u *val;
19543 int len; /* length of "val" to use or -1 (whole string) */
19544{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019545 /* Need to do this (at least) once, since we can't initialize a union.
19546 * Will always be invoked when "v:progname" is set. */
19547 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19548
Bram Moolenaare9a41262005-01-15 22:18:47 +000019549 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019551 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019553 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019555 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556}
19557
19558/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019559 * Set List v: variable to "val".
19560 */
19561 void
19562set_vim_var_list(idx, val)
19563 int idx;
19564 list_T *val;
19565{
19566 list_unref(vimvars[idx].vv_list);
19567 vimvars[idx].vv_list = val;
19568 if (val != NULL)
19569 ++val->lv_refcount;
19570}
19571
19572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 * Set v:register if needed.
19574 */
19575 void
19576set_reg_var(c)
19577 int c;
19578{
19579 char_u regname;
19580
19581 if (c == 0 || c == ' ')
19582 regname = '"';
19583 else
19584 regname = c;
19585 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019586 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 set_vim_var_string(VV_REG, &regname, 1);
19588}
19589
19590/*
19591 * Get or set v:exception. If "oldval" == NULL, return the current value.
19592 * Otherwise, restore the value to "oldval" and return NULL.
19593 * Must always be called in pairs to save and restore v:exception! Does not
19594 * take care of memory allocations.
19595 */
19596 char_u *
19597v_exception(oldval)
19598 char_u *oldval;
19599{
19600 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019601 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602
Bram Moolenaare9a41262005-01-15 22:18:47 +000019603 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 return NULL;
19605}
19606
19607/*
19608 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19609 * Otherwise, restore the value to "oldval" and return NULL.
19610 * Must always be called in pairs to save and restore v:throwpoint! Does not
19611 * take care of memory allocations.
19612 */
19613 char_u *
19614v_throwpoint(oldval)
19615 char_u *oldval;
19616{
19617 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019618 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
Bram Moolenaare9a41262005-01-15 22:18:47 +000019620 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 return NULL;
19622}
19623
19624#if defined(FEAT_AUTOCMD) || defined(PROTO)
19625/*
19626 * Set v:cmdarg.
19627 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19628 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19629 * Must always be called in pairs!
19630 */
19631 char_u *
19632set_cmdarg(eap, oldarg)
19633 exarg_T *eap;
19634 char_u *oldarg;
19635{
19636 char_u *oldval;
19637 char_u *newval;
19638 unsigned len;
19639
Bram Moolenaare9a41262005-01-15 22:18:47 +000019640 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019641 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019643 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019644 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019645 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 }
19647
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019648 if (eap->force_bin == FORCE_BIN)
19649 len = 6;
19650 else if (eap->force_bin == FORCE_NOBIN)
19651 len = 8;
19652 else
19653 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019654
19655 if (eap->read_edit)
19656 len += 7;
19657
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019658 if (eap->force_ff != 0)
19659 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19660# ifdef FEAT_MBYTE
19661 if (eap->force_enc != 0)
19662 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019663 if (eap->bad_char != 0)
19664 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019665# endif
19666
19667 newval = alloc(len + 1);
19668 if (newval == NULL)
19669 return NULL;
19670
19671 if (eap->force_bin == FORCE_BIN)
19672 sprintf((char *)newval, " ++bin");
19673 else if (eap->force_bin == FORCE_NOBIN)
19674 sprintf((char *)newval, " ++nobin");
19675 else
19676 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019677
19678 if (eap->read_edit)
19679 STRCAT(newval, " ++edit");
19680
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019681 if (eap->force_ff != 0)
19682 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19683 eap->cmd + eap->force_ff);
19684# ifdef FEAT_MBYTE
19685 if (eap->force_enc != 0)
19686 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19687 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019688 if (eap->bad_char == BAD_KEEP)
19689 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19690 else if (eap->bad_char == BAD_DROP)
19691 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19692 else if (eap->bad_char != 0)
19693 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019694# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019695 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019696 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697}
19698#endif
19699
19700/*
19701 * Get the value of internal variable "name".
19702 * Return OK or FAIL.
19703 */
19704 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019705get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 char_u *name;
19707 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019709 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710{
19711 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 typval_T *tv = NULL;
19713 typval_T atv;
19714 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716
19717 /* truncate the name, so that we can use strcmp() */
19718 cc = name[len];
19719 name[len] = NUL;
19720
19721 /*
19722 * Check for "b:changedtick".
19723 */
19724 if (STRCMP(name, "b:changedtick") == 0)
19725 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 atv.v_type = VAR_NUMBER;
19727 atv.vval.v_number = curbuf->b_changedtick;
19728 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 }
19730
19731 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 * Check for user-defined variables.
19733 */
19734 else
19735 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019736 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019738 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 }
19740
Bram Moolenaare9a41262005-01-15 22:18:47 +000019741 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019743 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 ret = FAIL;
19746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019747 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019748 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749
19750 name[len] = cc;
19751
19752 return ret;
19753}
19754
19755/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019756 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19757 * Also handle function call with Funcref variable: func(expr)
19758 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19759 */
19760 static int
19761handle_subscript(arg, rettv, evaluate, verbose)
19762 char_u **arg;
19763 typval_T *rettv;
19764 int evaluate; /* do more than finding the end */
19765 int verbose; /* give error messages */
19766{
19767 int ret = OK;
19768 dict_T *selfdict = NULL;
19769 char_u *s;
19770 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019771 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019772
19773 while (ret == OK
19774 && (**arg == '['
19775 || (**arg == '.' && rettv->v_type == VAR_DICT)
19776 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19777 && !vim_iswhite(*(*arg - 1)))
19778 {
19779 if (**arg == '(')
19780 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019781 /* need to copy the funcref so that we can clear rettv */
19782 functv = *rettv;
19783 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019784
19785 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019786 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019787 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019788 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19789 &len, evaluate, selfdict);
19790
19791 /* Clear the funcref afterwards, so that deleting it while
19792 * evaluating the arguments is possible (see test55). */
19793 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019794
19795 /* Stop the expression evaluation when immediately aborting on
19796 * error, or when an interrupt occurred or an exception was thrown
19797 * but not caught. */
19798 if (aborting())
19799 {
19800 if (ret == OK)
19801 clear_tv(rettv);
19802 ret = FAIL;
19803 }
19804 dict_unref(selfdict);
19805 selfdict = NULL;
19806 }
19807 else /* **arg == '[' || **arg == '.' */
19808 {
19809 dict_unref(selfdict);
19810 if (rettv->v_type == VAR_DICT)
19811 {
19812 selfdict = rettv->vval.v_dict;
19813 if (selfdict != NULL)
19814 ++selfdict->dv_refcount;
19815 }
19816 else
19817 selfdict = NULL;
19818 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19819 {
19820 clear_tv(rettv);
19821 ret = FAIL;
19822 }
19823 }
19824 }
19825 dict_unref(selfdict);
19826 return ret;
19827}
19828
19829/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019830 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 * value).
19832 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019833 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019835{
Bram Moolenaar33570922005-01-25 22:26:29 +000019836 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837}
19838
19839/*
19840 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 * The string "s" must have been allocated, it is consumed.
19842 * Return NULL for out of memory, the variable otherwise.
19843 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019844 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 char_u *s;
19847{
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850 rettv = alloc_tv();
19851 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019853 rettv->v_type = VAR_STRING;
19854 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855 }
19856 else
19857 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019858 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859}
19860
19861/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019862 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019864 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019866 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867{
19868 if (varp != NULL)
19869 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019870 switch (varp->v_type)
19871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019872 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019873 func_unref(varp->vval.v_string);
19874 /*FALLTHROUGH*/
19875 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 vim_free(varp->vval.v_string);
19877 break;
19878 case VAR_LIST:
19879 list_unref(varp->vval.v_list);
19880 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019881 case VAR_DICT:
19882 dict_unref(varp->vval.v_dict);
19883 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019884 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885#ifdef FEAT_FLOAT
19886 case VAR_FLOAT:
19887#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019888 case VAR_UNKNOWN:
19889 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019891 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019892 break;
19893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 vim_free(varp);
19895 }
19896}
19897
19898/*
19899 * Free the memory for a variable value and set the value to NULL or 0.
19900 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019901 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019902clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019903 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904{
19905 if (varp != NULL)
19906 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019910 func_unref(varp->vval.v_string);
19911 /*FALLTHROUGH*/
19912 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019913 vim_free(varp->vval.v_string);
19914 varp->vval.v_string = NULL;
19915 break;
19916 case VAR_LIST:
19917 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019918 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019919 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019920 case VAR_DICT:
19921 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019922 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019923 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019925 varp->vval.v_number = 0;
19926 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019927#ifdef FEAT_FLOAT
19928 case VAR_FLOAT:
19929 varp->vval.v_float = 0.0;
19930 break;
19931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019932 case VAR_UNKNOWN:
19933 break;
19934 default:
19935 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019936 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019937 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 }
19939}
19940
19941/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 * Set the value of a variable to NULL without freeing items.
19943 */
19944 static void
19945init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019947{
19948 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019949 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950}
19951
19952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 * Get the number value of a variable.
19954 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019955 * For incompatible types, return 0.
19956 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19957 * caller of incompatible types: it sets *denote to TRUE if "denote"
19958 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 */
19960 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019961get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019962 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964 int error = FALSE;
19965
19966 return get_tv_number_chk(varp, &error); /* return 0L on error */
19967}
19968
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019969 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019970get_tv_number_chk(varp, denote)
19971 typval_T *varp;
19972 int *denote;
19973{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019974 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019978 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019979 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019980#ifdef FEAT_FLOAT
19981 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019982 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019983 break;
19984#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019986 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019987 break;
19988 case VAR_STRING:
19989 if (varp->vval.v_string != NULL)
19990 vim_str2nr(varp->vval.v_string, NULL, NULL,
19991 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019992 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019993 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019994 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019995 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019996 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019997 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019998 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019999 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020003 if (denote == NULL) /* useful for values that must be unsigned */
20004 n = -1;
20005 else
20006 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020007 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008}
20009
20010/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020011 * Get the lnum from the first argument.
20012 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020013 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 */
20015 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020017 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018{
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 linenr_T lnum;
20021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020022 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 if (lnum == 0) /* no valid number, try using line() */
20024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020025 rettv.v_type = VAR_NUMBER;
20026 f_line(argvars, &rettv);
20027 lnum = rettv.vval.v_number;
20028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 }
20030 return lnum;
20031}
20032
20033/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020034 * Get the lnum from the first argument.
20035 * Also accepts "$", then "buf" is used.
20036 * Returns 0 on error.
20037 */
20038 static linenr_T
20039get_tv_lnum_buf(argvars, buf)
20040 typval_T *argvars;
20041 buf_T *buf;
20042{
20043 if (argvars[0].v_type == VAR_STRING
20044 && argvars[0].vval.v_string != NULL
20045 && argvars[0].vval.v_string[0] == '$'
20046 && buf != NULL)
20047 return buf->b_ml.ml_line_count;
20048 return get_tv_number_chk(&argvars[0], NULL);
20049}
20050
20051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 * Get the string value of a variable.
20053 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020054 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20055 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 * If the String variable has never been set, return an empty string.
20057 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020058 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20059 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 */
20061 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020062get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064{
20065 static char_u mybuf[NUMBUFLEN];
20066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068}
20069
20070 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020072 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020073 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020075 char_u *res = get_tv_string_buf_chk(varp, buf);
20076
20077 return res != NULL ? res : (char_u *)"";
20078}
20079
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020080 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020081get_tv_string_chk(varp)
20082 typval_T *varp;
20083{
20084 static char_u mybuf[NUMBUFLEN];
20085
20086 return get_tv_string_buf_chk(varp, mybuf);
20087}
20088
20089 static char_u *
20090get_tv_string_buf_chk(varp, buf)
20091 typval_T *varp;
20092 char_u *buf;
20093{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020096 case VAR_NUMBER:
20097 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20098 return buf;
20099 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020100 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020101 break;
20102 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020103 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020104 break;
20105 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020106 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020107 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020108#ifdef FEAT_FLOAT
20109 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020110 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020111 break;
20112#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020113 case VAR_STRING:
20114 if (varp->vval.v_string != NULL)
20115 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020116 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020117 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020121 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122}
20123
20124/*
20125 * Find variable "name" in the list of variables.
20126 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020127 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020128 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020129 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020132find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020134 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020137 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138
Bram Moolenaara7043832005-01-21 11:56:39 +000020139 ht = find_var_ht(name, &varname);
20140 if (htp != NULL)
20141 *htp = ht;
20142 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020144 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145}
20146
20147/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020148 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020149 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020152find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020154 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020155 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020156 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020157{
Bram Moolenaar33570922005-01-25 22:26:29 +000020158 hashitem_T *hi;
20159
20160 if (*varname == NUL)
20161 {
20162 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020163 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020165 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 case 'g': return &globvars_var;
20167 case 'v': return &vimvars_var;
20168 case 'b': return &curbuf->b_bufvar;
20169 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020170#ifdef FEAT_WINDOWS
20171 case 't': return &curtab->tp_winvar;
20172#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020173 case 'l': return current_funccal == NULL
20174 ? NULL : &current_funccal->l_vars_var;
20175 case 'a': return current_funccal == NULL
20176 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020177 }
20178 return NULL;
20179 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020180
20181 hi = hash_find(ht, varname);
20182 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020183 {
20184 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020185 * worked find the variable again. Don't auto-load a script if it was
20186 * loaded already, otherwise it would be loaded every time when
20187 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020188 if (ht == &globvarht && !writing)
20189 {
20190 /* Note: script_autoload() may make "hi" invalid. It must either
20191 * be obtained again or not used. */
20192 if (!script_autoload(varname, FALSE) || aborting())
20193 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020194 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020195 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020196 if (HASHITEM_EMPTY(hi))
20197 return NULL;
20198 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020200}
20201
20202/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020203 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020204 * Set "varname" to the start of name without ':'.
20205 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020206 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020207find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208 char_u *name;
20209 char_u **varname;
20210{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020211 hashitem_T *hi;
20212
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 if (name[1] != ':')
20214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020215 /* The name must not start with a colon or #. */
20216 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 return NULL;
20218 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020219
20220 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020221 hi = hash_find(&compat_hashtab, name);
20222 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020223 return &compat_hashtab;
20224
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 return &globvarht; /* global variable */
20227 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 }
20229 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020230 if (*name == 'g') /* global variable */
20231 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020232 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20233 */
20234 if (vim_strchr(name + 2, ':') != NULL
20235 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020236 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020238 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020240 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020241#ifdef FEAT_WINDOWS
20242 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020243 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020244#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 if (*name == 'v') /* v: variable */
20246 return &vimvarht;
20247 if (*name == 'a' && current_funccal != NULL) /* function argument */
20248 return &current_funccal->l_avars.dv_hashtab;
20249 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20250 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 if (*name == 's' /* script variable */
20252 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20253 return &SCRIPT_VARS(current_SID);
20254 return NULL;
20255}
20256
20257/*
20258 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020259 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 * Returns NULL when it doesn't exist.
20261 */
20262 char_u *
20263get_var_value(name)
20264 char_u *name;
20265{
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267
Bram Moolenaara7043832005-01-21 11:56:39 +000020268 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269 if (v == NULL)
20270 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020271 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272}
20273
20274/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 * sourcing this script and when executing functions defined in the script.
20277 */
20278 void
20279new_script_vars(id)
20280 scid_T id;
20281{
Bram Moolenaara7043832005-01-21 11:56:39 +000020282 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 hashtab_T *ht;
20284 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020285
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20287 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020288 /* Re-allocating ga_data means that an ht_array pointing to
20289 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020291 for (i = 1; i <= ga_scripts.ga_len; ++i)
20292 {
20293 ht = &SCRIPT_VARS(i);
20294 if (ht->ht_mask == HT_INIT_SIZE - 1)
20295 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020296 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020297 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020298 }
20299
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 while (ga_scripts.ga_len < id)
20301 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020302 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020303 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020304 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 }
20307 }
20308}
20309
20310/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20312 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 */
20314 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020315init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020316 dict_T *dict;
20317 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020318 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319{
Bram Moolenaar33570922005-01-25 22:26:29 +000020320 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020321 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020322 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020323 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020324 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 dict_var->di_tv.vval.v_dict = dict;
20326 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020327 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020328 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20329 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330}
20331
20332/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020333 * Unreference a dictionary initialized by init_var_dict().
20334 */
20335 void
20336unref_var_dict(dict)
20337 dict_T *dict;
20338{
20339 /* Now the dict needs to be freed if no one else is using it, go back to
20340 * normal reference counting. */
20341 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20342 dict_unref(dict);
20343}
20344
20345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 * Frees all allocated variables and the value they contain.
20348 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 */
20350 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020351vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020352 hashtab_T *ht;
20353{
20354 vars_clear_ext(ht, TRUE);
20355}
20356
20357/*
20358 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20359 */
20360 static void
20361vars_clear_ext(ht, free_val)
20362 hashtab_T *ht;
20363 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364{
Bram Moolenaara7043832005-01-21 11:56:39 +000020365 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 hashitem_T *hi;
20367 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020370 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020371 for (hi = ht->ht_array; todo > 0; ++hi)
20372 {
20373 if (!HASHITEM_EMPTY(hi))
20374 {
20375 --todo;
20376
Bram Moolenaar33570922005-01-25 22:26:29 +000020377 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020378 * ht_array might change then. hash_clear() takes care of it
20379 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 v = HI2DI(hi);
20381 if (free_val)
20382 clear_tv(&v->di_tv);
20383 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20384 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020385 }
20386 }
20387 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020388 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389}
20390
Bram Moolenaara7043832005-01-21 11:56:39 +000020391/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020392 * Delete a variable from hashtab "ht" at item "hi".
20393 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020396delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020397 hashtab_T *ht;
20398 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399{
Bram Moolenaar33570922005-01-25 22:26:29 +000020400 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020401
20402 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 clear_tv(&di->di_tv);
20404 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405}
20406
20407/*
20408 * List the value of one internal variable.
20409 */
20410 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020411list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020414 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020416 char_u *tofree;
20417 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020419
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020420 current_copyID += COPYID_INC;
20421 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020422 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020423 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020424 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020428list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 char_u *prefix;
20430 char_u *name;
20431 int type;
20432 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020433 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434{
Bram Moolenaar31859182007-08-14 20:41:13 +000020435 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20436 msg_start();
20437 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 if (name != NULL) /* "a:" vars don't have a name stored */
20439 msg_puts(name);
20440 msg_putchar(' ');
20441 msg_advance(22);
20442 if (type == VAR_NUMBER)
20443 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020444 else if (type == VAR_FUNC)
20445 msg_putchar('*');
20446 else if (type == VAR_LIST)
20447 {
20448 msg_putchar('[');
20449 if (*string == '[')
20450 ++string;
20451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020452 else if (type == VAR_DICT)
20453 {
20454 msg_putchar('{');
20455 if (*string == '{')
20456 ++string;
20457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458 else
20459 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020460
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020462
20463 if (type == VAR_FUNC)
20464 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020465 if (*first)
20466 {
20467 msg_clr_eos();
20468 *first = FALSE;
20469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470}
20471
20472/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 * If the variable already exists, the value is updated.
20475 * Otherwise the variable is created.
20476 */
20477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020478set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020481 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482{
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020487 ht = find_var_ht(name, &varname);
20488 if (ht == NULL || *varname == NUL)
20489 {
20490 EMSG2(_(e_illvar), name);
20491 return;
20492 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020493 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020494
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020495 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20496 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020497
Bram Moolenaar33570922005-01-25 22:26:29 +000020498 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020501 if (var_check_ro(v->di_flags, name)
20502 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 return;
20504 if (v->di_tv.v_type != tv->v_type
20505 && !((v->di_tv.v_type == VAR_STRING
20506 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020507 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020508 || tv->v_type == VAR_NUMBER))
20509#ifdef FEAT_FLOAT
20510 && !((v->di_tv.v_type == VAR_NUMBER
20511 || v->di_tv.v_type == VAR_FLOAT)
20512 && (tv->v_type == VAR_NUMBER
20513 || tv->v_type == VAR_FLOAT))
20514#endif
20515 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020516 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020517 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020518 return;
20519 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020520
20521 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020522 * Handle setting internal v: variables separately: we don't change
20523 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020524 */
20525 if (ht == &vimvarht)
20526 {
20527 if (v->di_tv.v_type == VAR_STRING)
20528 {
20529 vim_free(v->di_tv.vval.v_string);
20530 if (copy || tv->v_type != VAR_STRING)
20531 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20532 else
20533 {
20534 /* Take over the string to avoid an extra alloc/free. */
20535 v->di_tv.vval.v_string = tv->vval.v_string;
20536 tv->vval.v_string = NULL;
20537 }
20538 }
20539 else if (v->di_tv.v_type != VAR_NUMBER)
20540 EMSG2(_(e_intern2), "set_var()");
20541 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020542 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020544 if (STRCMP(varname, "searchforward") == 0)
20545 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20546 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020547 return;
20548 }
20549
20550 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 }
20552 else /* add a new variable */
20553 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020554 /* Can't add "v:" variable. */
20555 if (ht == &vimvarht)
20556 {
20557 EMSG2(_(e_illvar), name);
20558 return;
20559 }
20560
Bram Moolenaar92124a32005-06-17 22:03:40 +000020561 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020562 if (!valid_varname(varname))
20563 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020564
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020565 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20566 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020567 if (v == NULL)
20568 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020569 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020570 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020572 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 return;
20574 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020575 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020577
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020578 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020579 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020580 else
20581 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020582 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020583 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020584 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586}
20587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020588/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020589 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 * Also give an error message.
20591 */
20592 static int
20593var_check_ro(flags, name)
20594 int flags;
20595 char_u *name;
20596{
20597 if (flags & DI_FLAGS_RO)
20598 {
20599 EMSG2(_(e_readonlyvar), name);
20600 return TRUE;
20601 }
20602 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20603 {
20604 EMSG2(_(e_readonlysbx), name);
20605 return TRUE;
20606 }
20607 return FALSE;
20608}
20609
20610/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020611 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20612 * Also give an error message.
20613 */
20614 static int
20615var_check_fixed(flags, name)
20616 int flags;
20617 char_u *name;
20618{
20619 if (flags & DI_FLAGS_FIX)
20620 {
20621 EMSG2(_("E795: Cannot delete variable %s"), name);
20622 return TRUE;
20623 }
20624 return FALSE;
20625}
20626
20627/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020628 * Check if a funcref is assigned to a valid variable name.
20629 * Return TRUE and give an error if not.
20630 */
20631 static int
20632var_check_func_name(name, new_var)
20633 char_u *name; /* points to start of variable name */
20634 int new_var; /* TRUE when creating the variable */
20635{
20636 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20637 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20638 ? name[2] : name[0]))
20639 {
20640 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20641 name);
20642 return TRUE;
20643 }
20644 /* Don't allow hiding a function. When "v" is not NULL we might be
20645 * assigning another function to the same var, the type is checked
20646 * below. */
20647 if (new_var && function_exists(name))
20648 {
20649 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20650 name);
20651 return TRUE;
20652 }
20653 return FALSE;
20654}
20655
20656/*
20657 * Check if a variable name is valid.
20658 * Return FALSE and give an error if not.
20659 */
20660 static int
20661valid_varname(varname)
20662 char_u *varname;
20663{
20664 char_u *p;
20665
20666 for (p = varname; *p != NUL; ++p)
20667 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20668 && *p != AUTOLOAD_CHAR)
20669 {
20670 EMSG2(_(e_illvar), varname);
20671 return FALSE;
20672 }
20673 return TRUE;
20674}
20675
20676/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020677 * Return TRUE if typeval "tv" is set to be locked (immutable).
20678 * Also give an error message, using "name".
20679 */
20680 static int
20681tv_check_lock(lock, name)
20682 int lock;
20683 char_u *name;
20684{
20685 if (lock & VAR_LOCKED)
20686 {
20687 EMSG2(_("E741: Value is locked: %s"),
20688 name == NULL ? (char_u *)_("Unknown") : name);
20689 return TRUE;
20690 }
20691 if (lock & VAR_FIXED)
20692 {
20693 EMSG2(_("E742: Cannot change value of %s"),
20694 name == NULL ? (char_u *)_("Unknown") : name);
20695 return TRUE;
20696 }
20697 return FALSE;
20698}
20699
20700/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020701 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020702 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020703 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020704 * It is OK for "from" and "to" to point to the same item. This is used to
20705 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020706 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020707 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020708copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020709 typval_T *from;
20710 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020712 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020713 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020714 switch (from->v_type)
20715 {
20716 case VAR_NUMBER:
20717 to->vval.v_number = from->vval.v_number;
20718 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020719#ifdef FEAT_FLOAT
20720 case VAR_FLOAT:
20721 to->vval.v_float = from->vval.v_float;
20722 break;
20723#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020724 case VAR_STRING:
20725 case VAR_FUNC:
20726 if (from->vval.v_string == NULL)
20727 to->vval.v_string = NULL;
20728 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020729 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020730 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020731 if (from->v_type == VAR_FUNC)
20732 func_ref(to->vval.v_string);
20733 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020734 break;
20735 case VAR_LIST:
20736 if (from->vval.v_list == NULL)
20737 to->vval.v_list = NULL;
20738 else
20739 {
20740 to->vval.v_list = from->vval.v_list;
20741 ++to->vval.v_list->lv_refcount;
20742 }
20743 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020744 case VAR_DICT:
20745 if (from->vval.v_dict == NULL)
20746 to->vval.v_dict = NULL;
20747 else
20748 {
20749 to->vval.v_dict = from->vval.v_dict;
20750 ++to->vval.v_dict->dv_refcount;
20751 }
20752 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020753 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020755 break;
20756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757}
20758
20759/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020760 * Make a copy of an item.
20761 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020762 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20763 * reference to an already copied list/dict can be used.
20764 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020765 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020766 static int
20767item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020768 typval_T *from;
20769 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020770 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020771 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020772{
20773 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020774 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020775
Bram Moolenaar33570922005-01-25 22:26:29 +000020776 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020777 {
20778 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020779 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020780 }
20781 ++recurse;
20782
20783 switch (from->v_type)
20784 {
20785 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020786#ifdef FEAT_FLOAT
20787 case VAR_FLOAT:
20788#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020789 case VAR_STRING:
20790 case VAR_FUNC:
20791 copy_tv(from, to);
20792 break;
20793 case VAR_LIST:
20794 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020795 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 if (from->vval.v_list == NULL)
20797 to->vval.v_list = NULL;
20798 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20799 {
20800 /* use the copy made earlier */
20801 to->vval.v_list = from->vval.v_list->lv_copylist;
20802 ++to->vval.v_list->lv_refcount;
20803 }
20804 else
20805 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20806 if (to->vval.v_list == NULL)
20807 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020808 break;
20809 case VAR_DICT:
20810 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020811 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020812 if (from->vval.v_dict == NULL)
20813 to->vval.v_dict = NULL;
20814 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20815 {
20816 /* use the copy made earlier */
20817 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20818 ++to->vval.v_dict->dv_refcount;
20819 }
20820 else
20821 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20822 if (to->vval.v_dict == NULL)
20823 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020824 break;
20825 default:
20826 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020827 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020828 }
20829 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020830 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020831}
20832
20833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834 * ":echo expr1 ..." print each argument separated with a space, add a
20835 * newline at the end.
20836 * ":echon expr1 ..." print each argument plain.
20837 */
20838 void
20839ex_echo(eap)
20840 exarg_T *eap;
20841{
20842 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020843 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845 char_u *p;
20846 int needclr = TRUE;
20847 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020848 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849
20850 if (eap->skip)
20851 ++emsg_skip;
20852 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20853 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020854 /* If eval1() causes an error message the text from the command may
20855 * still need to be cleared. E.g., "echo 22,44". */
20856 need_clr_eos = needclr;
20857
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020859 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 {
20861 /*
20862 * Report the invalid expression unless the expression evaluation
20863 * has been cancelled due to an aborting error, an interrupt, or an
20864 * exception.
20865 */
20866 if (!aborting())
20867 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020868 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 break;
20870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020871 need_clr_eos = FALSE;
20872
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 if (!eap->skip)
20874 {
20875 if (atstart)
20876 {
20877 atstart = FALSE;
20878 /* Call msg_start() after eval1(), evaluating the expression
20879 * may cause a message to appear. */
20880 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020881 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020882 /* Mark the saved text as finishing the line, so that what
20883 * follows is displayed on a new line when scrolling back
20884 * at the more prompt. */
20885 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 }
20889 else if (eap->cmdidx == CMD_echo)
20890 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020891 current_copyID += COPYID_INC;
20892 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020893 if (p != NULL)
20894 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020896 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020898 if (*p != TAB && needclr)
20899 {
20900 /* remove any text still there from the command */
20901 msg_clr_eos();
20902 needclr = FALSE;
20903 }
20904 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 }
20906 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020907 {
20908#ifdef FEAT_MBYTE
20909 if (has_mbyte)
20910 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020911 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020912
20913 (void)msg_outtrans_len_attr(p, i, echo_attr);
20914 p += i - 1;
20915 }
20916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020918 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020921 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020923 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 arg = skipwhite(arg);
20925 }
20926 eap->nextcmd = check_nextcmd(arg);
20927
20928 if (eap->skip)
20929 --emsg_skip;
20930 else
20931 {
20932 /* remove text that may still be there from the command */
20933 if (needclr)
20934 msg_clr_eos();
20935 if (eap->cmdidx == CMD_echo)
20936 msg_end();
20937 }
20938}
20939
20940/*
20941 * ":echohl {name}".
20942 */
20943 void
20944ex_echohl(eap)
20945 exarg_T *eap;
20946{
20947 int id;
20948
20949 id = syn_name2id(eap->arg);
20950 if (id == 0)
20951 echo_attr = 0;
20952 else
20953 echo_attr = syn_id2attr(id);
20954}
20955
20956/*
20957 * ":execute expr1 ..." execute the result of an expression.
20958 * ":echomsg expr1 ..." Print a message
20959 * ":echoerr expr1 ..." Print an error
20960 * Each gets spaces around each argument and a newline at the end for
20961 * echo commands
20962 */
20963 void
20964ex_execute(eap)
20965 exarg_T *eap;
20966{
20967 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020968 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 int ret = OK;
20970 char_u *p;
20971 garray_T ga;
20972 int len;
20973 int save_did_emsg;
20974
20975 ga_init2(&ga, 1, 80);
20976
20977 if (eap->skip)
20978 ++emsg_skip;
20979 while (*arg != NUL && *arg != '|' && *arg != '\n')
20980 {
20981 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020982 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 {
20984 /*
20985 * Report the invalid expression unless the expression evaluation
20986 * has been cancelled due to an aborting error, an interrupt, or an
20987 * exception.
20988 */
20989 if (!aborting())
20990 EMSG2(_(e_invexpr2), p);
20991 ret = FAIL;
20992 break;
20993 }
20994
20995 if (!eap->skip)
20996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020997 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 len = (int)STRLEN(p);
20999 if (ga_grow(&ga, len + 2) == FAIL)
21000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 ret = FAIL;
21003 break;
21004 }
21005 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 ga.ga_len += len;
21009 }
21010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021011 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 arg = skipwhite(arg);
21013 }
21014
21015 if (ret != FAIL && ga.ga_data != NULL)
21016 {
21017 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021020 out_flush();
21021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 else if (eap->cmdidx == CMD_echoerr)
21023 {
21024 /* We don't want to abort following commands, restore did_emsg. */
21025 save_did_emsg = did_emsg;
21026 EMSG((char_u *)ga.ga_data);
21027 if (!force_abort)
21028 did_emsg = save_did_emsg;
21029 }
21030 else if (eap->cmdidx == CMD_execute)
21031 do_cmdline((char_u *)ga.ga_data,
21032 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21033 }
21034
21035 ga_clear(&ga);
21036
21037 if (eap->skip)
21038 --emsg_skip;
21039
21040 eap->nextcmd = check_nextcmd(arg);
21041}
21042
21043/*
21044 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21045 * "arg" points to the "&" or '+' when called, to "option" when returning.
21046 * Returns NULL when no option name found. Otherwise pointer to the char
21047 * after the option name.
21048 */
21049 static char_u *
21050find_option_end(arg, opt_flags)
21051 char_u **arg;
21052 int *opt_flags;
21053{
21054 char_u *p = *arg;
21055
21056 ++p;
21057 if (*p == 'g' && p[1] == ':')
21058 {
21059 *opt_flags = OPT_GLOBAL;
21060 p += 2;
21061 }
21062 else if (*p == 'l' && p[1] == ':')
21063 {
21064 *opt_flags = OPT_LOCAL;
21065 p += 2;
21066 }
21067 else
21068 *opt_flags = 0;
21069
21070 if (!ASCII_ISALPHA(*p))
21071 return NULL;
21072 *arg = p;
21073
21074 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21075 p += 4; /* termcap option */
21076 else
21077 while (ASCII_ISALPHA(*p))
21078 ++p;
21079 return p;
21080}
21081
21082/*
21083 * ":function"
21084 */
21085 void
21086ex_function(eap)
21087 exarg_T *eap;
21088{
21089 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021090 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 int j;
21092 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 char_u *name = NULL;
21095 char_u *p;
21096 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021097 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 garray_T newargs;
21099 garray_T newlines;
21100 int varargs = FALSE;
21101 int mustend = FALSE;
21102 int flags = 0;
21103 ufunc_T *fp;
21104 int indent;
21105 int nesting;
21106 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021107 dictitem_T *v;
21108 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021109 static int func_nr = 0; /* number for nameless function */
21110 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021111 hashtab_T *ht;
21112 int todo;
21113 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021114 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115
21116 /*
21117 * ":function" without argument: list functions.
21118 */
21119 if (ends_excmd(*eap->arg))
21120 {
21121 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021122 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021123 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021124 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021125 {
21126 if (!HASHITEM_EMPTY(hi))
21127 {
21128 --todo;
21129 fp = HI2UF(hi);
21130 if (!isdigit(*fp->uf_name))
21131 list_func_head(fp, FALSE);
21132 }
21133 }
21134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 eap->nextcmd = check_nextcmd(eap->arg);
21136 return;
21137 }
21138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021139 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021140 * ":function /pat": list functions matching pattern.
21141 */
21142 if (*eap->arg == '/')
21143 {
21144 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21145 if (!eap->skip)
21146 {
21147 regmatch_T regmatch;
21148
21149 c = *p;
21150 *p = NUL;
21151 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21152 *p = c;
21153 if (regmatch.regprog != NULL)
21154 {
21155 regmatch.rm_ic = p_ic;
21156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021157 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021158 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21159 {
21160 if (!HASHITEM_EMPTY(hi))
21161 {
21162 --todo;
21163 fp = HI2UF(hi);
21164 if (!isdigit(*fp->uf_name)
21165 && vim_regexec(&regmatch, fp->uf_name, 0))
21166 list_func_head(fp, FALSE);
21167 }
21168 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021169 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021170 }
21171 }
21172 if (*p == '/')
21173 ++p;
21174 eap->nextcmd = check_nextcmd(p);
21175 return;
21176 }
21177
21178 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021179 * Get the function name. There are these situations:
21180 * func normal function name
21181 * "name" == func, "fudi.fd_dict" == NULL
21182 * dict.func new dictionary entry
21183 * "name" == NULL, "fudi.fd_dict" set,
21184 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21185 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021186 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021187 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21188 * dict.func existing dict entry that's not a Funcref
21189 * "name" == NULL, "fudi.fd_dict" set,
21190 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021193 name = trans_function_name(&p, eap->skip, 0, &fudi);
21194 paren = (vim_strchr(p, '(') != NULL);
21195 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 {
21197 /*
21198 * Return on an invalid expression in braces, unless the expression
21199 * evaluation has been cancelled due to an aborting error, an
21200 * interrupt, or an exception.
21201 */
21202 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021203 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021204 if (!eap->skip && fudi.fd_newkey != NULL)
21205 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021206 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 else
21210 eap->skip = TRUE;
21211 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021212
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 /* An error in a function call during evaluation of an expression in magic
21214 * braces should not cause the function not to be defined. */
21215 saved_did_emsg = did_emsg;
21216 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217
21218 /*
21219 * ":function func" with only function name: list function.
21220 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021221 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 {
21223 if (!ends_excmd(*skipwhite(p)))
21224 {
21225 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021226 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 }
21228 eap->nextcmd = check_nextcmd(p);
21229 if (eap->nextcmd != NULL)
21230 *p = NUL;
21231 if (!eap->skip && !got_int)
21232 {
21233 fp = find_func(name);
21234 if (fp != NULL)
21235 {
21236 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021237 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021239 if (FUNCLINE(fp, j) == NULL)
21240 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 msg_putchar('\n');
21242 msg_outnum((long)(j + 1));
21243 if (j < 9)
21244 msg_putchar(' ');
21245 if (j < 99)
21246 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021247 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 out_flush(); /* show a line at a time */
21249 ui_breakcheck();
21250 }
21251 if (!got_int)
21252 {
21253 msg_putchar('\n');
21254 msg_puts((char_u *)" endfunction");
21255 }
21256 }
21257 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021258 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021260 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261 }
21262
21263 /*
21264 * ":function name(arg1, arg2)" Define function.
21265 */
21266 p = skipwhite(p);
21267 if (*p != '(')
21268 {
21269 if (!eap->skip)
21270 {
21271 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021272 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 }
21274 /* attempt to continue by skipping some text */
21275 if (vim_strchr(p, '(') != NULL)
21276 p = vim_strchr(p, '(');
21277 }
21278 p = skipwhite(p + 1);
21279
21280 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21281 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21282
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021283 if (!eap->skip)
21284 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021285 /* Check the name of the function. Unless it's a dictionary function
21286 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021287 if (name != NULL)
21288 arg = name;
21289 else
21290 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021291 if (arg != NULL && (fudi.fd_di == NULL
21292 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021293 {
21294 if (*arg == K_SPECIAL)
21295 j = 3;
21296 else
21297 j = 0;
21298 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21299 : eval_isnamec(arg[j])))
21300 ++j;
21301 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021302 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021303 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021304 /* Disallow using the g: dict. */
21305 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21306 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021307 }
21308
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 /*
21310 * Isolate the arguments: "arg1, arg2, ...)"
21311 */
21312 while (*p != ')')
21313 {
21314 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21315 {
21316 varargs = TRUE;
21317 p += 3;
21318 mustend = TRUE;
21319 }
21320 else
21321 {
21322 arg = p;
21323 while (ASCII_ISALNUM(*p) || *p == '_')
21324 ++p;
21325 if (arg == p || isdigit(*arg)
21326 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21327 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21328 {
21329 if (!eap->skip)
21330 EMSG2(_("E125: Illegal argument: %s"), arg);
21331 break;
21332 }
21333 if (ga_grow(&newargs, 1) == FAIL)
21334 goto erret;
21335 c = *p;
21336 *p = NUL;
21337 arg = vim_strsave(arg);
21338 if (arg == NULL)
21339 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021340
21341 /* Check for duplicate argument name. */
21342 for (i = 0; i < newargs.ga_len; ++i)
21343 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21344 {
21345 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21346 goto erret;
21347 }
21348
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21350 *p = c;
21351 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 if (*p == ',')
21353 ++p;
21354 else
21355 mustend = TRUE;
21356 }
21357 p = skipwhite(p);
21358 if (mustend && *p != ')')
21359 {
21360 if (!eap->skip)
21361 EMSG2(_(e_invarg2), eap->arg);
21362 break;
21363 }
21364 }
21365 ++p; /* skip the ')' */
21366
Bram Moolenaare9a41262005-01-15 22:18:47 +000021367 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 for (;;)
21369 {
21370 p = skipwhite(p);
21371 if (STRNCMP(p, "range", 5) == 0)
21372 {
21373 flags |= FC_RANGE;
21374 p += 5;
21375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021376 else if (STRNCMP(p, "dict", 4) == 0)
21377 {
21378 flags |= FC_DICT;
21379 p += 4;
21380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 else if (STRNCMP(p, "abort", 5) == 0)
21382 {
21383 flags |= FC_ABORT;
21384 p += 5;
21385 }
21386 else
21387 break;
21388 }
21389
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021390 /* When there is a line break use what follows for the function body.
21391 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21392 if (*p == '\n')
21393 line_arg = p + 1;
21394 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 EMSG(_(e_trailing));
21396
21397 /*
21398 * Read the body of the function, until ":endfunction" is found.
21399 */
21400 if (KeyTyped)
21401 {
21402 /* Check if the function already exists, don't let the user type the
21403 * whole function before telling him it doesn't work! For a script we
21404 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021405 if (!eap->skip && !eap->forceit)
21406 {
21407 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21408 EMSG(_(e_funcdict));
21409 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021410 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021413 if (!eap->skip && did_emsg)
21414 goto erret;
21415
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 msg_putchar('\n'); /* don't overwrite the function name */
21417 cmdline_row = msg_row;
21418 }
21419
21420 indent = 2;
21421 nesting = 0;
21422 for (;;)
21423 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021424 if (KeyTyped)
21425 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021427 sourcing_lnum_off = sourcing_lnum;
21428
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021429 if (line_arg != NULL)
21430 {
21431 /* Use eap->arg, split up in parts by line breaks. */
21432 theline = line_arg;
21433 p = vim_strchr(theline, '\n');
21434 if (p == NULL)
21435 line_arg += STRLEN(line_arg);
21436 else
21437 {
21438 *p = NUL;
21439 line_arg = p + 1;
21440 }
21441 }
21442 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 theline = getcmdline(':', 0L, indent);
21444 else
21445 theline = eap->getline(':', eap->cookie, indent);
21446 if (KeyTyped)
21447 lines_left = Rows - 1;
21448 if (theline == NULL)
21449 {
21450 EMSG(_("E126: Missing :endfunction"));
21451 goto erret;
21452 }
21453
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021454 /* Detect line continuation: sourcing_lnum increased more than one. */
21455 if (sourcing_lnum > sourcing_lnum_off + 1)
21456 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21457 else
21458 sourcing_lnum_off = 0;
21459
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 if (skip_until != NULL)
21461 {
21462 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21463 * don't check for ":endfunc". */
21464 if (STRCMP(theline, skip_until) == 0)
21465 {
21466 vim_free(skip_until);
21467 skip_until = NULL;
21468 }
21469 }
21470 else
21471 {
21472 /* skip ':' and blanks*/
21473 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21474 ;
21475
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021476 /* Check for "endfunction". */
21477 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021479 if (line_arg == NULL)
21480 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 break;
21482 }
21483
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021484 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * at "end". */
21486 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21487 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021488 else if (STRNCMP(p, "if", 2) == 0
21489 || STRNCMP(p, "wh", 2) == 0
21490 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 || STRNCMP(p, "try", 3) == 0)
21492 indent += 2;
21493
21494 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021495 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021497 if (*p == '!')
21498 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 p += eval_fname_script(p);
21500 if (ASCII_ISALPHA(*p))
21501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021502 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 if (*skipwhite(p) == '(')
21504 {
21505 ++nesting;
21506 indent += 2;
21507 }
21508 }
21509 }
21510
21511 /* Check for ":append" or ":insert". */
21512 p = skip_range(p, NULL);
21513 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21514 || (p[0] == 'i'
21515 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21516 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21517 skip_until = vim_strsave((char_u *)".");
21518
21519 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21520 arg = skipwhite(skiptowhite(p));
21521 if (arg[0] == '<' && arg[1] =='<'
21522 && ((p[0] == 'p' && p[1] == 'y'
21523 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21524 || (p[0] == 'p' && p[1] == 'e'
21525 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21526 || (p[0] == 't' && p[1] == 'c'
21527 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021528 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21529 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21531 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021532 || (p[0] == 'm' && p[1] == 'z'
21533 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 ))
21535 {
21536 /* ":python <<" continues until a dot, like ":append" */
21537 p = skipwhite(arg + 2);
21538 if (*p == NUL)
21539 skip_until = vim_strsave((char_u *)".");
21540 else
21541 skip_until = vim_strsave(p);
21542 }
21543 }
21544
21545 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021546 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021547 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021548 if (line_arg == NULL)
21549 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021551 }
21552
21553 /* Copy the line to newly allocated memory. get_one_sourceline()
21554 * allocates 250 bytes per line, this saves 80% on average. The cost
21555 * is an extra alloc/free. */
21556 p = vim_strsave(theline);
21557 if (p != NULL)
21558 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021559 if (line_arg == NULL)
21560 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021561 theline = p;
21562 }
21563
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021564 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21565
21566 /* Add NULL lines for continuation lines, so that the line count is
21567 * equal to the index in the growarray. */
21568 while (sourcing_lnum_off-- > 0)
21569 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021570
21571 /* Check for end of eap->arg. */
21572 if (line_arg != NULL && *line_arg == NUL)
21573 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 }
21575
21576 /* Don't define the function when skipping commands or when an error was
21577 * detected. */
21578 if (eap->skip || did_emsg)
21579 goto erret;
21580
21581 /*
21582 * If there are no errors, add the function
21583 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021585 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021586 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021587 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021588 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021589 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021590 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021591 goto erret;
21592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021594 fp = find_func(name);
21595 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597 if (!eap->forceit)
21598 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021599 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021600 goto erret;
21601 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021602 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021604 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 name);
21606 goto erret;
21607 }
21608 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021609 ga_clear_strings(&(fp->uf_args));
21610 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 vim_free(name);
21612 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 }
21615 else
21616 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617 char numbuf[20];
21618
21619 fp = NULL;
21620 if (fudi.fd_newkey == NULL && !eap->forceit)
21621 {
21622 EMSG(_(e_funcdict));
21623 goto erret;
21624 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021625 if (fudi.fd_di == NULL)
21626 {
21627 /* Can't add a function to a locked dictionary */
21628 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21629 goto erret;
21630 }
21631 /* Can't change an existing function if it is locked */
21632 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21633 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634
21635 /* Give the function a sequential number. Can only be used with a
21636 * Funcref! */
21637 vim_free(name);
21638 sprintf(numbuf, "%d", ++func_nr);
21639 name = vim_strsave((char_u *)numbuf);
21640 if (name == NULL)
21641 goto erret;
21642 }
21643
21644 if (fp == NULL)
21645 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021646 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 {
21648 int slen, plen;
21649 char_u *scriptname;
21650
21651 /* Check that the autoload name matches the script name. */
21652 j = FAIL;
21653 if (sourcing_name != NULL)
21654 {
21655 scriptname = autoload_name(name);
21656 if (scriptname != NULL)
21657 {
21658 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021659 plen = (int)STRLEN(p);
21660 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021661 if (slen > plen && fnamecmp(p,
21662 sourcing_name + slen - plen) == 0)
21663 j = OK;
21664 vim_free(scriptname);
21665 }
21666 }
21667 if (j == FAIL)
21668 {
21669 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21670 goto erret;
21671 }
21672 }
21673
21674 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 if (fp == NULL)
21676 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677
21678 if (fudi.fd_dict != NULL)
21679 {
21680 if (fudi.fd_di == NULL)
21681 {
21682 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021683 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021684 if (fudi.fd_di == NULL)
21685 {
21686 vim_free(fp);
21687 goto erret;
21688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021689 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21690 {
21691 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021692 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021693 goto erret;
21694 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 }
21696 else
21697 /* overwrite existing dict entry */
21698 clear_tv(&fudi.fd_di->di_tv);
21699 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021700 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021702 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021703
21704 /* behave like "dict" was used */
21705 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 }
21707
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021709 STRCPY(fp->uf_name, name);
21710 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021712 fp->uf_args = newargs;
21713 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021714#ifdef FEAT_PROFILE
21715 fp->uf_tml_count = NULL;
21716 fp->uf_tml_total = NULL;
21717 fp->uf_tml_self = NULL;
21718 fp->uf_profiling = FALSE;
21719 if (prof_def_func())
21720 func_do_profile(fp);
21721#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021722 fp->uf_varargs = varargs;
21723 fp->uf_flags = flags;
21724 fp->uf_calls = 0;
21725 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021726 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727
21728erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 ga_clear_strings(&newargs);
21730 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021731ret_free:
21732 vim_free(skip_until);
21733 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736}
21737
21738/*
21739 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021740 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021742 * flags:
21743 * TFN_INT: internal function name OK
21744 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 * Advances "pp" to just after the function name (if no error).
21746 */
21747 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021748trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 char_u **pp;
21750 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021754 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 char_u *start;
21756 char_u *end;
21757 int lead;
21758 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021760 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021761
21762 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021763 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021765
21766 /* Check for hard coded <SNR>: already translated function ID (from a user
21767 * command). */
21768 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21769 && (*pp)[2] == (int)KE_SNR)
21770 {
21771 *pp += 3;
21772 len = get_id_len(pp) + 3;
21773 return vim_strnsave(start, len);
21774 }
21775
21776 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21777 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021779 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021781
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021782 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21783 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021784 if (end == start)
21785 {
21786 if (!skip)
21787 EMSG(_("E129: Function name required"));
21788 goto theend;
21789 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021790 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021791 {
21792 /*
21793 * Report an invalid expression in braces, unless the expression
21794 * evaluation has been cancelled due to an aborting error, an
21795 * interrupt, or an exception.
21796 */
21797 if (!aborting())
21798 {
21799 if (end != NULL)
21800 EMSG2(_(e_invarg2), start);
21801 }
21802 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021803 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021804 goto theend;
21805 }
21806
21807 if (lv.ll_tv != NULL)
21808 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 if (fdp != NULL)
21810 {
21811 fdp->fd_dict = lv.ll_dict;
21812 fdp->fd_newkey = lv.ll_newkey;
21813 lv.ll_newkey = NULL;
21814 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021815 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021816 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21817 {
21818 name = vim_strsave(lv.ll_tv->vval.v_string);
21819 *pp = end;
21820 }
21821 else
21822 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021823 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21824 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021825 EMSG(_(e_funcref));
21826 else
21827 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021828 name = NULL;
21829 }
21830 goto theend;
21831 }
21832
21833 if (lv.ll_name == NULL)
21834 {
21835 /* Error found, but continue after the function name. */
21836 *pp = end;
21837 goto theend;
21838 }
21839
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021840 /* Check if the name is a Funcref. If so, use the value. */
21841 if (lv.ll_exp_name != NULL)
21842 {
21843 len = (int)STRLEN(lv.ll_exp_name);
21844 name = deref_func_name(lv.ll_exp_name, &len);
21845 if (name == lv.ll_exp_name)
21846 name = NULL;
21847 }
21848 else
21849 {
21850 len = (int)(end - *pp);
21851 name = deref_func_name(*pp, &len);
21852 if (name == *pp)
21853 name = NULL;
21854 }
21855 if (name != NULL)
21856 {
21857 name = vim_strsave(name);
21858 *pp = end;
21859 goto theend;
21860 }
21861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021862 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021863 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021864 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021865 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21866 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21867 {
21868 /* When there was "s:" already or the name expanded to get a
21869 * leading "s:" then remove it. */
21870 lv.ll_name += 2;
21871 len -= 2;
21872 lead = 2;
21873 }
21874 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021875 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021876 {
21877 if (lead == 2) /* skip over "s:" */
21878 lv.ll_name += 2;
21879 len = (int)(end - lv.ll_name);
21880 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021881
21882 /*
21883 * Copy the function name to allocated memory.
21884 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21885 * Accept <SNR>123_name() outside a script.
21886 */
21887 if (skip)
21888 lead = 0; /* do nothing */
21889 else if (lead > 0)
21890 {
21891 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021892 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21893 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021894 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021895 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021896 if (current_SID <= 0)
21897 {
21898 EMSG(_(e_usingsid));
21899 goto theend;
21900 }
21901 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21902 lead += (int)STRLEN(sid_buf);
21903 }
21904 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021905 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021906 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021907 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021908 goto theend;
21909 }
21910 name = alloc((unsigned)(len + lead + 1));
21911 if (name != NULL)
21912 {
21913 if (lead > 0)
21914 {
21915 name[0] = K_SPECIAL;
21916 name[1] = KS_EXTRA;
21917 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021918 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021919 STRCPY(name + 3, sid_buf);
21920 }
21921 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21922 name[len + lead] = NUL;
21923 }
21924 *pp = end;
21925
21926theend:
21927 clear_lval(&lv);
21928 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929}
21930
21931/*
21932 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21933 * Return 2 if "p" starts with "s:".
21934 * Return 0 otherwise.
21935 */
21936 static int
21937eval_fname_script(p)
21938 char_u *p;
21939{
21940 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21941 || STRNICMP(p + 1, "SNR>", 4) == 0))
21942 return 5;
21943 if (p[0] == 's' && p[1] == ':')
21944 return 2;
21945 return 0;
21946}
21947
21948/*
21949 * Return TRUE if "p" starts with "<SID>" or "s:".
21950 * Only works if eval_fname_script() returned non-zero for "p"!
21951 */
21952 static int
21953eval_fname_sid(p)
21954 char_u *p;
21955{
21956 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21957}
21958
21959/*
21960 * List the head of the function: "name(arg1, arg2)".
21961 */
21962 static void
21963list_func_head(fp, indent)
21964 ufunc_T *fp;
21965 int indent;
21966{
21967 int j;
21968
21969 msg_start();
21970 if (indent)
21971 MSG_PUTS(" ");
21972 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021973 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 {
21975 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021976 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 }
21978 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021979 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021981 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982 {
21983 if (j)
21984 MSG_PUTS(", ");
21985 msg_puts(FUNCARG(fp, j));
21986 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021987 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 {
21989 if (j)
21990 MSG_PUTS(", ");
21991 MSG_PUTS("...");
21992 }
21993 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021994 if (fp->uf_flags & FC_ABORT)
21995 MSG_PUTS(" abort");
21996 if (fp->uf_flags & FC_RANGE)
21997 MSG_PUTS(" range");
21998 if (fp->uf_flags & FC_DICT)
21999 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022000 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022001 if (p_verbose > 0)
22002 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003}
22004
22005/*
22006 * Find a function by name, return pointer to it in ufuncs.
22007 * Return NULL for unknown function.
22008 */
22009 static ufunc_T *
22010find_func(name)
22011 char_u *name;
22012{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022013 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022015 hi = hash_find(&func_hashtab, name);
22016 if (!HASHITEM_EMPTY(hi))
22017 return HI2UF(hi);
22018 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019}
22020
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022021#if defined(EXITFREE) || defined(PROTO)
22022 void
22023free_all_functions()
22024{
22025 hashitem_T *hi;
22026
22027 /* Need to start all over every time, because func_free() may change the
22028 * hash table. */
22029 while (func_hashtab.ht_used > 0)
22030 for (hi = func_hashtab.ht_array; ; ++hi)
22031 if (!HASHITEM_EMPTY(hi))
22032 {
22033 func_free(HI2UF(hi));
22034 break;
22035 }
22036}
22037#endif
22038
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022039 int
22040translated_function_exists(name)
22041 char_u *name;
22042{
22043 if (builtin_function(name))
22044 return find_internal_func(name) >= 0;
22045 return find_func(name) != NULL;
22046}
22047
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022048/*
22049 * Return TRUE if a function "name" exists.
22050 */
22051 static int
22052function_exists(name)
22053 char_u *name;
22054{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022055 char_u *nm = name;
22056 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057 int n = FALSE;
22058
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022059 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022060 nm = skipwhite(nm);
22061
22062 /* Only accept "funcname", "funcname ", "funcname (..." and
22063 * "funcname(...", not "funcname!...". */
22064 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022065 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022066 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022067 return n;
22068}
22069
Bram Moolenaara1544c02013-05-30 12:35:52 +020022070 char_u *
22071get_expanded_name(name, check)
22072 char_u *name;
22073 int check;
22074{
22075 char_u *nm = name;
22076 char_u *p;
22077
22078 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22079
22080 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022081 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022082 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022083
Bram Moolenaara1544c02013-05-30 12:35:52 +020022084 vim_free(p);
22085 return NULL;
22086}
22087
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022088/*
22089 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022090 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022091 */
22092 static int
22093builtin_function(name)
22094 char_u *name;
22095{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022096 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22097 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022098}
22099
Bram Moolenaar05159a02005-02-26 23:04:13 +000022100#if defined(FEAT_PROFILE) || defined(PROTO)
22101/*
22102 * Start profiling function "fp".
22103 */
22104 static void
22105func_do_profile(fp)
22106 ufunc_T *fp;
22107{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022108 int len = fp->uf_lines.ga_len;
22109
22110 if (len == 0)
22111 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022112 fp->uf_tm_count = 0;
22113 profile_zero(&fp->uf_tm_self);
22114 profile_zero(&fp->uf_tm_total);
22115 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022116 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022117 if (fp->uf_tml_total == NULL)
22118 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022119 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022120 if (fp->uf_tml_self == NULL)
22121 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022122 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022123 fp->uf_tml_idx = -1;
22124 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22125 || fp->uf_tml_self == NULL)
22126 return; /* out of memory */
22127
22128 fp->uf_profiling = TRUE;
22129}
22130
22131/*
22132 * Dump the profiling results for all functions in file "fd".
22133 */
22134 void
22135func_dump_profile(fd)
22136 FILE *fd;
22137{
22138 hashitem_T *hi;
22139 int todo;
22140 ufunc_T *fp;
22141 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022142 ufunc_T **sorttab;
22143 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022144
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022145 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022146 if (todo == 0)
22147 return; /* nothing to dump */
22148
Bram Moolenaar73830342005-02-28 22:48:19 +000022149 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22150
Bram Moolenaar05159a02005-02-26 23:04:13 +000022151 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22152 {
22153 if (!HASHITEM_EMPTY(hi))
22154 {
22155 --todo;
22156 fp = HI2UF(hi);
22157 if (fp->uf_profiling)
22158 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022159 if (sorttab != NULL)
22160 sorttab[st_len++] = fp;
22161
Bram Moolenaar05159a02005-02-26 23:04:13 +000022162 if (fp->uf_name[0] == K_SPECIAL)
22163 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22164 else
22165 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22166 if (fp->uf_tm_count == 1)
22167 fprintf(fd, "Called 1 time\n");
22168 else
22169 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22170 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22171 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22172 fprintf(fd, "\n");
22173 fprintf(fd, "count total (s) self (s)\n");
22174
22175 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22176 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022177 if (FUNCLINE(fp, i) == NULL)
22178 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022179 prof_func_line(fd, fp->uf_tml_count[i],
22180 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022181 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22182 }
22183 fprintf(fd, "\n");
22184 }
22185 }
22186 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022187
22188 if (sorttab != NULL && st_len > 0)
22189 {
22190 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22191 prof_total_cmp);
22192 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22193 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22194 prof_self_cmp);
22195 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22196 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022197
22198 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022199}
Bram Moolenaar73830342005-02-28 22:48:19 +000022200
22201 static void
22202prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22203 FILE *fd;
22204 ufunc_T **sorttab;
22205 int st_len;
22206 char *title;
22207 int prefer_self; /* when equal print only self time */
22208{
22209 int i;
22210 ufunc_T *fp;
22211
22212 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22213 fprintf(fd, "count total (s) self (s) function\n");
22214 for (i = 0; i < 20 && i < st_len; ++i)
22215 {
22216 fp = sorttab[i];
22217 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22218 prefer_self);
22219 if (fp->uf_name[0] == K_SPECIAL)
22220 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22221 else
22222 fprintf(fd, " %s()\n", fp->uf_name);
22223 }
22224 fprintf(fd, "\n");
22225}
22226
22227/*
22228 * Print the count and times for one function or function line.
22229 */
22230 static void
22231prof_func_line(fd, count, total, self, prefer_self)
22232 FILE *fd;
22233 int count;
22234 proftime_T *total;
22235 proftime_T *self;
22236 int prefer_self; /* when equal print only self time */
22237{
22238 if (count > 0)
22239 {
22240 fprintf(fd, "%5d ", count);
22241 if (prefer_self && profile_equal(total, self))
22242 fprintf(fd, " ");
22243 else
22244 fprintf(fd, "%s ", profile_msg(total));
22245 if (!prefer_self && profile_equal(total, self))
22246 fprintf(fd, " ");
22247 else
22248 fprintf(fd, "%s ", profile_msg(self));
22249 }
22250 else
22251 fprintf(fd, " ");
22252}
22253
22254/*
22255 * Compare function for total time sorting.
22256 */
22257 static int
22258#ifdef __BORLANDC__
22259_RTLENTRYF
22260#endif
22261prof_total_cmp(s1, s2)
22262 const void *s1;
22263 const void *s2;
22264{
22265 ufunc_T *p1, *p2;
22266
22267 p1 = *(ufunc_T **)s1;
22268 p2 = *(ufunc_T **)s2;
22269 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22270}
22271
22272/*
22273 * Compare function for self time sorting.
22274 */
22275 static int
22276#ifdef __BORLANDC__
22277_RTLENTRYF
22278#endif
22279prof_self_cmp(s1, s2)
22280 const void *s1;
22281 const void *s2;
22282{
22283 ufunc_T *p1, *p2;
22284
22285 p1 = *(ufunc_T **)s1;
22286 p2 = *(ufunc_T **)s2;
22287 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22288}
22289
Bram Moolenaar05159a02005-02-26 23:04:13 +000022290#endif
22291
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022292/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022294 * Return TRUE if a package was loaded.
22295 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022296 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022297script_autoload(name, reload)
22298 char_u *name;
22299 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022300{
22301 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022302 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022303 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022304 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022305
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022306 /* Return quickly when autoload disabled. */
22307 if (no_autoload)
22308 return FALSE;
22309
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022310 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022311 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022312 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022313 return FALSE;
22314
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022315 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022316
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022317 /* Find the name in the list of previously loaded package names. Skip
22318 * "autoload/", it's always the same. */
22319 for (i = 0; i < ga_loaded.ga_len; ++i)
22320 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22321 break;
22322 if (!reload && i < ga_loaded.ga_len)
22323 ret = FALSE; /* was loaded already */
22324 else
22325 {
22326 /* Remember the name if it wasn't loaded already. */
22327 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22328 {
22329 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22330 tofree = NULL;
22331 }
22332
22333 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022334 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022335 ret = TRUE;
22336 }
22337
22338 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022339 return ret;
22340}
22341
22342/*
22343 * Return the autoload script name for a function or variable name.
22344 * Returns NULL when out of memory.
22345 */
22346 static char_u *
22347autoload_name(name)
22348 char_u *name;
22349{
22350 char_u *p;
22351 char_u *scriptname;
22352
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022353 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022354 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22355 if (scriptname == NULL)
22356 return FALSE;
22357 STRCPY(scriptname, "autoload/");
22358 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022359 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022360 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022361 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022362 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022363 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022364}
22365
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22367
22368/*
22369 * Function given to ExpandGeneric() to obtain the list of user defined
22370 * function names.
22371 */
22372 char_u *
22373get_user_func_name(xp, idx)
22374 expand_T *xp;
22375 int idx;
22376{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022377 static long_u done;
22378 static hashitem_T *hi;
22379 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380
22381 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022383 done = 0;
22384 hi = func_hashtab.ht_array;
22385 }
22386 if (done < func_hashtab.ht_used)
22387 {
22388 if (done++ > 0)
22389 ++hi;
22390 while (HASHITEM_EMPTY(hi))
22391 ++hi;
22392 fp = HI2UF(hi);
22393
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022394 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022395 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022396
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022397 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22398 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399
22400 cat_func_name(IObuff, fp);
22401 if (xp->xp_context != EXPAND_USER_FUNC)
22402 {
22403 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022404 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 STRCAT(IObuff, ")");
22406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 return IObuff;
22408 }
22409 return NULL;
22410}
22411
22412#endif /* FEAT_CMDL_COMPL */
22413
22414/*
22415 * Copy the function name of "fp" to buffer "buf".
22416 * "buf" must be able to hold the function name plus three bytes.
22417 * Takes care of script-local function names.
22418 */
22419 static void
22420cat_func_name(buf, fp)
22421 char_u *buf;
22422 ufunc_T *fp;
22423{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022424 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 {
22426 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022427 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022430 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431}
22432
22433/*
22434 * ":delfunction {name}"
22435 */
22436 void
22437ex_delfunction(eap)
22438 exarg_T *eap;
22439{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022440 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 char_u *p;
22442 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444
22445 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022446 name = trans_function_name(&p, eap->skip, 0, &fudi);
22447 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022449 {
22450 if (fudi.fd_dict != NULL && !eap->skip)
22451 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 if (!ends_excmd(*skipwhite(p)))
22455 {
22456 vim_free(name);
22457 EMSG(_(e_trailing));
22458 return;
22459 }
22460 eap->nextcmd = check_nextcmd(p);
22461 if (eap->nextcmd != NULL)
22462 *p = NUL;
22463
22464 if (!eap->skip)
22465 fp = find_func(name);
22466 vim_free(name);
22467
22468 if (!eap->skip)
22469 {
22470 if (fp == NULL)
22471 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022472 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 return;
22474 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022475 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476 {
22477 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22478 return;
22479 }
22480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022481 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 /* Delete the dict item that refers to the function, it will
22484 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022485 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022487 else
22488 func_free(fp);
22489 }
22490}
22491
22492/*
22493 * Free a function and remove it from the list of functions.
22494 */
22495 static void
22496func_free(fp)
22497 ufunc_T *fp;
22498{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022499 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022500
22501 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022502 ga_clear_strings(&(fp->uf_args));
22503 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022504#ifdef FEAT_PROFILE
22505 vim_free(fp->uf_tml_count);
22506 vim_free(fp->uf_tml_total);
22507 vim_free(fp->uf_tml_self);
22508#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022509
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022510 /* remove the function from the function hashtable */
22511 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22512 if (HASHITEM_EMPTY(hi))
22513 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022514 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 hash_remove(&func_hashtab, hi);
22516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022517 vim_free(fp);
22518}
22519
22520/*
22521 * Unreference a Function: decrement the reference count and free it when it
22522 * becomes zero. Only for numbered functions.
22523 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022524 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022525func_unref(name)
22526 char_u *name;
22527{
22528 ufunc_T *fp;
22529
22530 if (name != NULL && isdigit(*name))
22531 {
22532 fp = find_func(name);
22533 if (fp == NULL)
22534 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022535 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022536 {
22537 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022538 * when "uf_calls" becomes zero. */
22539 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 func_free(fp);
22541 }
22542 }
22543}
22544
22545/*
22546 * Count a reference to a Function.
22547 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022548 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022549func_ref(name)
22550 char_u *name;
22551{
22552 ufunc_T *fp;
22553
22554 if (name != NULL && isdigit(*name))
22555 {
22556 fp = find_func(name);
22557 if (fp == NULL)
22558 EMSG2(_(e_intern2), "func_ref()");
22559 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022560 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
22562}
22563
22564/*
22565 * Call a user function.
22566 */
22567 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022568call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569 ufunc_T *fp; /* pointer to function */
22570 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 typval_T *argvars; /* arguments */
22572 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 linenr_T firstline; /* first line of range */
22574 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 char_u *save_sourcing_name;
22578 linenr_T save_sourcing_lnum;
22579 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022580 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 int save_did_emsg;
22582 static int depth = 0;
22583 dictitem_T *v;
22584 int fixvar_idx = 0; /* index in fixvar[] */
22585 int i;
22586 int ai;
22587 char_u numbuf[NUMBUFLEN];
22588 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022589#ifdef FEAT_PROFILE
22590 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022591 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593
22594 /* If depth of calling is getting too high, don't execute the function */
22595 if (depth >= p_mfd)
22596 {
22597 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022598 rettv->v_type = VAR_NUMBER;
22599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 return;
22601 }
22602 ++depth;
22603
22604 line_breakcheck(); /* check for CTRL-C hit */
22605
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022606 fc = (funccall_T *)alloc(sizeof(funccall_T));
22607 fc->caller = current_funccal;
22608 current_funccal = fc;
22609 fc->func = fp;
22610 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022611 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022612 fc->linenr = 0;
22613 fc->returned = FALSE;
22614 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022616 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22617 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022620 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22622 * each argument variable and saves a lot of time.
22623 */
22624 /*
22625 * Init l: variables.
22626 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022627 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022628 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022629 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022630 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22631 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022632 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022633 name = v->di_key;
22634 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022636 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022638 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022639 v->di_tv.vval.v_dict = selfdict;
22640 ++selfdict->dv_refcount;
22641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022642
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 /*
22644 * Init a: variables.
22645 * Set a:0 to "argcount".
22646 * Set a:000 to a list with room for the "..." arguments.
22647 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022648 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022649 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022650 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022651 /* Use "name" to avoid a warning from some compiler that checks the
22652 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022653 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022654 name = v->di_key;
22655 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022657 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022659 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022660 v->di_tv.vval.v_list = &fc->l_varlist;
22661 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22662 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22663 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022664
22665 /*
22666 * Set a:firstline to "firstline" and a:lastline to "lastline".
22667 * Set a:name to named arguments.
22668 * Set a:N to the "..." arguments.
22669 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022672 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 (varnumber_T)lastline);
22674 for (i = 0; i < argcount; ++i)
22675 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022676 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022677 if (ai < 0)
22678 /* named argument a:name */
22679 name = FUNCARG(fp, i);
22680 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 /* "..." argument a:1, a:2, etc. */
22683 sprintf((char *)numbuf, "%d", ai + 1);
22684 name = numbuf;
22685 }
22686 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22687 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022688 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22690 }
22691 else
22692 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022693 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22694 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 if (v == NULL)
22696 break;
22697 v->di_flags = DI_FLAGS_RO;
22698 }
22699 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022700 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022701
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022702 /* Note: the values are copied directly to avoid alloc/free.
22703 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022705 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706
22707 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22708 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022709 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22710 fc->l_listitems[ai].li_tv = argvars[i];
22711 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022712 }
22713 }
22714
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715 /* Don't redraw while executing the function. */
22716 ++RedrawingDisabled;
22717 save_sourcing_name = sourcing_name;
22718 save_sourcing_lnum = sourcing_lnum;
22719 sourcing_lnum = 1;
22720 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022721 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 if (sourcing_name != NULL)
22723 {
22724 if (save_sourcing_name != NULL
22725 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22726 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22727 else
22728 STRCPY(sourcing_name, "function ");
22729 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22730
22731 if (p_verbose >= 12)
22732 {
22733 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022734 verbose_enter_scroll();
22735
Bram Moolenaar555b2802005-05-19 21:08:39 +000022736 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 if (p_verbose >= 14)
22738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022740 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022741 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022742 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743
22744 msg_puts((char_u *)"(");
22745 for (i = 0; i < argcount; ++i)
22746 {
22747 if (i > 0)
22748 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022749 if (argvars[i].v_type == VAR_NUMBER)
22750 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 else
22752 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022753 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22754 if (s != NULL)
22755 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022756 if (vim_strsize(s) > MSG_BUF_CLEN)
22757 {
22758 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22759 s = buf;
22760 }
22761 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022762 vim_free(tofree);
22763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 }
22765 }
22766 msg_puts((char_u *)")");
22767 }
22768 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022769
22770 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 --no_wait_return;
22772 }
22773 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022774#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022775 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022776 {
22777 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22778 func_do_profile(fp);
22779 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022780 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022781 {
22782 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022783 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022784 profile_zero(&fp->uf_tm_children);
22785 }
22786 script_prof_save(&wait_start);
22787 }
22788#endif
22789
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 save_did_emsg = did_emsg;
22793 did_emsg = FALSE;
22794
22795 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022796 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22798
22799 --RedrawingDisabled;
22800
22801 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022802 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022804 clear_tv(rettv);
22805 rettv->v_type = VAR_NUMBER;
22806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 }
22808
Bram Moolenaar05159a02005-02-26 23:04:13 +000022809#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022810 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022811 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022812 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022813 profile_end(&call_start);
22814 profile_sub_wait(&wait_start, &call_start);
22815 profile_add(&fp->uf_tm_total, &call_start);
22816 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022817 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022818 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022819 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22820 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022821 }
22822 }
22823#endif
22824
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 /* when being verbose, mention the return value */
22826 if (p_verbose >= 12)
22827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022829 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022832 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022833 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022834 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022835 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022838 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022839 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022840 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022841 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022842
Bram Moolenaar555b2802005-05-19 21:08:39 +000022843 /* The value may be very long. Skip the middle part, so that we
22844 * have some idea how it starts and ends. smsg() would always
22845 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022846 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022847 if (s != NULL)
22848 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022849 if (vim_strsize(s) > MSG_BUF_CLEN)
22850 {
22851 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22852 s = buf;
22853 }
22854 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022855 vim_free(tofree);
22856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 }
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
22864 vim_free(sourcing_name);
22865 sourcing_name = save_sourcing_name;
22866 sourcing_lnum = save_sourcing_lnum;
22867 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022868#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022869 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022870 script_prof_restore(&wait_start);
22871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872
22873 if (p_verbose >= 12 && sourcing_name != NULL)
22874 {
22875 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022876 verbose_enter_scroll();
22877
Bram Moolenaar555b2802005-05-19 21:08:39 +000022878 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022880
22881 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 --no_wait_return;
22883 }
22884
22885 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022886 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022888
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022889 /* If the a:000 list and the l: and a: dicts are not referenced we can
22890 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022891 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22892 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22893 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22894 {
22895 free_funccal(fc, FALSE);
22896 }
22897 else
22898 {
22899 hashitem_T *hi;
22900 listitem_T *li;
22901 int todo;
22902
22903 /* "fc" is still in use. This can happen when returning "a:000" or
22904 * assigning "l:" to a global variable.
22905 * Link "fc" in the list for garbage collection later. */
22906 fc->caller = previous_funccal;
22907 previous_funccal = fc;
22908
22909 /* Make a copy of the a: variables, since we didn't do that above. */
22910 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22911 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22912 {
22913 if (!HASHITEM_EMPTY(hi))
22914 {
22915 --todo;
22916 v = HI2DI(hi);
22917 copy_tv(&v->di_tv, &v->di_tv);
22918 }
22919 }
22920
22921 /* Make a copy of the a:000 items, since we didn't do that above. */
22922 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22923 copy_tv(&li->li_tv, &li->li_tv);
22924 }
22925}
22926
22927/*
22928 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022929 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022930 */
22931 static int
22932can_free_funccal(fc, copyID)
22933 funccall_T *fc;
22934 int copyID;
22935{
22936 return (fc->l_varlist.lv_copyID != copyID
22937 && fc->l_vars.dv_copyID != copyID
22938 && fc->l_avars.dv_copyID != copyID);
22939}
22940
22941/*
22942 * Free "fc" and what it contains.
22943 */
22944 static void
22945free_funccal(fc, free_val)
22946 funccall_T *fc;
22947 int free_val; /* a: vars were allocated */
22948{
22949 listitem_T *li;
22950
22951 /* The a: variables typevals may not have been allocated, only free the
22952 * allocated variables. */
22953 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22954
22955 /* free all l: variables */
22956 vars_clear(&fc->l_vars.dv_hashtab);
22957
22958 /* Free the a:000 variables if they were allocated. */
22959 if (free_val)
22960 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22961 clear_tv(&li->li_tv);
22962
22963 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964}
22965
22966/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 * Add a number variable "name" to dict "dp" with value "nr".
22968 */
22969 static void
22970add_nr_var(dp, v, name, nr)
22971 dict_T *dp;
22972 dictitem_T *v;
22973 char *name;
22974 varnumber_T nr;
22975{
22976 STRCPY(v->di_key, name);
22977 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22978 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22979 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022980 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022981 v->di_tv.vval.v_number = nr;
22982}
22983
22984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 * ":return [expr]"
22986 */
22987 void
22988ex_return(eap)
22989 exarg_T *eap;
22990{
22991 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 int returning = FALSE;
22994
22995 if (current_funccal == NULL)
22996 {
22997 EMSG(_("E133: :return not inside a function"));
22998 return;
22999 }
23000
23001 if (eap->skip)
23002 ++emsg_skip;
23003
23004 eap->nextcmd = NULL;
23005 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 {
23008 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023009 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023011 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
23013 /* It's safer to return also on error. */
23014 else if (!eap->skip)
23015 {
23016 /*
23017 * Return unless the expression evaluation has been cancelled due to an
23018 * aborting error, an interrupt, or an exception.
23019 */
23020 if (!aborting())
23021 returning = do_return(eap, FALSE, TRUE, NULL);
23022 }
23023
23024 /* When skipping or the return gets pending, advance to the next command
23025 * in this line (!returning). Otherwise, ignore the rest of the line.
23026 * Following lines will be ignored by get_func_line(). */
23027 if (returning)
23028 eap->nextcmd = NULL;
23029 else if (eap->nextcmd == NULL) /* no argument */
23030 eap->nextcmd = check_nextcmd(arg);
23031
23032 if (eap->skip)
23033 --emsg_skip;
23034}
23035
23036/*
23037 * Return from a function. Possibly makes the return pending. Also called
23038 * for a pending return at the ":endtry" or after returning from an extra
23039 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023041 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 * FALSE when the return gets pending.
23043 */
23044 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023045do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 exarg_T *eap;
23047 int reanimate;
23048 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023049 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050{
23051 int idx;
23052 struct condstack *cstack = eap->cstack;
23053
23054 if (reanimate)
23055 /* Undo the return. */
23056 current_funccal->returned = FALSE;
23057
23058 /*
23059 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23060 * not in its finally clause (which then is to be executed next) is found.
23061 * In this case, make the ":return" pending for execution at the ":endtry".
23062 * Otherwise, return normally.
23063 */
23064 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23065 if (idx >= 0)
23066 {
23067 cstack->cs_pending[idx] = CSTP_RETURN;
23068
23069 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023070 /* A pending return again gets pending. "rettv" points to an
23071 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023073 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 else
23075 {
23076 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023077 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023079 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023081 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 {
23083 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023084 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 else
23087 EMSG(_(e_outofmem));
23088 }
23089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023090 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091
23092 if (reanimate)
23093 {
23094 /* The pending return value could be overwritten by a ":return"
23095 * without argument in a finally clause; reset the default
23096 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023097 current_funccal->rettv->v_type = VAR_NUMBER;
23098 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 }
23100 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023101 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
23103 else
23104 {
23105 current_funccal->returned = TRUE;
23106
23107 /* If the return is carried out now, store the return value. For
23108 * a return immediately after reanimation, the value is already
23109 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023110 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023112 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023113 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023115 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
23117 }
23118
23119 return idx < 0;
23120}
23121
23122/*
23123 * Free the variable with a pending return value.
23124 */
23125 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023126discard_pending_return(rettv)
23127 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128{
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130}
23131
23132/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023133 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 * is an allocated string. Used by report_pending() for verbose messages.
23135 */
23136 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023137get_return_cmd(rettv)
23138 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023140 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023141 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023142 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023144 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023145 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023146 if (s == NULL)
23147 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023148
23149 STRCPY(IObuff, ":return ");
23150 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23151 if (STRLEN(s) + 8 >= IOSIZE)
23152 STRCPY(IObuff + IOSIZE - 4, "...");
23153 vim_free(tofree);
23154 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155}
23156
23157/*
23158 * Get next function line.
23159 * Called by do_cmdline() to get the next line.
23160 * Returns allocated string, or NULL for end of function.
23161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 char_u *
23163get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023164 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023166 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167{
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023169 ufunc_T *fp = fcp->func;
23170 char_u *retval;
23171 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172
23173 /* If breakpoints have been added/deleted need to check for it. */
23174 if (fcp->dbg_tick != debug_tick)
23175 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023176 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 sourcing_lnum);
23178 fcp->dbg_tick = debug_tick;
23179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023180#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023181 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023182 func_line_end(cookie);
23183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184
Bram Moolenaar05159a02005-02-26 23:04:13 +000023185 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023186 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23187 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 retval = NULL;
23189 else
23190 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023191 /* Skip NULL lines (continuation lines). */
23192 while (fcp->linenr < gap->ga_len
23193 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23194 ++fcp->linenr;
23195 if (fcp->linenr >= gap->ga_len)
23196 retval = NULL;
23197 else
23198 {
23199 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23200 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023201#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023202 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023203 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023204#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 }
23207
23208 /* Did we encounter a breakpoint? */
23209 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23210 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023211 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023213 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 sourcing_lnum);
23215 fcp->dbg_tick = debug_tick;
23216 }
23217
23218 return retval;
23219}
23220
Bram Moolenaar05159a02005-02-26 23:04:13 +000023221#if defined(FEAT_PROFILE) || defined(PROTO)
23222/*
23223 * Called when starting to read a function line.
23224 * "sourcing_lnum" must be correct!
23225 * When skipping lines it may not actually be executed, but we won't find out
23226 * until later and we need to store the time now.
23227 */
23228 void
23229func_line_start(cookie)
23230 void *cookie;
23231{
23232 funccall_T *fcp = (funccall_T *)cookie;
23233 ufunc_T *fp = fcp->func;
23234
23235 if (fp->uf_profiling && sourcing_lnum >= 1
23236 && sourcing_lnum <= fp->uf_lines.ga_len)
23237 {
23238 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023239 /* Skip continuation lines. */
23240 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23241 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023242 fp->uf_tml_execed = FALSE;
23243 profile_start(&fp->uf_tml_start);
23244 profile_zero(&fp->uf_tml_children);
23245 profile_get_wait(&fp->uf_tml_wait);
23246 }
23247}
23248
23249/*
23250 * Called when actually executing a function line.
23251 */
23252 void
23253func_line_exec(cookie)
23254 void *cookie;
23255{
23256 funccall_T *fcp = (funccall_T *)cookie;
23257 ufunc_T *fp = fcp->func;
23258
23259 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23260 fp->uf_tml_execed = TRUE;
23261}
23262
23263/*
23264 * Called when done with a function line.
23265 */
23266 void
23267func_line_end(cookie)
23268 void *cookie;
23269{
23270 funccall_T *fcp = (funccall_T *)cookie;
23271 ufunc_T *fp = fcp->func;
23272
23273 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23274 {
23275 if (fp->uf_tml_execed)
23276 {
23277 ++fp->uf_tml_count[fp->uf_tml_idx];
23278 profile_end(&fp->uf_tml_start);
23279 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023280 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023281 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23282 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023283 }
23284 fp->uf_tml_idx = -1;
23285 }
23286}
23287#endif
23288
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289/*
23290 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023291 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 */
23293 int
23294func_has_ended(cookie)
23295 void *cookie;
23296{
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298
23299 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23300 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023301 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 || fcp->returned);
23303}
23304
23305/*
23306 * return TRUE if cookie indicates a function which "abort"s on errors.
23307 */
23308 int
23309func_has_abort(cookie)
23310 void *cookie;
23311{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023312 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313}
23314
23315#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23316typedef enum
23317{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023318 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23319 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23320 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321} var_flavour_T;
23322
23323static var_flavour_T var_flavour __ARGS((char_u *varname));
23324
23325 static var_flavour_T
23326var_flavour(varname)
23327 char_u *varname;
23328{
23329 char_u *p = varname;
23330
23331 if (ASCII_ISUPPER(*p))
23332 {
23333 while (*(++p))
23334 if (ASCII_ISLOWER(*p))
23335 return VAR_FLAVOUR_SESSION;
23336 return VAR_FLAVOUR_VIMINFO;
23337 }
23338 else
23339 return VAR_FLAVOUR_DEFAULT;
23340}
23341#endif
23342
23343#if defined(FEAT_VIMINFO) || defined(PROTO)
23344/*
23345 * Restore global vars that start with a capital from the viminfo file
23346 */
23347 int
23348read_viminfo_varlist(virp, writing)
23349 vir_T *virp;
23350 int writing;
23351{
23352 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023353 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355
23356 if (!writing && (find_viminfo_parameter('!') != NULL))
23357 {
23358 tab = vim_strchr(virp->vir_line + 1, '\t');
23359 if (tab != NULL)
23360 {
23361 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023362 switch (*tab)
23363 {
23364 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023365#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023366 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023367#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023368 case 'D': type = VAR_DICT; break;
23369 case 'L': type = VAR_LIST; break;
23370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371
23372 tab = vim_strchr(tab, '\t');
23373 if (tab != NULL)
23374 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023375 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023376 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023377 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023379#ifdef FEAT_FLOAT
23380 else if (type == VAR_FLOAT)
23381 (void)string2float(tab + 1, &tv.vval.v_float);
23382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023384 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023385 if (type == VAR_DICT || type == VAR_LIST)
23386 {
23387 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23388
23389 if (etv == NULL)
23390 /* Failed to parse back the dict or list, use it as a
23391 * string. */
23392 tv.v_type = VAR_STRING;
23393 else
23394 {
23395 vim_free(tv.vval.v_string);
23396 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023397 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023398 }
23399 }
23400
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023401 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023402
23403 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023404 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023405 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23406 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 }
23408 }
23409 }
23410
23411 return viminfo_readline(virp);
23412}
23413
23414/*
23415 * Write global vars that start with a capital to the viminfo file
23416 */
23417 void
23418write_viminfo_varlist(fp)
23419 FILE *fp;
23420{
Bram Moolenaar33570922005-01-25 22:26:29 +000023421 hashitem_T *hi;
23422 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023423 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023424 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023425 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023426 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428
23429 if (find_viminfo_parameter('!') == NULL)
23430 return;
23431
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023432 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023434 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023435 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023437 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023439 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023440 this_var = HI2DI(hi);
23441 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023443 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023444 {
23445 case VAR_STRING: s = "STR"; break;
23446 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023447#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023448 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023449#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023450 case VAR_DICT: s = "DIC"; break;
23451 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023452 default: continue;
23453 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023455 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023456 if (p != NULL)
23457 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023458 vim_free(tofree);
23459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 }
23461 }
23462}
23463#endif
23464
23465#if defined(FEAT_SESSION) || defined(PROTO)
23466 int
23467store_session_globals(fd)
23468 FILE *fd;
23469{
Bram Moolenaar33570922005-01-25 22:26:29 +000023470 hashitem_T *hi;
23471 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023472 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 char_u *p, *t;
23474
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023475 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023476 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023478 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023480 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 this_var = HI2DI(hi);
23482 if ((this_var->di_tv.v_type == VAR_NUMBER
23483 || this_var->di_tv.v_type == VAR_STRING)
23484 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023486 /* Escape special characters with a backslash. Turn a LF and
23487 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023488 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023489 (char_u *)"\\\"\n\r");
23490 if (p == NULL) /* out of memory */
23491 break;
23492 for (t = p; *t != NUL; ++t)
23493 if (*t == '\n')
23494 *t = 'n';
23495 else if (*t == '\r')
23496 *t = 'r';
23497 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023498 this_var->di_key,
23499 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23500 : ' ',
23501 p,
23502 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23503 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023504 || put_eol(fd) == FAIL)
23505 {
23506 vim_free(p);
23507 return FAIL;
23508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 vim_free(p);
23510 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023511#ifdef FEAT_FLOAT
23512 else if (this_var->di_tv.v_type == VAR_FLOAT
23513 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23514 {
23515 float_T f = this_var->di_tv.vval.v_float;
23516 int sign = ' ';
23517
23518 if (f < 0)
23519 {
23520 f = -f;
23521 sign = '-';
23522 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023523 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023524 this_var->di_key, sign, f) < 0)
23525 || put_eol(fd) == FAIL)
23526 return FAIL;
23527 }
23528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 }
23530 }
23531 return OK;
23532}
23533#endif
23534
Bram Moolenaar661b1822005-07-28 22:36:45 +000023535/*
23536 * Display script name where an item was last set.
23537 * Should only be invoked when 'verbose' is non-zero.
23538 */
23539 void
23540last_set_msg(scriptID)
23541 scid_T scriptID;
23542{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023543 char_u *p;
23544
Bram Moolenaar661b1822005-07-28 22:36:45 +000023545 if (scriptID != 0)
23546 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023547 p = home_replace_save(NULL, get_scriptname(scriptID));
23548 if (p != NULL)
23549 {
23550 verbose_enter();
23551 MSG_PUTS(_("\n\tLast set from "));
23552 MSG_PUTS(p);
23553 vim_free(p);
23554 verbose_leave();
23555 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023556 }
23557}
23558
Bram Moolenaard812df62008-11-09 12:46:09 +000023559/*
23560 * List v:oldfiles in a nice way.
23561 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023562 void
23563ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023564 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023565{
23566 list_T *l = vimvars[VV_OLDFILES].vv_list;
23567 listitem_T *li;
23568 int nr = 0;
23569
23570 if (l == NULL)
23571 msg((char_u *)_("No old files"));
23572 else
23573 {
23574 msg_start();
23575 msg_scroll = TRUE;
23576 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23577 {
23578 msg_outnum((long)++nr);
23579 MSG_PUTS(": ");
23580 msg_outtrans(get_tv_string(&li->li_tv));
23581 msg_putchar('\n');
23582 out_flush(); /* output one line at a time */
23583 ui_breakcheck();
23584 }
23585 /* Assume "got_int" was set to truncate the listing. */
23586 got_int = FALSE;
23587
23588#ifdef FEAT_BROWSE_CMD
23589 if (cmdmod.browse)
23590 {
23591 quit_more = FALSE;
23592 nr = prompt_for_number(FALSE);
23593 msg_starthere();
23594 if (nr > 0)
23595 {
23596 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23597 (long)nr);
23598
23599 if (p != NULL)
23600 {
23601 p = expand_env_save(p);
23602 eap->arg = p;
23603 eap->cmdidx = CMD_edit;
23604 cmdmod.browse = FALSE;
23605 do_exedit(eap, NULL);
23606 vim_free(p);
23607 }
23608 }
23609 }
23610#endif
23611 }
23612}
23613
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614#endif /* FEAT_EVAL */
23615
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023617#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618
23619#ifdef WIN3264
23620/*
23621 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23622 */
23623static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23624static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23625static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23626
23627/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023628 * Get the short path (8.3) for the filename in "fnamep".
23629 * Only works for a valid file name.
23630 * When the path gets longer "fnamep" is changed and the allocated buffer
23631 * is put in "bufp".
23632 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23633 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 */
23635 static int
23636get_short_pathname(fnamep, bufp, fnamelen)
23637 char_u **fnamep;
23638 char_u **bufp;
23639 int *fnamelen;
23640{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023641 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 char_u *newbuf;
23643
23644 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 l = GetShortPathName(*fnamep, *fnamep, len);
23646 if (l > len - 1)
23647 {
23648 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023649 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 newbuf = vim_strnsave(*fnamep, l);
23651 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653
23654 vim_free(*bufp);
23655 *fnamep = *bufp = newbuf;
23656
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023657 /* Really should always succeed, as the buffer is big enough. */
23658 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 }
23660
23661 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023662 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663}
23664
23665/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023666 * Get the short path (8.3) for the filename in "fname". The converted
23667 * path is returned in "bufp".
23668 *
23669 * Some of the directories specified in "fname" may not exist. This function
23670 * will shorten the existing directories at the beginning of the path and then
23671 * append the remaining non-existing path.
23672 *
23673 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023674 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023675 * bufp - Pointer to an allocated buffer for the filename.
23676 * fnamelen - Length of the filename pointed to by fname
23677 *
23678 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 */
23680 static int
23681shortpath_for_invalid_fname(fname, bufp, fnamelen)
23682 char_u **fname;
23683 char_u **bufp;
23684 int *fnamelen;
23685{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023686 char_u *short_fname, *save_fname, *pbuf_unused;
23687 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023689 int old_len, len;
23690 int new_len, sfx_len;
23691 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692
23693 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023694 old_len = *fnamelen;
23695 save_fname = vim_strnsave(*fname, old_len);
23696 pbuf_unused = NULL;
23697 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023699 endp = save_fname + old_len - 1; /* Find the end of the copy */
23700 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023702 /*
23703 * Try shortening the supplied path till it succeeds by removing one
23704 * directory at a time from the tail of the path.
23705 */
23706 len = 0;
23707 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023709 /* go back one path-separator */
23710 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23711 --endp;
23712 if (endp <= save_fname)
23713 break; /* processed the complete path */
23714
23715 /*
23716 * Replace the path separator with a NUL and try to shorten the
23717 * resulting path.
23718 */
23719 ch = *endp;
23720 *endp = 0;
23721 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023722 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023723 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23724 {
23725 retval = FAIL;
23726 goto theend;
23727 }
23728 *endp = ch; /* preserve the string */
23729
23730 if (len > 0)
23731 break; /* successfully shortened the path */
23732
23733 /* failed to shorten the path. Skip the path separator */
23734 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 }
23736
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023737 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739 /*
23740 * Succeeded in shortening the path. Now concatenate the shortened
23741 * path with the remaining path at the tail.
23742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023744 /* Compute the length of the new path. */
23745 sfx_len = (int)(save_endp - endp) + 1;
23746 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023748 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023750 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023752 /* There is not enough space in the currently allocated string,
23753 * copy it to a buffer big enough. */
23754 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 {
23757 retval = FAIL;
23758 goto theend;
23759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 }
23761 else
23762 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023763 /* Transfer short_fname to the main buffer (it's big enough),
23764 * unless get_short_pathname() did its work in-place. */
23765 *fname = *bufp = save_fname;
23766 if (short_fname != save_fname)
23767 vim_strncpy(save_fname, short_fname, len);
23768 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023770
23771 /* concat the not-shortened part of the path */
23772 vim_strncpy(*fname + len, endp, sfx_len);
23773 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023775
23776theend:
23777 vim_free(pbuf_unused);
23778 vim_free(save_fname);
23779
23780 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781}
23782
23783/*
23784 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023785 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786 */
23787 static int
23788shortpath_for_partial(fnamep, bufp, fnamelen)
23789 char_u **fnamep;
23790 char_u **bufp;
23791 int *fnamelen;
23792{
23793 int sepcount, len, tflen;
23794 char_u *p;
23795 char_u *pbuf, *tfname;
23796 int hasTilde;
23797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023798 /* Count up the path separators from the RHS.. so we know which part
23799 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023801 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 if (vim_ispathsep(*p))
23803 ++sepcount;
23804
23805 /* Need full path first (use expand_env() to remove a "~/") */
23806 hasTilde = (**fnamep == '~');
23807 if (hasTilde)
23808 pbuf = tfname = expand_env_save(*fnamep);
23809 else
23810 pbuf = tfname = FullName_save(*fnamep, FALSE);
23811
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023812 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023814 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816
23817 if (len == 0)
23818 {
23819 /* Don't have a valid filename, so shorten the rest of the
23820 * path if we can. This CAN give us invalid 8.3 filenames, but
23821 * there's not a lot of point in guessing what it might be.
23822 */
23823 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023824 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23825 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 }
23827
23828 /* Count the paths backward to find the beginning of the desired string. */
23829 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023830 {
23831#ifdef FEAT_MBYTE
23832 if (has_mbyte)
23833 p -= mb_head_off(tfname, p);
23834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 if (vim_ispathsep(*p))
23836 {
23837 if (sepcount == 0 || (hasTilde && sepcount == 1))
23838 break;
23839 else
23840 sepcount --;
23841 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 if (hasTilde)
23844 {
23845 --p;
23846 if (p >= tfname)
23847 *p = '~';
23848 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 }
23851 else
23852 ++p;
23853
23854 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23855 vim_free(*bufp);
23856 *fnamelen = (int)STRLEN(p);
23857 *bufp = pbuf;
23858 *fnamep = p;
23859
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023860 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861}
23862#endif /* WIN3264 */
23863
23864/*
23865 * Adjust a filename, according to a string of modifiers.
23866 * *fnamep must be NUL terminated when called. When returning, the length is
23867 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023868 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 * When there is an error, *fnamep is set to NULL.
23870 */
23871 int
23872modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23873 char_u *src; /* string with modifiers */
23874 int *usedlen; /* characters after src that are used */
23875 char_u **fnamep; /* file name so far */
23876 char_u **bufp; /* buffer for allocated file name or NULL */
23877 int *fnamelen; /* length of fnamep */
23878{
23879 int valid = 0;
23880 char_u *tail;
23881 char_u *s, *p, *pbuf;
23882 char_u dirname[MAXPATHL];
23883 int c;
23884 int has_fullname = 0;
23885#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023886 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 int has_shortname = 0;
23888#endif
23889
23890repeat:
23891 /* ":p" - full path/file_name */
23892 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23893 {
23894 has_fullname = 1;
23895
23896 valid |= VALID_PATH;
23897 *usedlen += 2;
23898
23899 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23900 if ((*fnamep)[0] == '~'
23901#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23902 && ((*fnamep)[1] == '/'
23903# ifdef BACKSLASH_IN_FILENAME
23904 || (*fnamep)[1] == '\\'
23905# endif
23906 || (*fnamep)[1] == NUL)
23907
23908#endif
23909 )
23910 {
23911 *fnamep = expand_env_save(*fnamep);
23912 vim_free(*bufp); /* free any allocated file name */
23913 *bufp = *fnamep;
23914 if (*fnamep == NULL)
23915 return -1;
23916 }
23917
23918 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023919 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 {
23921 if (vim_ispathsep(*p)
23922 && p[1] == '.'
23923 && (p[2] == NUL
23924 || vim_ispathsep(p[2])
23925 || (p[2] == '.'
23926 && (p[3] == NUL || vim_ispathsep(p[3])))))
23927 break;
23928 }
23929
23930 /* FullName_save() is slow, don't use it when not needed. */
23931 if (*p != NUL || !vim_isAbsName(*fnamep))
23932 {
23933 *fnamep = FullName_save(*fnamep, *p != NUL);
23934 vim_free(*bufp); /* free any allocated file name */
23935 *bufp = *fnamep;
23936 if (*fnamep == NULL)
23937 return -1;
23938 }
23939
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023940#ifdef WIN3264
23941# if _WIN32_WINNT >= 0x0500
23942 if (vim_strchr(*fnamep, '~') != NULL)
23943 {
23944 /* Expand 8.3 filename to full path. Needed to make sure the same
23945 * file does not have two different names.
23946 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23947 p = alloc(_MAX_PATH + 1);
23948 if (p != NULL)
23949 {
23950 if (GetLongPathName(*fnamep, p, MAXPATHL))
23951 {
23952 vim_free(*bufp);
23953 *bufp = *fnamep = p;
23954 }
23955 else
23956 vim_free(p);
23957 }
23958 }
23959# endif
23960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 /* Append a path separator to a directory. */
23962 if (mch_isdir(*fnamep))
23963 {
23964 /* Make room for one or two extra characters. */
23965 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23966 vim_free(*bufp); /* free any allocated file name */
23967 *bufp = *fnamep;
23968 if (*fnamep == NULL)
23969 return -1;
23970 add_pathsep(*fnamep);
23971 }
23972 }
23973
23974 /* ":." - path relative to the current directory */
23975 /* ":~" - path relative to the home directory */
23976 /* ":8" - shortname path - postponed till after */
23977 while (src[*usedlen] == ':'
23978 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23979 {
23980 *usedlen += 2;
23981 if (c == '8')
23982 {
23983#ifdef WIN3264
23984 has_shortname = 1; /* Postpone this. */
23985#endif
23986 continue;
23987 }
23988 pbuf = NULL;
23989 /* Need full path first (use expand_env() to remove a "~/") */
23990 if (!has_fullname)
23991 {
23992 if (c == '.' && **fnamep == '~')
23993 p = pbuf = expand_env_save(*fnamep);
23994 else
23995 p = pbuf = FullName_save(*fnamep, FALSE);
23996 }
23997 else
23998 p = *fnamep;
23999
24000 has_fullname = 0;
24001
24002 if (p != NULL)
24003 {
24004 if (c == '.')
24005 {
24006 mch_dirname(dirname, MAXPATHL);
24007 s = shorten_fname(p, dirname);
24008 if (s != NULL)
24009 {
24010 *fnamep = s;
24011 if (pbuf != NULL)
24012 {
24013 vim_free(*bufp); /* free any allocated file name */
24014 *bufp = pbuf;
24015 pbuf = NULL;
24016 }
24017 }
24018 }
24019 else
24020 {
24021 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24022 /* Only replace it when it starts with '~' */
24023 if (*dirname == '~')
24024 {
24025 s = vim_strsave(dirname);
24026 if (s != NULL)
24027 {
24028 *fnamep = s;
24029 vim_free(*bufp);
24030 *bufp = s;
24031 }
24032 }
24033 }
24034 vim_free(pbuf);
24035 }
24036 }
24037
24038 tail = gettail(*fnamep);
24039 *fnamelen = (int)STRLEN(*fnamep);
24040
24041 /* ":h" - head, remove "/file_name", can be repeated */
24042 /* Don't remove the first "/" or "c:\" */
24043 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24044 {
24045 valid |= VALID_HEAD;
24046 *usedlen += 2;
24047 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024048 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024049 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 *fnamelen = (int)(tail - *fnamep);
24051#ifdef VMS
24052 if (*fnamelen > 0)
24053 *fnamelen += 1; /* the path separator is part of the path */
24054#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024055 if (*fnamelen == 0)
24056 {
24057 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24058 p = vim_strsave((char_u *)".");
24059 if (p == NULL)
24060 return -1;
24061 vim_free(*bufp);
24062 *bufp = *fnamep = tail = p;
24063 *fnamelen = 1;
24064 }
24065 else
24066 {
24067 while (tail > s && !after_pathsep(s, tail))
24068 mb_ptr_back(*fnamep, tail);
24069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 }
24071
24072 /* ":8" - shortname */
24073 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24074 {
24075 *usedlen += 2;
24076#ifdef WIN3264
24077 has_shortname = 1;
24078#endif
24079 }
24080
24081#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024082 /*
24083 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 */
24085 if (has_shortname)
24086 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024087 /* Copy the string if it is shortened by :h and when it wasn't copied
24088 * yet, because we are going to change it in place. Avoids changing
24089 * the buffer name for "%:8". */
24090 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 {
24092 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024093 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 return -1;
24095 vim_free(*bufp);
24096 *bufp = *fnamep = p;
24097 }
24098
24099 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024100 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 if (!has_fullname && !vim_isAbsName(*fnamep))
24102 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024103 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 return -1;
24105 }
24106 else
24107 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024108 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109
Bram Moolenaardc935552011-08-17 15:23:23 +020024110 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024112 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 return -1;
24114
24115 if (l == 0)
24116 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024117 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024119 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 return -1;
24121 }
24122 *fnamelen = l;
24123 }
24124 }
24125#endif /* WIN3264 */
24126
24127 /* ":t" - tail, just the basename */
24128 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24129 {
24130 *usedlen += 2;
24131 *fnamelen -= (int)(tail - *fnamep);
24132 *fnamep = tail;
24133 }
24134
24135 /* ":e" - extension, can be repeated */
24136 /* ":r" - root, without extension, can be repeated */
24137 while (src[*usedlen] == ':'
24138 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24139 {
24140 /* find a '.' in the tail:
24141 * - for second :e: before the current fname
24142 * - otherwise: The last '.'
24143 */
24144 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24145 s = *fnamep - 2;
24146 else
24147 s = *fnamep + *fnamelen - 1;
24148 for ( ; s > tail; --s)
24149 if (s[0] == '.')
24150 break;
24151 if (src[*usedlen + 1] == 'e') /* :e */
24152 {
24153 if (s > tail)
24154 {
24155 *fnamelen += (int)(*fnamep - (s + 1));
24156 *fnamep = s + 1;
24157#ifdef VMS
24158 /* cut version from the extension */
24159 s = *fnamep + *fnamelen - 1;
24160 for ( ; s > *fnamep; --s)
24161 if (s[0] == ';')
24162 break;
24163 if (s > *fnamep)
24164 *fnamelen = s - *fnamep;
24165#endif
24166 }
24167 else if (*fnamep <= tail)
24168 *fnamelen = 0;
24169 }
24170 else /* :r */
24171 {
24172 if (s > tail) /* remove one extension */
24173 *fnamelen = (int)(s - *fnamep);
24174 }
24175 *usedlen += 2;
24176 }
24177
24178 /* ":s?pat?foo?" - substitute */
24179 /* ":gs?pat?foo?" - global substitute */
24180 if (src[*usedlen] == ':'
24181 && (src[*usedlen + 1] == 's'
24182 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24183 {
24184 char_u *str;
24185 char_u *pat;
24186 char_u *sub;
24187 int sep;
24188 char_u *flags;
24189 int didit = FALSE;
24190
24191 flags = (char_u *)"";
24192 s = src + *usedlen + 2;
24193 if (src[*usedlen + 1] == 'g')
24194 {
24195 flags = (char_u *)"g";
24196 ++s;
24197 }
24198
24199 sep = *s++;
24200 if (sep)
24201 {
24202 /* find end of pattern */
24203 p = vim_strchr(s, sep);
24204 if (p != NULL)
24205 {
24206 pat = vim_strnsave(s, (int)(p - s));
24207 if (pat != NULL)
24208 {
24209 s = p + 1;
24210 /* find end of substitution */
24211 p = vim_strchr(s, sep);
24212 if (p != NULL)
24213 {
24214 sub = vim_strnsave(s, (int)(p - s));
24215 str = vim_strnsave(*fnamep, *fnamelen);
24216 if (sub != NULL && str != NULL)
24217 {
24218 *usedlen = (int)(p + 1 - src);
24219 s = do_string_sub(str, pat, sub, flags);
24220 if (s != NULL)
24221 {
24222 *fnamep = s;
24223 *fnamelen = (int)STRLEN(s);
24224 vim_free(*bufp);
24225 *bufp = s;
24226 didit = TRUE;
24227 }
24228 }
24229 vim_free(sub);
24230 vim_free(str);
24231 }
24232 vim_free(pat);
24233 }
24234 }
24235 /* after using ":s", repeat all the modifiers */
24236 if (didit)
24237 goto repeat;
24238 }
24239 }
24240
24241 return valid;
24242}
24243
24244/*
24245 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24246 * "flags" can be "g" to do a global substitute.
24247 * Returns an allocated string, NULL for error.
24248 */
24249 char_u *
24250do_string_sub(str, pat, sub, flags)
24251 char_u *str;
24252 char_u *pat;
24253 char_u *sub;
24254 char_u *flags;
24255{
24256 int sublen;
24257 regmatch_T regmatch;
24258 int i;
24259 int do_all;
24260 char_u *tail;
24261 garray_T ga;
24262 char_u *ret;
24263 char_u *save_cpo;
24264
24265 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24266 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024267 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268
24269 ga_init2(&ga, 1, 200);
24270
24271 do_all = (flags[0] == 'g');
24272
24273 regmatch.rm_ic = p_ic;
24274 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24275 if (regmatch.regprog != NULL)
24276 {
24277 tail = str;
24278 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24279 {
24280 /*
24281 * Get some space for a temporary buffer to do the substitution
24282 * into. It will contain:
24283 * - The text up to where the match is.
24284 * - The substituted text.
24285 * - The text after the match.
24286 */
24287 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24288 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24289 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24290 {
24291 ga_clear(&ga);
24292 break;
24293 }
24294
24295 /* copy the text up to where the match is */
24296 i = (int)(regmatch.startp[0] - tail);
24297 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24298 /* add the substituted text */
24299 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24300 + ga.ga_len + i, TRUE, TRUE, FALSE);
24301 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 /* avoid getting stuck on a match with an empty string */
24303 if (tail == regmatch.endp[0])
24304 {
24305 if (*tail == NUL)
24306 break;
24307 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24308 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 }
24310 else
24311 {
24312 tail = regmatch.endp[0];
24313 if (*tail == NUL)
24314 break;
24315 }
24316 if (!do_all)
24317 break;
24318 }
24319
24320 if (ga.ga_data != NULL)
24321 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24322
Bram Moolenaar473de612013-06-08 18:19:48 +020024323 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 }
24325
24326 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24327 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024328 if (p_cpo == empty_option)
24329 p_cpo = save_cpo;
24330 else
24331 /* Darn, evaluating {sub} expression changed the value. */
24332 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333
24334 return ret;
24335}
24336
24337#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */